using System.Linq; using UnityEngine; using UnityEditor; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; namespace BFEditor.Resource { // 创建bmfont // 这里是给战斗用的美术数字,多个fontsetting文件公用一个png文件 public class CreateBattleBMFontEditorNew : Editor { static void TryGenerateFontFile(Dictionary fontMap, Dictionary> infoMap, string folerPath, string fontName) { Font font; if(fontMap.TryGetValue(fontName, out font)) { return; } string customFontPath = folerPath + fontName + ".fontsettings"; Font customFont = null; if (!File.Exists(customFontPath)) { customFont = new Font(); AssetDatabase.CreateAsset(customFont, customFontPath); AssetDatabase.SaveAssets(); }else{ customFont = AssetDatabase.LoadAssetAtPath(customFontPath); } fontMap.Add(fontName, customFont); infoMap.Add(fontName, new List()); } [MenuItem("Assets/BF Tools/BMFont/CreateBMFontSettingsNew", false, 2)] static void CreateFontNew() { Object obj = Selection.activeObject; string fntPath = AssetDatabase.GetAssetPath(obj).Replace("\\", "/"); if (fntPath.IndexOf(".fnt") == -1) { // 不是字体文件 return; } string jsonPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(obj)) + "/battleBMFontConfigList.bbcl"; if (!File.Exists(jsonPath)) { // 没有json配置映射 BF.BFLog.LogError("没有映射配置"); return; } BFEditor.BattleBMFontConfigList battleBMFontConfigList = ParseBattleBMFontConfigMap.ToObject(File.ReadAllText(jsonPath)); Dictionary> infoMap = new Dictionary>(); Dictionary fontMap = new Dictionary(); var index = fntPath.LastIndexOf("/"); string folerPath = fntPath.Substring(0, index + 1); StreamReader reader = new StreamReader(new FileStream(fntPath, FileMode.Open)); Regex reg = new Regex(@"char id=(?\d+)\s+x=(?\d+)\s+y=(?\d+)\s+width=(?\d+)\s+height=(?\d+)\s+xoffset=(?\d+)\s+yoffset=(?\d+)\s+xadvance=(?\d+)\s+"); string line = reader.ReadLine(); int lineHeight = 0; int texWidth = 1; int texHeight = 1; while (line != null) { line = line.Replace("\"", ""); if (line.IndexOf("char id=") != -1) { Match match = reg.Match(line); if (match != Match.Empty) { int idIndex = System.Convert.ToInt32(match.Groups["id"].Value); List> infoList = battleBMFontConfigList.fileInfoList[idIndex]; for(int i = 0; i < infoList.Count; i++) { int unicodeID; string fontName; Dictionary infoDic = infoList[i]; foreach (int id in infoDic.Keys) { unicodeID = id; fontName = infoDic[id]; TryGenerateFontFile(fontMap, infoMap, folerPath, fontName); var x = System.Convert.ToInt32(match.Groups["x"].Value); var y = System.Convert.ToInt32(match.Groups["y"].Value); var width = System.Convert.ToInt32(match.Groups["width"].Value); var height = System.Convert.ToInt32(match.Groups["height"].Value); var xoffset = System.Convert.ToInt32(match.Groups["xoffset"].Value); var yoffset = System.Convert.ToInt32(match.Groups["yoffset"].Value); var xadvance = System.Convert.ToInt32(match.Groups["xadvance"].Value); int charIndex = unicodeID; var info = new CharacterInfo(); info.index = charIndex; float uvx = 1.0f*x/texWidth; float uvy = 1.0f - (1.0f*(y + height)/texHeight); float uvw = 1.0f*width/texWidth; float uvh = 1.0f*height/texHeight; info.uvBottomLeft = new Vector2(uvx, uvy); info.uvBottomRight = new Vector2(uvx + uvw, uvy); info.uvTopLeft = new Vector2(uvx, uvy + uvh); info.uvTopRight = new Vector2(uvx + uvw, uvy + uvh); info.minX = 0; info.maxX = (int)width; info.minY = -(int)height / 2; info.maxY = (int)height / 2; info.advance = (int)xadvance; var charList = infoMap[fontName]; charList.Add(info); break; } } } } else if (line.IndexOf("scaleW=") != -1) { Regex reg2 = new Regex(@"common lineHeight=(?\d+)\s+.*scaleW=(?\d+)\s+scaleH=(?\d+)"); Match match = reg2.Match(line); if (match != Match.Empty) { lineHeight = System.Convert.ToInt32(match.Groups["lineHeight"].Value); texWidth = System.Convert.ToInt32(match.Groups["scaleW"].Value); texHeight = System.Convert.ToInt32(match.Groups["scaleH"].Value); } } line = reader.ReadLine(); } string materialFilename = fntPath.Replace(".fnt", ".mat"); string textureFilename = fntPath.Replace(".fnt", ".png"); Material mat = null; if (File.Exists(materialFilename)) { mat = AssetDatabase.LoadAssetAtPath(materialFilename, typeof(Material)) as Material; Shader shader = Shader.Find("UI/Default Font"); mat.shader = shader; Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; mat.SetTexture("_MainTex", tex); EditorUtility.SetDirty(mat); }else{ { Shader shader = Shader.Find("UI/Default Font"); mat = new Material(shader); Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; mat.SetTexture("_MainTex", tex); Debug.Log(fntPath.Replace(".fnt", ".mat")); AssetDatabase.CreateAsset(mat, fntPath.Replace(".fnt", ".mat")); EditorUtility.SetDirty(mat); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } foreach (KeyValuePair item in fontMap) { var key = item.Key; var val = item.Value; val.characterInfo = infoMap[key].ToArray(); val.material = mat; EditorUtility.SetDirty(val); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); // 设置图片格式 TextureImporter textureImporter = AssetImporter.GetAtPath(textureFilename) as TextureImporter; textureImporter.alphaIsTransparency = true; var settingsAndorid = new TextureImporterPlatformSettings(); settingsAndorid.overridden = true; settingsAndorid.name = "Android"; settingsAndorid.textureCompression = TextureImporterCompression.Uncompressed; settingsAndorid.maxTextureSize = 2048; settingsAndorid.format = TextureImporterFormat.ETC2_RGBA8; textureImporter.SetPlatformTextureSettings(settingsAndorid); var settingsIOS = new TextureImporterPlatformSettings(); settingsIOS.overridden = true; settingsIOS.name = "iPhone"; settingsIOS.textureCompression = TextureImporterCompression.Uncompressed; settingsIOS.maxTextureSize = 2048; settingsIOS.format = TextureImporterFormat.ASTC_6x6; textureImporter.SetPlatformTextureSettings(settingsIOS); Debug.Log("CreateFont Success"); AssetDatabase.ImportAsset(textureFilename); } } }