コード例 #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) == -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.Substring(dataPath.Length - 6);
            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.
            TextMeshProFont font_asset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TextMeshProFont)) as TextMeshProFont;

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

                if (m_destination_Atlas != null)
                {
                    m_font_Atlas = m_destination_Atlas;
                }

                // If using the C# SDF creation mode, we need the scaledown factor.
                int scaleDownFactor = font_renderMode >= RenderModes.DistanceField16 ? 1 : font_scaledownFactor;

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

                // Add GlyphInfo[] to Font Asset
                GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, scaleDownFactor);
                font_asset.AddGlyphInfo(glyphs);

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

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

                // Add Font Atlas as Sub-Asset
                font_asset.atlas       = m_font_Atlas;
                m_font_Atlas.name      = tex_FileName + " Atlas";
                m_font_Atlas.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset);

                // Create new Material and Add it as Sub-Asset
                Shader   default_Shader = Shader.Find("TMPro/Distance Field");
                Material tmp_material   = new Material(default_Shader);
                //tmp_material.shaderKeywords = new string[] { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" };
                tmp_material.name = tex_FileName + " Material";
                tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas);
                tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width);
                tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height);


                tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, font_asset.NormalStyle);
                tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, font_asset.BoldStyle);

                int spread = font_renderMode >= RenderModes.DistanceField16 ? font_padding + 1 : font_spread;
                tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF.

                font_asset.material    = tmp_material;
                tmp_material.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(tmp_material, font_asset);
            }
            else
            {
                // Find all Materials referencing this font atlas.
                Material[] material_references = TMPro_EditorUtility.FindMaterialReferences(font_asset.material);

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

                int scaleDownFactor = font_renderMode >= RenderModes.DistanceField16 ? 1 : font_scaledownFactor;
                // Add FaceInfo to Font Asset
                FaceInfo face = GetFaceInfo(m_font_faceInfo, scaleDownFactor);
                font_asset.AddFaceInfo(face);

                // Add GlyphInfo[] to Font Asset
                GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, scaleDownFactor);
                font_asset.AddGlyphInfo(glyphs);

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

                // Add Font Atlas as Sub-Asset
                font_asset.atlas       = m_font_Atlas;
                m_font_Atlas.name      = tex_FileName + " Atlas";
                m_font_Atlas.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset);

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

                    material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, font_asset.NormalStyle);
                    material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, font_asset.BoldStyle);

                    int spread = font_renderMode >= RenderModes.DistanceField16 ? font_padding + 1 : font_spread;
                    material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF.
                }
            }

            // Saving File for Debug
            //var pngData = destination_Atlas.EncodeToPNG();
            //File.WriteAllBytes("Assets/Textures/Debug Distance Field.png", pngData);
            //font_asset.fontCreationSettings = SaveFontCreationSettings();


            AssetDatabase.SaveAssets();

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

            font_asset.ReadFontDefinition();

            AssetDatabase.Refresh();

            // 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, font_asset);
        }
コード例 #2
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) == -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.Substring(dataPath.Length - 6);
            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.
            TextMeshProFont font_asset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TextMeshProFont)) as TextMeshProFont;

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

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

                // Add GlyphInfo[] to Font Asset
                GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, 1);
                font_asset.AddGlyphInfo(glyphs);

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


                // Add Font Atlas as Sub-Asset
                font_asset.atlas       = m_font_Atlas;
                m_font_Atlas.name      = tex_FileName + " Atlas";
                m_font_Atlas.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset);

                // Create new Material and Add it as Sub-Asset
                Shader   default_Shader = Shader.Find("TMPro/Bitmap");
                Material tmp_material   = new Material(default_Shader);
                tmp_material.name = tex_FileName + " Material";
                tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas);
                font_asset.material    = tmp_material;
                tmp_material.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(tmp_material, font_asset);
            }
            else
            {
                // Find all Materials referencing this font atlas.
                Material[] material_references = TMPro_EditorUtility.FindMaterialReferences(font_asset.material);

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

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

                // Add GlyphInfo[] to Font Asset
                GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, 1);
                font_asset.AddGlyphInfo(glyphs);

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

                // Add Font Atlas as Sub-Asset
                font_asset.atlas       = m_font_Atlas;
                m_font_Atlas.name      = tex_FileName + " Atlas";
                m_font_Atlas.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset);

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

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

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

            AssetDatabase.Refresh();

            // 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, font_asset);
        }