using System;
using UnityEditor;
using UnityEngine;
public class CreateFont : EditorWindow
{
[MenuItem("创建字体")]
public static void Open()
{
GetWindow<CreateFont>("创建字体");
}
private Texture2D tex;
private string specialCharAscii;
private void OnGUI()
{
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("字体图片:");
tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("Special Char Ascii");
specialCharAscii = EditorGUILayout.TextField(specialCharAscii);
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
if (GUILayout.Button("创建", GUILayout.Height(35)))
{
Create();
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
private void Create()
{
if (tex == null)
{
Debug.LogError("创建失败,图片为空!");
return;
}
string selectionPath = AssetDatabase.GetAssetPath(tex);
string fontPath = selectionPath.Split(new[] { tex.name }, StringSplitOptions.None)[0];
string fontName = tex.name;
string[] specialChar = new string[] { };
if (specialCharAscii != null && specialCharAscii.Length > 0)
specialChar = specialCharAscii.Split(',', (char)StringSplitOptions.RemoveEmptyEntries);
string fontPathName = fontPath + fontName + ".fontsettings";
string matPathName = fontPath + fontName + ".mat";
float lineSpace = 0.1f;
object[] sprites = AssetDatabase.LoadAllAssetsAtPath(selectionPath);
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 - 1];
for (int i = 1; i < sprites.Length; i++)
{
Sprite sprite = sprites[i] as Sprite;
if (sprite.rect.height > lineSpace)
lineSpace = sprite.rect.height;
}
for (int i = 0; i < sprites.Length - 1; i++)
{
Sprite spr = sprites[i + 1] as Sprite;
CharacterInfo info = new CharacterInfo();
if (i < 10) info.index = 48 + i;
else if (specialChar.Length + 10 > i)
info.index = int.Parse(specialChar[i - 10]);
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(rect.x / tex.width, rect.y / tex.height);
info.uvBottomRight = new Vector2((rect.x + rect.width) / tex.width, rect.y / tex.height);
info.uvTopLeft = new Vector2(rect.x / tex.width, (rect.y + rect.height) / tex.height);
info.uvTopRight = new Vector2((rect.x + rect.width) / tex.width, (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, matPathName);
AssetDatabase.CreateAsset(m_myFont, fontPathName);
m_myFont.characterInfo = characterInfo;
EditorUtility.SetDirty(m_myFont);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.LogError("创建字体成功");
}
else
{
Debug.LogError("图集错误!");
}
}
}
需要以下几步就可以搞定啦
- 将图片的textureType改为sprite,spriteMode改为Multiple
- 打开spriteEditor, 把一个Sprite切割成多个
- 在unity的菜单栏找到 创建字体
- 在project界面找到刚才的图片,拖入到工具的 字体图片 栏里
- 如果有特殊符号,可以在工具的 Special Char Ascii 里填上对应的ASCII码,有多个时,可以用逗号,隔开
- 点击创建。OK啦
注意:图片数字要以0开始顺序排哦
|