コード例 #1
0
    void Save_SDF_FontAsset(string filePath)
    {
        //filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath.

        //string dataPath = Application.dataPath;

        //if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1)
        //{
        //    Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\"");
        //    return;
        //}

        string relativeAssetPath = filePath;
        string tex_DirName       = Path.GetDirectoryName(relativeAssetPath);
        string tex_FileName      = Path.GetFileNameWithoutExtension(relativeAssetPath);
        string tex_Path_NoExt    = tex_DirName + "/" + tex_FileName;


        // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one.
        TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(tex_Path_NoExt + ".asset");

        if (fontAsset == null)
        {
            //Debug.Log("Creating TextMeshPro font asset!");
            fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset.
            AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset");

            // Reference to the source font file
            //font_asset.sourceFontFile = font_TTF as Font;

            //Set Font Asset Type
            fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF;

            //if (m_destination_Atlas != null)
            //    m_font_Atlas = m_destination_Atlas;

            // If using the C# SDF creation mode, we need the scale down factor.
            int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor;

            // Add FaceInfo to Font Asset
            FaceInfo face = GetFaceInfo(m_FontFaceInfo, scaleDownFactor);
            fontAsset.AddFaceInfo(face);

            // Add GlyphInfo[] to Font Asset
            TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, scaleDownFactor);
            fontAsset.AddGlyphInfo(glyphs);

            // Get and Add Kerning Pairs to Font Asset
            if (m_IncludeKerningPairs)
            {
                string       fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile);
                KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize);
                fontAsset.AddKerningInfo(kerningTable);
            }

            // Add Line Breaking Rules
            //LineBreakingTable lineBreakingTable = new LineBreakingTable();
            //

            // Add Font Atlas as Sub-Asset
            fontAsset.atlas = m_FontAtlas;
            if (!m_FontAtlas.name.EndsWith(" Atlas")) // 因为图集复用,所以只要加到第一个资产里
            {
                m_FontAtlas.name = tex_FileName + " Atlas";
                AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset);
            }

            // Create new Material and Add it as Sub-Asset
            Shader   default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection;
            Material tmp_material   = new Material(default_Shader);

            tmp_material.name = tex_FileName + " Material";
            tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas);
            tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlas.width);
            tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlas.height);

            int spread = m_Padding + 1;
            tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF.

            tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
            tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);

            fontAsset.material = tmp_material;

            AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
        }
        else
        {
            // Find all Materials referencing this font atlas.
            Material[] material_references = TMProFontCustomizedCreater.FindMaterialReferences(fontAsset);

            if (fontAsset.atlas) // 有可能被其他资产删除了
            {
                // Destroy Assets that will be replaced.
                DestroyImmediate(fontAsset.atlas, true);
            }

            //Set Font Asset Type
            fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF;

            int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor;
                                     // Add FaceInfo to Font Asset
            FaceInfo face = GetFaceInfo(m_FontFaceInfo, scaleDownFactor);
            fontAsset.AddFaceInfo(face);

            // Add GlyphInfo[] to Font Asset
            TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, scaleDownFactor);
            fontAsset.AddGlyphInfo(glyphs);

            // Get and Add Kerning Pairs to Font Asset
            if (m_IncludeKerningPairs)
            {
                string       fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile);
                KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize);
                fontAsset.AddKerningInfo(kerningTable);
            }

            // Add Font Atlas as Sub-Asset
            fontAsset.atlas = m_FontAtlas;
            if (!m_FontAtlas.name.EndsWith(" Atlas")) // 因为图集复用,所以只要加到第一个资产里
            {
                m_FontAtlas.name = tex_FileName + " Atlas";
                AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset);
            }

            // Special handling due to a bug in earlier versions of Unity.
            m_FontAtlas.hideFlags        = HideFlags.None;
            fontAsset.material.hideFlags = HideFlags.None;

            // Assign new font atlas texture to the existing material.
            fontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, fontAsset.atlas);

            // Update the Texture reference on the Material
            for (int i = 0; i < material_references.Length; i++)
            {
                material_references[i].SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas);
                material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlas.width);
                material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlas.height);

                int spread = m_Padding + 1;
                material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF.

                material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
                material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
            }
        }

        // Saving File for Debug
        //var pngData = m_FontAtlas.EncodeToPNG();
        //File.WriteAllBytes("Assets/Debug Distance Field.png", pngData);

        // Save Font Asset creation settings
        m_SelectedFontAsset = fontAsset;
        m_LegacyFontAsset   = null;

        // 提到这里才能保存完整
        fontAsset.ReadFontDefinition();

        AssetDatabase.SaveAssets();

        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset));  // Re-import font asset to get the new updated version.

        //fontAsset.ReadFontDefinition();

        AssetDatabase.Refresh();

        //m_FontAtlas = null; 贴图不要删除

        // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET
        TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
    }
