121 lines
5.2 KiB
C#
121 lines
5.2 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace BFEditor.Resource
|
|
{
|
|
// 创建bmfont
|
|
public class CreateFontEditor : Editor
|
|
{
|
|
[MenuItem("Assets/BF Tools/BMFont/CreateBMFontSettings", false, 1)]
|
|
static void CreateFont()
|
|
{
|
|
Object obj = Selection.activeObject;
|
|
string fntPath = AssetDatabase.GetAssetPath(obj);
|
|
if (fntPath.IndexOf(".fnt") == -1) {
|
|
// 不是字体文件
|
|
return;
|
|
}
|
|
|
|
string customFontPath = fntPath.Replace(".fnt", ".fontsettings");
|
|
Font customFont = null;
|
|
if (!File.Exists(customFontPath)) {
|
|
customFont = new Font();
|
|
AssetDatabase.CreateAsset(customFont, customFontPath);
|
|
AssetDatabase.SaveAssets();
|
|
}else{
|
|
customFont = AssetDatabase.LoadAssetAtPath<Font>(customFontPath);
|
|
}
|
|
|
|
StreamReader reader = new StreamReader(new FileStream(fntPath, FileMode.Open));
|
|
|
|
List<CharacterInfo> charList = new List<CharacterInfo>();
|
|
Regex reg = new Regex(@"char id=(?<id>\d+)\s+x=(?<x>\d+)\s+y=(?<y>\d+)\s+width=(?<width>\d+)\s+height=(?<height>\d+)\s+xoffset=(?<xoffset>\d+)\s+yoffset=(?<yoffset>\d+)\s+xadvance=(?<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) {
|
|
var id = System.Convert.ToInt32(match.Groups["id"].Value);
|
|
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);
|
|
CharacterInfo info = new CharacterInfo();
|
|
info.index = id;
|
|
|
|
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;
|
|
|
|
charList.Add(info);
|
|
}
|
|
} else if (line.IndexOf("scaleW=") != -1) {
|
|
Regex reg2 = new Regex(@"common lineHeight=(?<lineHeight>\d+)\s+.*scaleW=(?<scaleW>\d+)\s+scaleH=(?<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();
|
|
}
|
|
|
|
customFont.characterInfo = charList.ToArray();
|
|
|
|
string materialFilename = fntPath.Replace(".fnt", ".mat");
|
|
string textureFilename = fntPath.Replace(".fnt", ".png");
|
|
|
|
if (File.Exists(materialFilename)) {
|
|
Material 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);
|
|
customFont.material = mat;
|
|
}else{
|
|
Material mat = null;
|
|
{
|
|
Shader shader = Shader.Find("UI/Default Font");
|
|
mat = new Material(shader);
|
|
Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture;
|
|
mat.SetTexture("_MainTex", tex);
|
|
|
|
AssetDatabase.CreateAsset(mat, fntPath.Replace(".fnt", ".mat"));
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
customFont.material = mat;
|
|
}
|
|
EditorUtility.SetDirty(customFont);
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
Debug.Log("CreateFont Success");
|
|
}
|
|
}
|
|
}
|