Unity自定义字体的实现

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

实现的效果如下:

最后成功的同志们,要注意看回来这里,Color要设置为白色的,不然原来的字体颜色会受到这个影响,如果看不到字体,请把字体UI拉大一点就能看到了。

准备一个图集,没有的话,可以去百度找找,然后设置为如下的图就是图集,九宫格切割也不会的话 百度下!

首先将美术给的图集切割好,尽量保持切割的高度和宽度一样,然后创建一个Custom Font,接着 如下所示的设置参数:

注意:Line Spacing 设置其中字体的最大高度, 可自我调整与换行有关。

Default Material 材质如下:Font Texture就是美术给的字体图集,Shader选GUI/Text Shader

 剩下的就是设置Character Rects

其中Size 是字体总数,我这里只有10个所以为10.

Element x 表示字体元素,必须按照如下说明设置,不然会字体会很凌乱

Index ASCII码 例如:上图的第一个元素0,Index是48  代表0的ASCII码。

Uv 的设置 方法:

参考上图,Uv 的X就是 那个Position的X  除以 图集宽度

也就是0/256, Y同理,188/256 这个计算可以直接在Unity编辑器上面打188/256 然后按回车就会计算出来了所以不是很麻烦。

剩下W,H也是一样的。

Vert 的X,Y 直接都填0 , W 填 字体的宽度上图就是53, H填 字体的高度的负数 -67

Advance 直接填 字体宽度 53

还需要填写其他9个,道理一样,就不废话了,我也不会 做过多说明,为什么这样搞,因为我就是这样成功了,拿来用就够了,有一些编辑器插件可以快速地动态生成字体,就只需要一个图集,然后直接帮你 自动化生成,不过这样也是有些许问题的。。

 参考别人的东西修改的一个自动化生成自定义字体 编辑器代码 如下:

using UnityEngine;
using UnityEditor;
using System.IO;

/// <summary>
/// 注意:1、根据已切割好的图集自动生成的Custom Text要手动调整lineSpacing已达到分行效果(自测)
/// 2、图集必须放于Resouces文件夹下
/// 3、图集切割出来的小图片的名称一定是要写上对应数字的ASCII码,例如:图片"0"  名称就是48,1 就是 49  直至9 是 57,中文的话 ///没有ASCII码故无法使用这种方法来自动化生成,不过有插件
/// 使用方法:拖拽图集、写上生成的字体名字、选择保存路径,点击创建
/// 测试方法:创建Text,将生成的font拖到font属性,打0~9数字,然后调整lineSpacing,每次调整完后需清空,再次打数字
/// </summary>
public class CustomText : EditorWindow
{
	[MenuItem("Tools/创建字体(sprite)")]
	public static void Open()
	{
		GetWindow<CustomText>("创建字体");
	}

	private Texture2D tex;
	private string fontName;
	private string fontPath;

	private void OnGUI()
	{
		GUILayout.BeginVertical();

		GUILayout.BeginHorizontal();
		GUILayout.Label("字体图片:");
		tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);
		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();
		GUILayout.Label("字体名称:");
		fontName = EditorGUILayout.TextField(fontName);
		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();
		if (GUILayout.Button(string.IsNullOrEmpty(fontPath) ? "选择路径" : fontPath))
		{
			fontPath = EditorUtility.OpenFolderPanel("字体路径", Application.dataPath, "");
			if (string.IsNullOrEmpty(fontPath))
			{
				Debug.Log("取消选择路径");
			}
			else
			{
				fontPath = fontPath.Replace(Application.dataPath, "") + "/";
			}
		}
		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();
		if (GUILayout.Button("创建"))
		{	
			Create();
		}
		GUILayout.EndHorizontal();

		GUILayout.EndVertical();
	}

	private void Create()
	{
		if (tex == null)
		{
			Debug.LogWarning("创建失败,图片为空!");
			return;
		}

		if (string.IsNullOrEmpty(fontPath))
		{
			Debug.LogWarning("字体路径为空!");
			return;
		}
		if (fontName == null)
		{
			Debug.LogWarning("创建失败,字体名称为空!");
			return;
		}
		else
		{
			if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings"))
			{
				Debug.LogError("创建失败,已存在同名字体文件");
				return;
			}
			if (File.Exists(Application.dataPath + fontPath + fontName + ".mat"))
			{
				Debug.LogError("创建失败,已存在同名字体材质文件");
				return;
			}
		}

		string selectionPath = AssetDatabase.GetAssetPath(tex);
		Debug.Log(selectionPath);
		if (selectionPath.Contains("/Resources/"))
		{
			string selectionExt = Path.GetExtension(selectionPath);
			Debug.Log(selectionExt);
			if (selectionExt.Length == 0)
			{
				Debug.LogError("创建失败!");
				return;
			}

			string fontPathName = fontPath + fontName + ".fontsettings";
			string matPathName = fontPath + fontName + ".mat";
			float lineSpace = 0.1f;
			//string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length).Replace("Assets/Resources/", "");
			string loadPath = selectionPath.Replace(selectionExt, "").Substring(selectionPath.IndexOf("/Resources/") + "/Resources/".Length);
			Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
			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];
				for (int i = 0; i < sprites.Length; i++)
				{
					if (sprites[i].rect.height > lineSpace)
					{
						lineSpace = sprites[i].rect.height;
					}
				}
				for (int i = 0; i < sprites.Length; i++)
				{
					Sprite spr = sprites[i];
					CharacterInfo info = new CharacterInfo();
					try
					{
						info.index = System.Convert.ToInt32(spr.name);
					}
					catch
					{
						Debug.LogError("创建失败,Sprite名称错误!");
						return;
					}
					Rect rect = spr.rect;
					float pivot = -lineSpace / 2 - 0.5f * rect.height;
					int offsetY = (int)(pivot + (lineSpace - rect.height) / 2); //- 38
					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 = offsetY;
					info.maxX = (int)rect.width;
					info.maxY = (int)rect.height + offsetY;
					info.advance = (int)rect.width;
					characterInfo[i] = info;
				}
				AssetDatabase.CreateAsset(mat, "Assets" + matPathName);
				AssetDatabase.CreateAsset(m_myFont, "Assets" + fontPathName);
				m_myFont.characterInfo = characterInfo;
				EditorUtility.SetDirty(m_myFont);
				AssetDatabase.SaveAssets();
				AssetDatabase.Refresh();//刷新资源
				Debug.Log("创建字体成功");

			}
			else
			{
				Debug.LogError("图集错误!");
			}
		}
		else
		{
			Debug.LogError("创建失败,选择的图片不在Resources文件夹内!");
		}
	}
}

参考文章:https://blog.csdn.net/u012662020/article/details/79996686

太难懂了我就这样改了下,希望比参考文章能容易理解点呵呵

猜你喜欢

转载自blog.csdn.net/qq_39574690/article/details/83574729