【Unity UGUI】簡單的美術字體的製作(教你寫外掛)
在 unity UGUI 使用中我們常常用到美術字體,然而有時卻沒有那麼複雜那麼多,再此介紹下生成美術字體的原理
選中預先製作好的圖片
點選Go 就可以生成一個簡單的字型了
使用也很簡單
要注意設定(生成後unity又有bug最好點選字型檔案隨便改點什麼儲存一下 再改回來儲存 不然下次開啟會消失)
有些清空比如"1"會比較窄可以點選font檔案自己改一些配置
本工具地址: 連結: 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 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;
}
}
}