一键生成UGUI艺术字体






需要注意的是:字体文件的名字要对应相应的艺术字体,比如 1->1的艺术字体,中->对应中的艺术字体等等。

并且在生成字体文件后,要修改一下字体,然后再回去保存,才能让字体文件生效


[html]  view plain  copy
  1. using UnityEditor;  
  2. using UnityEngine;  
  3. using System.Collections;  
  4. using System.Collections.Generic;  
  5. using System.IO;  
  6. using System;  
  7. public class MakeArtFont  
  8. {  
  9.   
  10.     [MenuItem("Assets/LoadFont(选中目标文件夹)")]  
  11.     static void CarteNewFont()  
  12.     {  
  13.   
  14.         UnityEngine.Object obj = Selection.activeObject;  
  15.         string setImgPath = AssetDatabase.GetAssetPath(obj);  
  16.         CombinImage(setImgPath.Replace("Assets/", ""), obj.name);  
  17.   
  18.     }  
  19.   
  20.     /// <summary>  
  21.     /// 调用此函数后使此图片合并  
  22.     /// </summary>  
  23.     /// <param name="sourceImg">粘贴的源图片</param>  
  24.     /// <param name="destImg">粘贴的目标图片</param>  
  25.     public static void CombinImage(string sourceImg, string fileName)  
  26.     {  
  27.         string[] pngs = Directory.GetFiles(Application.dataPath + "/" + sourceImg, "*.png");  
  28.   
  29.         string texturePath = Application.dataPath + "/" + sourceImg + "/" + fileName + "_texture.png";  
  30.   
  31.         int maxWidth = 0;  
  32.         int maxHeight = 0;  
  33.   
  34.         foreach (string path in pngs)  
  35.         {  
  36.             string subPngPath = path.Replace(Application.dataPath, "Assets");  
  37.   
  38.             if (subPngPath.IndexOf("texture") == -1)  
  39.             {  
  40.                 Texture tex2D = AssetDatabase.LoadAssetAtPath(subPngPath, typeof(Texture)) as Texture;  
  41.                 maxWidth += tex2D.width;  
  42.                 if (maxHeight < tex2D.height)  
  43.                 {  
  44.                     maxHeight = tex2D.height;  
  45.                 }  
  46.             }  
  47.         }  
  48.   
  49.         Texture2D fullTexture = new Texture2D(maxWidth, maxHeight, TextureFormat.RGBA32, false);  
  50.   
  51.         Color[] fullTcolors = fullTexture.GetPixels();  
  52.   
  53.         for (int i = 0; i < fullTcolors.Length; i++)  
  54.         {  
  55.             fullTcolors[i].a = 0;  
  56.             fullTcolors[i].r = 0;  
  57.             fullTcolors[i].g = 0;  
  58.             fullTcolors[i].b = 0;  
  59.   
  60.         }  
  61.         fullTexture.SetPixels(fullTcolors);  
  62.   
  63.         int posX = 0;  
  64.   
  65.   
  66.         List<BmInfo> bmInfoList = new List<BmInfo>();  
  67.   
  68.         foreach (string path in pngs)  
  69.         {  
  70.             string subPngPath = path.Replace(Application.dataPath, "Assets");  
  71.   
  72.             if (subPngPath.IndexOf("texture") == -1)  
  73.             {  
  74.   
  75.                 SetTextureImporter(subPngPath);  
  76.   
  77.                 Texture2D tex2D = AssetDatabase.LoadAssetAtPath(subPngPath, typeof(Texture2D)) as Texture2D;  
  78.                 Color[] colors = tex2D.GetPixels();  
  79.                 fullTexture.SetPixels(posX, 0, tex2D.width, tex2D.height, colors);  
  80.   
  81.                 BmInfo bmInfo = new BmInfo();  
  82.   
  83.                 bmInfo.index = Uncode(tex2D.name);  
  84.                 bmInfo.width = tex2D.width;  
  85.                 bmInfo.height = tex2D.height;  
  86.                 bmInfo.x = posX;  
  87.                 bmInfo.y = 0;  
  88.   
  89.                 bmInfoList.Add(bmInfo);  
  90.   
  91.                 posX += tex2D.width;  
  92.   
  93.             }  
  94.   
  95.         }  
  96.   
  97.         byte[] full = fullTexture.EncodeToPNG();  
  98.         File.WriteAllBytes(texturePath, full);  
  99.   
  100.         AssetDatabase.Refresh();  
  101.   
  102.         texturePath = texturePath.Replace(Application.dataPath, "Assets");  
  103.   
  104.         SetTextureImporter(texturePath);  
  105.   
  106.         Texture texture = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture)) as Texture;  
  107.         CreateBitmapFont(fileName, texture, bmInfoList, maxWidth, maxHeight);  
  108.   
  109.   
  110.     }  
  111.   
  112.     public static void SetTextureImporter(string subPngPath)  
  113.     {  
  114.         TextureImporter ti = AssetImporter.GetAtPath(subPngPath) as TextureImporter;  
  115.         ti.textureType = TextureImporterType.Sprite;  
  116.         ti.mipmapEnabled = false;  
  117.         ti.isReadable = true;  
  118.         ti.filterMode = FilterMode.Trilinear;  
  119.         ti.textureFormat = TextureImporterFormat.AutomaticTruecolor;  
  120.         AssetDatabase.ImportAsset(subPngPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);  
  121.     }  
  122.   
  123.   
  124.     /// <summary>  
  125.     /// 创建新的美术字体  
  126.     /// </summary>  
  127.     /// <param name="texture"></param>  
  128.     /// <param name="bmInfoList"></param>  
  129.     /// <param name="columnNum"></param>  
  130.     /// <param name="lineNum"></param>  
  131.     public static void CreateBitmapFont(string fileName, Texture texture, List<BmInfo> bmInfoList, int maxWidth, int maxHeight)  
  132.     {  
  133.   
  134.         string assetPath = GetAssetPath(AssetDatabase.GetAssetPath(texture));  
  135.         string fontPath = assetPath + "/" + fileName + "_1.fontsettings";  
  136.         string materialPath = assetPath + "/" + fileName + "_2.mat";  
  137.   
  138.   
  139.         Material fontMaterial = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;//new Material(Shader.Find("UI/Default"));  
  140.         if (fontMaterial == null)  
  141.         {  
  142.             fontMaterial = new Material(Shader.Find("UI/Default"));  
  143.             fontMaterial.mainTexture = texture;  
  144.             AssetDatabase.CreateAsset(fontMaterial, materialPath);  
  145.         }  
  146.         else  
  147.         {  
  148.             fontMaterial.mainTexture = texture;  
  149.         }  
  150.   
  151.         Font assetFont = AssetDatabase.LoadAssetAtPath(fontPath, typeof(Font)) as Font;  
  152.   
  153.         string ApplicationDataPath = Application.dataPath.Replace("Assets", "") + fontPath;  
  154.   
  155.         if (assetFont == null)  
  156.         {  
  157.             assetFont = new Font();  
  158.             AssetDatabase.CreateAsset(assetFont, fontPath);  
  159.         }  
  160.   
  161.         assetFont.material = fontMaterial;  
  162.         CharacterInfo[] characters = new CharacterInfo[bmInfoList.Count];  
  163.         for (int i = 0; i < bmInfoList.Count; i++)  
  164.         {  
  165.   
  166.             BmInfo bmInfo = bmInfoList[i];  
  167.   
  168.             CharacterInfo info = new CharacterInfo();  
  169.             info.index = bmInfo.index;  
  170.             info.uv.width = (float)bmInfo.width / (float)maxWidth;  
  171.             info.uv.height = (float)bmInfo.height / (float)maxHeight;  
  172.             info.uv.x = (float)bmInfo.x / (float)maxWidth;  
  173.             info.uv.y = (1 - (float)bmInfo.y / (float)maxHeight) - info.uv.height;  
  174.   
  175.   
  176.             info.vert.x = 0;  
  177.             info.vert.y = 0;  
  178.             info.vert.width = (float)bmInfo.width;  
  179.             info.vert.height = -(float)bmInfo.height;  
  180.             info.width = (float)bmInfo.width;  
  181.   
  182.             characters[i] = info;  
  183.   
  184.         }  
  185.   
  186.         assetFont.characterInfo = characters;  
  187.   
  188.   
  189.   
  190.     }  
  191.     private static string GetAssetPath(string path)  
  192.     {  
  193.         return path == "" || path == null ? "Assets/" : path.Substring(0, path.LastIndexOf("/"));  
  194.     }  
  195.   
  196.     //将中文字符转为10进制整数  
  197.     public static int Uncode(string str)  
  198.     {  
  199.         int outStr = 0;  
  200.         if (!string.IsNullOrEmpty(str))  
  201.         {  
  202.   
  203.             if (str == "。")  
  204.             {  
  205.                 outStr = (int)"."[0];  
  206.             }  
  207.             else  
  208.             {  
  209.                 outStr = ((int)str[0]);  
  210.             }  
  211.         }  
  212.   
  213.         return outStr;  
  214.     }  
  215. }  
  216.   
  217. public class BmInfo  
  218. {  
  219.     public int index = 0;  
  220.     public float width = 0;  
  221.     public float height = 0;  
  222.     public float x = 0;  
  223.     public float y = 0;  
  224. }  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值