コード例 #2
0
        private void Save_SDF_FontAsset(string filePath)
        {
            filePath = filePath.Substring(0, filePath.Length - 6);
            string dataPath = Application.dataPath;

            if (filePath.IndexOf(dataPath, StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\"");
                return;
            }
            string        path                     = filePath.Substring(dataPath.Length - 6);
            string        directoryName            = Path.GetDirectoryName(path);
            string        fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
            string        str           = directoryName + "/" + fileNameWithoutExtension;
            TMP_FontAsset tMP_FontAsset = AssetDatabase.LoadAssetAtPath(str + ".asset", typeof(TMP_FontAsset)) as TMP_FontAsset;

            if (tMP_FontAsset == null)
            {
                tMP_FontAsset = ScriptableObject.CreateInstance <WeaverCore.Assets.TMPro.TMP_FontAsset>();
                AssetDatabase.CreateAsset(tMP_FontAsset, str + ".asset");
                tMP_FontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF;
                int      scaleFactor = (font_renderMode >= RenderModes.DistanceField16) ? 1 : font_scaledownFactor;
                FaceInfo faceInfo    = GetFaceInfo(m_font_faceInfo, scaleFactor);
                tMP_FontAsset.AddFaceInfo(faceInfo);
                TMP_Glyph[] glyphInfo = GetGlyphInfo(m_font_glyphInfo, scaleFactor);
                tMP_FontAsset.AddGlyphInfo(glyphInfo);
                if (includeKerningPairs)
                {
                    string       assetPath    = AssetDatabase.GetAssetPath(font_TTF);
                    KerningTable kerningTable = GetKerningTable(assetPath, (int)faceInfo.PointSize);
                    tMP_FontAsset.AddKerningInfo(kerningTable);
                }
                tMP_FontAsset.atlas = m_font_Atlas;
                m_font_Atlas.name   = fileNameWithoutExtension + " Atlas";
                AssetDatabase.AddObjectToAsset(m_font_Atlas, tMP_FontAsset);
                Shader   shader   = Shader.Find("TextMeshPro/Distance Field");
                Material material = new Material(shader);
                material.name = fileNameWithoutExtension + " Material";
                material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas);
                material.SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width);
                material.SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height);
                int num = font_padding + 1;
                material.SetFloat(ShaderUtilities.ID_GradientScale, num);
                material.SetFloat(ShaderUtilities.ID_WeightNormal, tMP_FontAsset.normalStyle);
                material.SetFloat(ShaderUtilities.ID_WeightBold, tMP_FontAsset.boldStyle);
                tMP_FontAsset.material = material;
                AssetDatabase.AddObjectToAsset(material, tMP_FontAsset);
            }
            else
            {
                Material[] array = TMP_EditorUtility.FindMaterialReferences(tMP_FontAsset);
                UnityEngine.Object.DestroyImmediate(tMP_FontAsset.atlas, true);
                tMP_FontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF;
                int      scaleFactor2 = (font_renderMode >= RenderModes.DistanceField16) ? 1 : font_scaledownFactor;
                FaceInfo faceInfo2    = GetFaceInfo(m_font_faceInfo, scaleFactor2);
                tMP_FontAsset.AddFaceInfo(faceInfo2);
                TMP_Glyph[] glyphInfo2 = GetGlyphInfo(m_font_glyphInfo, scaleFactor2);
                tMP_FontAsset.AddGlyphInfo(glyphInfo2);
                if (includeKerningPairs)
                {
                    string       assetPath2    = AssetDatabase.GetAssetPath(font_TTF);
                    KerningTable kerningTable2 = GetKerningTable(assetPath2, (int)faceInfo2.PointSize);
                    tMP_FontAsset.AddKerningInfo(kerningTable2);
                }
                tMP_FontAsset.atlas              = m_font_Atlas;
                m_font_Atlas.name                = fileNameWithoutExtension + " Atlas";
                m_font_Atlas.hideFlags           = HideFlags.None;
                tMP_FontAsset.material.hideFlags = HideFlags.None;
                AssetDatabase.AddObjectToAsset(m_font_Atlas, tMP_FontAsset);
                tMP_FontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, tMP_FontAsset.atlas);
                for (int i = 0; i < array.Length; i++)
                {
                    array[i].SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas);
                    array[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width);
                    array[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height);
                    int num2 = font_padding + 1;
                    array[i].SetFloat(ShaderUtilities.ID_GradientScale, num2);
                    array[i].SetFloat(ShaderUtilities.ID_WeightNormal, tMP_FontAsset.normalStyle);
                    array[i].SetFloat(ShaderUtilities.ID_WeightBold, tMP_FontAsset.boldStyle);
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tMP_FontAsset));
            tMP_FontAsset.ReadFontDefinition();
            AssetDatabase.Refresh();
            m_font_Atlas = null;
            TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, tMP_FontAsset);
        }
