Unity通过代码实现创建自定义字体

Unity通过代码实现创建自定义字体

在这里插入图片描述

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class MyFont : MonoBehaviour 
{
    [MenuItem("Assets/CreateImageFont")]
    private static void CreateImageFont()
    {
        if (Selection.objects.Length <= 0)
        {
            Debug.LogError("未选择图片!");
            return;
        }
        Object[] objects = Selection.objects;
        for (int i = 0; i < objects.Length; i++)
        {
            if (objects[i].GetType() == typeof(Texture2D))
            {
                CreateFont(objects[i] as Texture2D);
            }
        }
        //AssetDatabase.Refresh();
        Debug.Log("创建字体成功!");
    }

    public static void CreateFont(Texture2D texture2D)
    {
        string texturePath = AssetDatabase.GetAssetPath(texture2D);
        string extension = Path.GetExtension(texturePath);
        string createPath = texturePath.Remove(texturePath.Length-extension.Length);
        string fontPath = createPath + ".fontsettings";
        Font font = AssetDatabase.LoadAssetAtPath<Font>(fontPath);
        if (font == null)
        {
            font = new Font();
            Material material = new Material(Shader.Find("GUI/Text Shader"));
            material.mainTexture = texture2D;
            string matPath = createPath + ".mat";
            AssetDatabase.CreateAsset(material, matPath);
            font.material = material;
            AssetDatabase.CreateAsset(font, fontPath);
        }
        Sprite[] sprites = GetAllSelectSprite(texturePath);
        CharacterInfo[] characterInfos = new CharacterInfo[sprites.Length];
        for (int i = 0; i < characterInfos.Length; i++)
        {
            characterInfos[i] = new CharacterInfo();

            Rect rect = sprites[i].rect;

            characterInfos[i].index = sprites[i].name[sprites[i].name.Length-1];

            characterInfos[i].uvBottomLeft = new Vector2(rect.x/texture2D.width,rect.y/texture2D.height);
            characterInfos[i].uvBottomRight = new Vector2((rect.x+rect.width)/texture2D.width,rect.y/texture2D.height);
            characterInfos[i].uvTopLeft = new Vector2(rect.x/texture2D.width,(rect.y+rect.height)/texture2D.height);
            characterInfos[i].uvTopRight = new Vector2((rect.x+rect.width)/texture2D.width,(rect.y+rect.height)/texture2D.height);

            characterInfos[i].minX = 0;
            characterInfos[i].maxX = (int)rect.width;
            characterInfos[i].minY = 0 - (int)sprites[i].pivot.y;
            characterInfos[i].maxY = (int)rect.height - (int)sprites[i].pivot.y;

            characterInfos[i].advance = (int)rect.width;
        }
        font.characterInfo = characterInfos;

        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
        EditorUtility.SetDirty(font);
    }
    private static Sprite[] GetAllSelectSprite(string path)
    {
        List<Sprite> sprites = new List<Sprite>();
        UnityEngine.Object[] objects = AssetDatabase.LoadAllAssetsAtPath(path);
        if (objects.Length <= 0)
        {
            Debug.LogError("请切割图片!");
        }
        for (int i = 0; i < objects.Length; i++)
        {
            if (objects[i].GetType() == typeof(Sprite))
            {
                sprites.Add(objects[i] as Sprite);
            }
        }
        return sprites.ToArray();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值