【UGUI】简单的美术字体的制作(教你写插件)

在  UGUI 使用中我们常常用到美术字体,然而有时却没有那么复杂那么多,再此介绍下生成美术字体的原理


选中预先制作好的图片


点击Go 就可以生成一个简单的字体了

使用也很简单

要注意设置(生成后unity又有bug最好点击字体文件随便改点什么保存一下 再改回来保存 不然下次打开会消失)


有些清空比如"1"会比较窄可以点击font文件自己改一些配置


另比较全面的字体制作: https://blog.csdn.net/qq168213001/article/details/49123123

本工具地址: 链接: https://pan.baidu.com/s/1zqbuLgierewG7erU3LFpgA 密码: dydv

以下是源码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; //unity 插件制作引入库


public class MakeTheFont : ScriptableWizard {
    [Tooltip("字体包含文本")]
    public string FntTxt="0123456789";
    [Tooltip("字体偏移 x ")]
    public float OffsetX = 0;
    [Tooltip("字体偏移 y ")]
    public float OffsetY = 0;
    [Tooltip("字体间距,默认为字体宽度")]
    public int Space = -1;


  
    private int a;
    [MenuItem("Edit-Rain/Make The Font")]//创建界面按钮
    static void CreateWizard() {
        ScriptableWizard.DisplayWizard("Make The Font", typeof(MakeTheFont),"GO"); //创建设置弹框
        Debug.Log("test1");
    }
    private void OnEnable()
    {
        
        Debug.Log("OnEnable");


    }


    private void OnSelectionChange()
    {
        //Debug.Log(" Select "+ Selection.objects[Selection.objects.Length-1].name);
        errorString = "";
        if (Selection.objects.Length > 0) {
            var msg = "select : ";
            foreach (Object o in Selection.objects)
            {
                string name = o.name;
                string path = AssetDatabase.GetAssetPath(o.GetInstanceID());
                //Debug.Log(path.Remove(0, path.IndexOf(name)+name.Length));
                if (path.Remove(0, path.IndexOf(name) + name.Length) != ".png")
                {
                    errorString = "Select file type error, please select PNG";
                }
                else {
                    msg += name+"." ;
                }
            }
            helpString = msg;
        }   
    }
    private void OnWizardCreate()

    {

        //Selection 选中对象

        foreach (Object o in Selection.objects) {

            //取得图片

            string name = o.name;
            string path = AssetDatabase.GetAssetPath(o.GetInstanceID());
            if (path.Remove(0, path.IndexOf(name) + name.Length) != ".png")
            {
                Debug.Log(" Err File is : " + path + ",please select PNG");
                continue;
                //errorString = "Select file type error, please select PNG";
            }
            int index = path.IndexOf(name);
            //Debug.Log(index);
            path = path.Remove(index);
            //Debug.Log(path);

            //创建字体文件
            Font CustomFont = new Font();
            {
                AssetDatabase.CreateAsset(CustomFont, path + "" + name + ".fontsettings");
                AssetDatabase.SaveAssets();
            }

            //设置字体文件
            Texture tex = AssetDatabase.LoadAssetAtPath(path+""+ name+".png", typeof(Texture)) as Texture;


            Debug.Log(tex.width);

            //字体图片要在一行并且等分 没写别的处理

            float width = (float)tex.width/(float)FntTxt.Length;
            float diffX = 1.0f/ (float)FntTxt.Length;
            CharacterInfo[] characterInfo = new CharacterInfo[FntTxt.Length];
            for (int i = 0; i < FntTxt.Length; i++)
            {


                CharacterInfo info = new CharacterInfo();
                info.index = (int)FntTxt[i];
                info.uv.x = diffX*i;
                info.uv.y = 1;
                info.uv.width = diffX;
                info.uv.height = -1;
                info.vert.x = OffsetX;
                info.vert.y = OffsetY;
                info.vert.width = width;
                info.vert.height = tex.height;
                info.advance = Space<=0?(int)width:Space;
                characterInfo[i] = info;
            }
            CustomFont.characterInfo = characterInfo;


            Material mat = null;
            {
                Shader shader = Shader.Find("Transparent/Diffuse");
                mat = new Material(shader);
                mat.SetTexture("_MainTex", tex);
                AssetDatabase.CreateAsset(mat, path + name + ".mat");
                AssetDatabase.SaveAssets();
            }
            CustomFont.material = mat;
        }
      
    }
}

猜你喜欢

转载自blog.csdn.net/Somnus_Rain/article/details/80771898