Unity使用Sprite图集制作UGUI字体

20 篇文章 2 订阅
using UnityEngine;
using UnityEditor;
using System.IO;

public class CreateFont : EditorWindow
{
    [MenuItem("Tools/创建字体(sprite)")]
    public static void Open()
    {
        GetWindow<CreateFont>("创建字体");
    }

    private Texture2D tex;
    private string fontName;
    private string fontPath;

    private void OnGUI()
    {
        GUILayout.BeginVertical();

        GUILayout.BeginHorizontal();
        GUILayout.Label("字体图片:");
        tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("字体名称:");
        fontName = EditorGUILayout.TextField(fontName);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button(string.IsNullOrEmpty(fontPath) ? "选择路径" : fontPath))
        {
            fontPath = EditorUtility.OpenFolderPanel("字体路径", Application.dataPath, "");
            if (string.IsNullOrEmpty(fontPath))
            {
                Debug.Log("取消选择路径");
            }
            else
            {
                fontPath = fontPath.Replace(Application.dataPath, "") + "/";
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("创建"))
        {
            Create();
        }
        GUILayout.EndHorizontal();

        GUILayout.EndVertical();
    }

    private void Create()
    {
        if (tex == null)
        {
            Debug.LogWarning("创建失败,图片为空!");
            return;
        }

        if (string.IsNullOrEmpty(fontPath))
        {
            Debug.LogWarning("字体路径为空!");
            return;
        }
        if (fontName == null)
        {
            Debug.LogWarning("创建失败,字体名称为空!");
            return;
        }
        else
        {
            if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings"))
            {
                Debug.LogError("创建失败,已存在同名字体文件");
                return;
            }
            if (File.Exists(Application.dataPath + fontPath + fontName + ".mat"))
            {
                Debug.LogError("创建失败,已存在同名字体材质文件");
                return;
            }
        }

        string selectionPath = AssetDatabase.GetAssetPath(tex);
        string selectionExt = Path.GetExtension(selectionPath);
        if (selectionExt.Length == 0)
        {
            Debug.LogError("创建失败!");
            return;
        }

        string fontPathName = fontPath + fontName + ".fontsettings";
        string matPathName = fontPath + fontName + ".mat";
        float lineSpace = 0.1f;
        AssetImporter assetImporter = AssetImporter.GetAtPath(selectionPath);
        Debug.Log(assetImporter.assetBundleName);
        string assetBundleName = assetImporter.assetBundleName;
        Debug.Log(assetBundleName);
        string loadpath = "c:/simpleframework/" + assetBundleName;
        //string loadpath = Application.dataPath + "/StreamingAssets/"+ assetBundleName;
        Debug.Log(loadpath);
        Debug.Log(selectionPath);
        AssetBundle.UnloadAllAssetBundles(true);
        //byte[] stream = File.ReadAllBytes(loadpath);
        //AssetBundle ab = AssetBundle.LoadFromMemory(stream);
        AssetBundle ab = AssetBundle.LoadFromFile(loadpath);
        string tempName = Path.GetFileName(selectionPath);
        string assetName = tempName.Replace(Path.GetExtension(tempName),"");
        Debug.Log(assetName);
        Sprite[] sprites = ab.LoadAssetWithSubAssets<Sprite>(assetName);
        Debug.Log(sprites.Length);
        if (sprites.Length > 0)
        {
            Material mat = new Material(Shader.Find("GUI/Text Shader"));
            mat.SetTexture("_MainTex", tex);
            Font m_myFont = new Font();
            m_myFont.material = mat;
            CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
            for (int i = 0; i < sprites.Length; i++)
            {
                if (sprites[i].rect.height > lineSpace)
                {
                    lineSpace = sprites[i].rect.height;
                }
            }
            for (int i = 0; i < sprites.Length; i++)
            {
                Sprite spr = sprites[i];
                CharacterInfo info = new CharacterInfo();
                try
                {
                    Debug.Log(spr.name);
                    info.index = System.Convert.ToInt32(spr.name);
                }
                catch
                {
                    Debug.LogError("创建失败,Sprite名称错误!");
                    return;
                }
                Rect rect = spr.rect;
                float pivot = spr.pivot.y / rect.height - 0.5f;
                if (pivot > 0)
                {
                    pivot = -lineSpace / 2 - spr.pivot.y;
                }
                else if (pivot < 0)
                {
                    pivot = -lineSpace / 2 + rect.height - spr.pivot.y;
                }
                else
                {
                    pivot = -lineSpace / 2;
                }
                int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
                info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);
                info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);
                info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);
                info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y + rect.height) / tex.height);
                info.minX = 0;
                info.minY = -(int)rect.height - offsetY;
                info.maxX = (int)rect.width;
                info.maxY = -offsetY;
                info.advance = (int)rect.width;
                characterInfo[i] = info;
            }
            AssetDatabase.CreateAsset(mat, "Assets" + matPathName);
            AssetDatabase.CreateAsset(m_myFont, "Assets" + fontPathName);
            m_myFont.characterInfo = characterInfo;
            EditorUtility.SetDirty(m_myFont);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();//刷新资源
            Debug.Log("创建字体成功");

        }
        else
        {
            Debug.LogError("图集错误!");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值