Example #1
0
    private void OnMyEnable()
    {
        TMProFontCustomizedCreater.CustomizedCreaterSettings settings =
            TMProFontCustomizedCreater.GetCustomizedCreaterSettings();

        // 以字体做索引,相同的字体只会生成一次字体纹理
        string str1 = "t:Font";

        string[] fonts = AssetDatabase.FindAssets(str1, new[] { settings.fontFolderPath });

        m_FontAssetInfos.Clear();
        foreach (var font in fonts)
        {
            FontAssetInfo info = new FontAssetInfo();
            info.fontPath = AssetDatabase.GUIDToAssetPath(font);
            info.fontName = Path.GetFileNameWithoutExtension(info.fontPath);

            List <string> assetPaths = new List <string>();
            str1 = "t:TMP_FontAsset " + info.fontName + " SDF";
            var assets = AssetDatabase.FindAssets(str1, new[] { settings.fontMaterialsFolderPath });
            foreach (var asset in assets)
            {
                assetPaths.Add(AssetDatabase.GUIDToAssetPath(asset));
            }

            if (assetPaths.Count > 0)
            {
                info.assets = assetPaths.ToArray();
                m_FontAssetInfos.Add(info);
            }
        }

        m_PointSizeSamplingMode = settings.pointSizeSamplingMode;
        m_PointSize             = settings.pointSize;
        m_Padding     = settings.padding;
        m_PackingMode = (FontPackingModes)settings.packingMode;
        m_AtlasWidth  = settings.atlasWidth;
        m_AtlasHeight = settings.atlasHeight;
        m_CharacterSetSelectionMode = settings.characterSetSelectionMode;
        m_CharacterSequenceFile     = settings.characterSequenceFile;
        m_FontStyle              = (FaceStyles)settings.fontStyle;
        m_FontStyleValue         = settings.fontStyleModifier;
        m_RenderMode             = (RenderModes)settings.renderMode;
        m_IncludeKerningPairs    = settings.includeFontFeatures;
        m_FontBackupPaths        = settings.fontBackupPaths;
        m_CharacterUseFontBackup = settings.characterUseFontBackup;

        if (string.IsNullOrEmpty(m_WarningMessage) || m_SelectedFontAsset || m_LegacyFontAsset || m_SavedFontAtlas || m_IsFontAtlasInvalid)
        {
            // 仅为了去除警告
        }
    }
Example #2
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);
    }