来自于:itsxwz
通过导入一张图片到Unity,把图片上的字符分割,再通过脚本转换为适用的字体文件。(精灵格式的图片)

1.图片处理

图片:
image.png
处理:
image.png

2.编辑器扩展代码

  1. /*
  2. *R0-V1.0
  3. *Modify Date:2018-11-29
  4. *Modifier:ZoJet
  5. *Modify Reason:根据图片生成包含对应字符的字体文件
  6. *Modify Content:
  7. */
  8. using System.IO;
  9. using UnityEditor;
  10. using UnityEngine;
  11. public class CreateFontBySprite : MonoBehaviour {
  12. [MenuItem("Tools/CreateFont")]
  13. private static void CreateFont() {
  14. if (Selection.objects == null || Selection.objects.Length == 0) {
  15. Debug.Log("No selected object or sharding atlas");
  16. return;
  17. }
  18. Object o = Selection.objects[0];
  19. if (o.GetType() != typeof(Texture2D)) {
  20. Debug.Log("The selected file is not a picture");
  21. return;
  22. }
  23. string selectionPath = AssetDatabase.GetAssetPath(o);
  24. if (selectionPath.Contains("Resources")) {
  25. string selectionExt = Path.GetExtension(selectionPath);
  26. if (selectionExt.Length == 0) {
  27. return;
  28. }
  29. string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);
  30. string fontPathName = loadPath + ".fontsettings";
  31. string matPathName = loadPath + ".mat";
  32. float lineSpace = 0.1f;
  33. loadPath = Path.GetFileNameWithoutExtension(selectionPath);
  34. Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
  35. if (sprites.Length > 0) {
  36. Texture2D tex = o as Texture2D;
  37. Material mat = new Material(Shader.Find("GUI/Text Shader"));
  38. AssetDatabase.CreateAsset(mat, matPathName);
  39. mat.SetTexture("_MainTex", tex);
  40. Font font = new Font();
  41. font.material = mat;
  42. AssetDatabase.CreateAsset(font, fontPathName);
  43. CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
  44. for (int i = 0; i < sprites.Length; i++) {
  45. if (sprites[i].rect.height > lineSpace) {
  46. lineSpace = sprites[i].rect.height;
  47. }
  48. }
  49. for (int i = 0; i < sprites.Length; i++) {
  50. Sprite spr = sprites[i];
  51. CharacterInfo info = new CharacterInfo();
  52. info.index = (int)spr.name[spr.name.Length - 1];
  53. Rect rect = spr.rect;
  54. float pivot = spr.pivot.y / rect.height - 0.5f;
  55. if (pivot > 0) {
  56. pivot = -lineSpace / 2 - spr.pivot.y;
  57. } else if (pivot < 0) {
  58. pivot = -lineSpace / 2 + rect.height - spr.pivot.y;
  59. } else {
  60. pivot = -lineSpace / 2;
  61. }
  62. int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
  63. //设置字符映射到材质上的坐标
  64. info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);
  65. info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);
  66. info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);
  67. info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y + rect.height) / tex.height);
  68. //设置字符顶点的偏移位置和宽高
  69. info.minX = 0;
  70. info.minY = -(int)rect.height - offsetY;
  71. info.maxX = (int)rect.width;
  72. info.maxY = -offsetY;
  73. //设置字符的宽度
  74. info.advance = (int)rect.width;
  75. characterInfo[i] = info;
  76. }
  77. font.characterInfo = characterInfo;
  78. EditorUtility.SetDirty(font);
  79. AssetDatabase.SaveAssets();
  80. AssetDatabase.Refresh();
  81. Debug.Log("Max Height:" + lineSpace + " Prefect Height:" + (lineSpace + 2));
  82. } else {
  83. Debug.Log("Sprite must be placed in the Resources folder and selected");
  84. }
  85. }
  86. }
  87. }

3.效果

image.png

4.说明

1.把图片切割为每一最小单元都是一个字符
2.字符对应其ASCII码值
3.选中处理后sprite,转换为字体文件
image.png
image.png