Unity用脚本配置简单的数字艺术字体CustomFont

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AnYuanLzh/article/details/83542236

unity 2017.4.3f1
1、代码

/***************************************************
 * 文件名: FontCustom.cs
 * 时  间: 2018-10-30
 * 作  者: AnyuanLzh
 * 描  述: 
 ***************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class FontCustom
{
    static string fontPath = "Assets/AllRes/assetbundle/games/common/fonts/digit1.fontsettings";
    static string targetString = "0123456789x+.-";
    static int texWidth = 336;
    static int texHeight = 36;

    static int padding = -2;

    [MenuItem("Tools/Lzh/生成数字字体")]
    public static void GetAllFileNameInOneDir()
    {
        //Debug.LogError("使用前请在代码中设置好配置:" + fontPath);
        //return;


        Debug.Log("字体文件不存在:" + fontPath);
        Font font = AssetDatabase.LoadAssetAtPath<Font>(fontPath);
        Debug.Log(font.name);
        if (font == null)
        {
            Debug.LogError("字体文件不存在:"+ fontPath);
            return;
        }

        int num = targetString.Length;
        int fontWidth = texWidth / num;
        int fontHeight = texHeight;
        float uvWidth = 1f / num;
        List<CharacterInfo> charInfoList = new List<CharacterInfo>();
        for(int i=0; i<num; i++)
        {
            CharacterInfo charInfo = new CharacterInfo();
            charInfo.index = (int)targetString[i];
            charInfo.uvBottomLeft= new Vector2(uvWidth * i, 0);
            charInfo.uvBottomRight = new Vector2(uvWidth * i + uvWidth, 0);
            charInfo.uvTopLeft = new Vector2(uvWidth * i, 1);
            charInfo.uvTopRight = new Vector2(uvWidth * i + uvWidth, 1);
            charInfo.minX = 0;
            charInfo.maxX = fontWidth - 0;
            charInfo.minY = 0;
            charInfo.maxY = fontHeight;
            charInfo.advance = fontWidth  + padding;

            charInfoList.Add(charInfo);
        }
        font.characterInfo = charInfoList.ToArray();
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

}

2、效果
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AnYuanLzh/article/details/83542236