コード例 #3
0
    void Save_Normal_FontAsset(string filePath)
    {
        //filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath.

        //string dataPath = Application.dataPath;

        //if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1)
        //{
        //    Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\"");
        //    return;
        //}

        string relativeAssetPath = filePath;
        string tex_DirName       = Path.GetDirectoryName(relativeAssetPath);
        string tex_FileName      = Path.GetFileNameWithoutExtension(relativeAssetPath);
        string tex_Path_NoExt    = tex_DirName + "/" + tex_FileName;

        // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one.
        TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TMP_FontAsset)) as TMP_FontAsset;

        if (fontAsset == null)
        {
            //Debug.Log("Creating TextMeshPro font asset!");
            fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset.
            AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset");

            //Set Font Asset Type
            fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.Bitmap;

            // Reference to the source font file
            //font_asset.sourceFontFile = font_TTF as Font;

            // Add FaceInfo to Font Asset
            FaceInfo face = GetFaceInfo(m_FontFaceInfo, 1);
            fontAsset.AddFaceInfo(face);

            // Add GlyphInfo[] to Font Asset
            TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, 1);
            fontAsset.AddGlyphInfo(glyphs);

            // Get and Add Kerning Pairs to Font Asset
            if (m_IncludeKerningPairs)
            {
                string       fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile);
                KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize);
                fontAsset.AddKerningInfo(kerningTable);
            }


            // Add Font Atlas as Sub-Asset
            fontAsset.atlas  = m_FontAtlas;
            m_FontAtlas.name = tex_FileName + " Atlas";

            AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset);

            // Create new Material and Add it as Sub-Asset
            Shader   default_Shader = Shader.Find("TextMeshPro/Bitmap"); // m_shaderSelection;
            Material tmp_material   = new Material(default_Shader);
            tmp_material.name = tex_FileName + " Material";
            tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas);
            fontAsset.material = tmp_material;

            AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
        }
        else
        {
            // Find all Materials referencing this font atlas.
            Material[] material_references = TMP_EditorUtility.FindMaterialReferences(fontAsset);

            // Destroy Assets that will be replaced.
            DestroyImmediate(fontAsset.atlas, true);

            //Set Font Asset Type
            fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.Bitmap;

            // Add FaceInfo to Font Asset
            FaceInfo face = GetFaceInfo(m_FontFaceInfo, 1);
            fontAsset.AddFaceInfo(face);

            // Add GlyphInfo[] to Font Asset
            TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, 1);
            fontAsset.AddGlyphInfo(glyphs);

            // Get and Add Kerning Pairs to Font Asset
            if (m_IncludeKerningPairs)
            {
                string       fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile);
                KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize);
                fontAsset.AddKerningInfo(kerningTable);
            }

            // Add Font Atlas as Sub-Asset
            fontAsset.atlas  = m_FontAtlas;
            m_FontAtlas.name = tex_FileName + " Atlas";

            // Special handling due to a bug in earlier versions of Unity.
            m_FontAtlas.hideFlags        = HideFlags.None;
            fontAsset.material.hideFlags = HideFlags.None;

            AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset);

            // Assign new font atlas texture to the existing material.
            fontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, fontAsset.atlas);

            // Update the Texture reference on the Material
            for (int i = 0; i < material_references.Length; i++)
            {
                material_references[i].SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas);
            }
        }

        // Save Font Asset creation settings
        m_SelectedFontAsset = fontAsset;
        m_LegacyFontAsset   = null;

        AssetDatabase.SaveAssets();

        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset));  // Re-import font asset to get the new updated version.

        //EditorUtility.SetDirty(font_asset);
        fontAsset.ReadFontDefinition();

        AssetDatabase.Refresh();

        m_FontAtlas = null;

        // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET
        TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
    }