参考文章
详细步骤说明
- 新的字体图片BattleTextFont.png,放置于Resources目录下,如图
- 选中图片,将Sprite Mode设置为Multiple
- 点击Sprite Editor 将各个字切割好
- 创建工具脚本CostomFont.cs,自动生成字体文件,右键菜单会出现CreateMyFontSprite选项
using UnityEngine;
using UnityEditor;
using System.IO;
public class CustomFont : MonoBehaviour
{
[MenuItem("Assets/CreateMyFontSprite")]
static void CreateMyFontSprite()
{
Debug.LogWarning("abc");
if (Selection.objects == null)
{
Debug.LogWarning("没有选中Sprite文件,需要将Sprite Mode设置成Multiple,切分好,并且以以名字的最后一个字符当做ascii码111");
return;
}
if (Selection.objects.Length == 0)
{
Debug.LogWarning("没有选中Sprite文件,需要将Sprite Mode设置成Multiple,切分好,并且以以名字的最后一个字符当做ascii码");
return;
}
string resoursePath = "Resources";
UnityEngine.Object o = Selection.objects[0];
if (o.GetType() != typeof(Texture2D))
{
Debug.LogWarning("选中的并不是图片文件");
return;
}
string selectionPath = AssetDatabase.GetAssetPath(o);
Debug.Log("selectionPath=" + selectionPath);
{
string selectionExt = Path.GetExtension(selectionPath);
if (selectionExt.Length == 0)
{
Debug.LogWarning("选中的扩展名是为空");
return;
}
string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);
string fontPathName = loadPath + ".fontsettings";
string matPathName = loadPath + ".mat";
float lineSpace = 0.1f;
loadPath = Path.GetFileNameWithoutExtension(selectionPath);
Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
Debug.Log("loadPath=" + loadPath + ",size=" + sprites.Length);
if (sprites.Length > 0)
{
Texture2D tex = o as Texture2D;
Material mat = new Material(Shader.Find("GUI/Text Shader"));
AssetDatabase.CreateAsset(mat, matPathName);
mat.SetTexture("_MainTex", tex);
Font m_myFont = new Font();
m_myFont.material = mat;
AssetDatabase.CreateAsset(m_myFont, fontPathName);
CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
bool[] flags = new bool[sprites.Length];
for (int i = 0; i < sprites.Length; i++)
{
if (sprites[i].rect.height > lineSpace)
{
lineSpace = sprites[i].rect.height;
}
}
int realIndex = 0;
for (int i = 0; i < sprites.Length; i++)
{
Sprite spr = sprites[i];
if (!spr.name.StartsWith("font_"))
{
Debug.Log("skip sprite " + spr.name + ", the name doesn't starts with font_");
flags[i] = false;
}
else
{
flags[i] = true;
realIndex++;
}
CharacterInfo info = new CharacterInfo();
info.index = (int)spr.name[spr.name.Length - 1];
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;
}
Debug.Log(pivot);
int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);
info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);
info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);
info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(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;
}
if (realIndex <= 0)
{
Debug.Log("no valid sprite");
return;
}
Debug.Log("font sprites size = " + realIndex);
var temp = new CharacterInfo[realIndex];
for (int i = 0, temp_index= 0; temp_index < realIndex && i < characterInfo.Length; ++i)
{
if (flags[i])
{
temp[temp_index++] = characterInfo[i];
}
}
m_myFont.characterInfo = temp;
EditorUtility.SetDirty(m_myFont);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
AssetDatabase.ExportPackage(fontPathName, "temp.unitypackage");
AssetDatabase.DeleteAsset(fontPathName);
AssetDatabase.ImportPackage("temp.unitypackage", true);
AssetDatabase.Refresh();
Debug.Log("创建字体成功, 最大高度:" + lineSpace + ", 最佳高度:" + (lineSpace + 2));
}
else
{
Debug.LogWarning("没有选中Sprite文件,需要将Sprite放到Resources文件夹下面,可以参考函数上方的说明操作");
}
}
}
}
- 选中BattleTextFont.png图片,点击CreateMyFontSprite,将会自动生成字体文件BattleTextFont.mat 和 BattleTextFont.fontsettings
- 测试:新建UI -> Text,字体选为BattleTextFont
- 最终效果
字符间距调整技巧
- 可以修改fontsettings中Character Rects里面元素中的Vert->W和Advance均能达到效果
- 还可以修改fontsettings里的Tracking
其他
- 修改UI text的四种方式
- Unity/UI —— 使用字符图片自定义字体(Custom Font)
- Custom Font不足
- 无法定义中文字体。这点是由于Custom Font使用Ascii字符集导致的,Ascii字符集并不包含汉字。
- 无法通过Size改变字体大小。 就像上一节的演示图一样,数字“1”显然比Text文本框的范围还要高,这是因为Custom Font本身无法改变字符大小,不过依然可以通过Scale来变相调节
- 无法得到理想的字体颜色。 当改变Color,会在原图片的基础上变化,所以为了得到最好的显示效果,最好保证 字体颜色是白色,背景为透明。