Пример #1
0
        void UpdateFont(TMP_FontAsset fontFile)
        {
            var fontText = m_SourceFontFile.text;
            var fnt      = FntParse.GetFntParse(ref fontText);

            for (int i = 0; i < fontFile.characterTable.Count; i++)
            {
                var unicode    = fontFile.characterTable[i].unicode;
                var glyphIndex = fontFile.characterTable[i].glyphIndex;
                for (int j = 0; j < fnt.charInfos.Length; j++)
                {
                    if (unicode == fnt.charInfos[j].index)
                    {
                        var glyph = fontFile.glyphLookupTable[glyphIndex];
                        PatchGlyph(fnt.rawCharInfos[j],
                                   fnt.textureHeight,
                                   fnt.textureWidth,
                                   ref glyph);
                        fontFile.glyphLookupTable[glyphIndex] = glyph;
                        break;
                    }
                }
            }

            var newFaceInfo = fontFile.faceInfo;

            newFaceInfo.baseline   = fnt.lineBaseHeight;
            newFaceInfo.lineHeight = fnt.lineHeight;
            newFaceInfo.ascentLine = fnt.lineHeight;
            newFaceInfo.pointSize  = fnt.fontSize;

            var fontType         = typeof(TMP_FontAsset);
            var faceInfoProperty = fontType.GetProperty("faceInfo");

            faceInfoProperty.SetValue(fontFile, newFaceInfo);

            fontFile.material.SetTexture("_MainTex", m_Texture2D);
            fontFile.atlasTextures[0] = m_Texture2D;
        }
Пример #2
0
    private void Build()
    {
        var texturePath = AssetDatabase.GetAssetPath(Texture);
        var rootPath    = Path.GetDirectoryName(texturePath);
        var fontPath    = string.Format("{0}/{1}.fontsettings", rootPath, Texture.name);
        var objs        = AssetDatabase.LoadAllAssetsAtPath(texturePath).ToList();

        objs.RemoveAt(0);
        Sprite[] sprites = objs.Cast <Sprite>().ToArray();

        string   text  = GetFntFormat(Characters, sprites);
        FntParse parse = FntParse.GetFntParse(ref text);

        if (parse == null)
        {
            return;
        }
        Texture2D[] textures = DoImportTextures(parse, rootPath, text);
        Font        font     = AssetDatabase.LoadMainAssetAtPath(fontPath) as Font;

        if (font == null)
        {
            font = new Font();
            AssetDatabase.CreateAsset(font, fontPath);
            AssetDatabase.WriteImportSettingsIfDirty(fontPath);
            AssetDatabase.ImportAsset(fontPath);
        }
        Material material = AssetDatabase.LoadAssetAtPath(fontPath, typeof(Material)) as Material;

        if (material == null)
        {
            material      = new Material(Shader.Find("UI/Default"));
            material.name = "Font Material";
            AssetDatabase.AddObjectToAsset(material, fontPath);
            AssetDatabase.ImportAsset(fontPath);
        }
        font.material        = material;
        material.shader      = Shader.Find(textures.Length > 1 ? "BFI/Font" + textures.Length : "UI/Default");
        material.mainTexture = textures[0];
        for (int i = 1; i < textures.Length; i++)
        {
            material.SetTexture("_MainTex" + (i + 1), textures[i]);
        }
        font.characterInfo = parse.charInfos;
        SerializedObject so = new SerializedObject(font);

        so.Update();
        so.FindProperty("m_FontSize").floatValue    = Mathf.Abs(parse.fontSize);
        so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight;
        so.FindProperty("m_Ascent").floatValue      = parse.lineBaseHeight;
        SerializedProperty prop = so.FindProperty("m_Descent");

        if (prop != null)
        {
            prop.floatValue = parse.lineBaseHeight - parse.lineHeight;
        }
        UpdateKernings(so, parse.kernings);
        so.ApplyModifiedProperties();
        so.SetIsDifferentCacheDirty();
        AssetDatabase.SaveAssets();
    }