参考两位博主作出改进
切图: https://blog.csdn.net/angelsmiles/article/details/50464369
生成字体: http://www.unity.5helpyou.com/3211.html
using UnityEngine;
using UnityEditor;
using System.IO;
public static class SpriteSheetPackerImport
{
[MenuItem("LuaFramework/Process to Sprites")]
static void ProcessToSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取旋转的对象
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称
string path = rootPath + "/" + image.name + ".PNG";//图片路径名称
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;
AssetDatabase.CreateFolder(rootPath, image.name);//创建文件夹
foreach (SpriteMetaData metaData in texImp.spritesheet)//遍历小图集
{
Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);
for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)//Y轴像素
{
for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)
myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, image.GetPixel(x, y));
}
//转换纹理到EncodeToPNG兼容格式
if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24)
{
Texture2D newTexture = new Texture2D(myimage.width, myimage.height);
newTexture.SetPixels(myimage.GetPixels(0), 0);
myimage = newTexture;
}
var pngData = myimage.EncodeToPNG();
File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".PNG", pngData);
}
}
}
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Xml;
public class BMFont
{
[MenuItem("Assets/FontOption")]
static void Font()
{
string dirName = "";
string fntname = SelectObjectPathInfo(ref dirName).Split('.')[0];
Texture2D texture = AssetDatabase.LoadAssetAtPath(dirName + fntname + "_0.png", typeof(Texture2D))as Texture2D;
string textureFilename = dirName + texture.name + ".png";
//创建u3d字体
Font font = new Font();
{
AssetDatabase.CreateAsset(font, dirName + fntname + ".fontsettings");
AssetDatabase.SaveAssets();
}
XmlDocument xml = new XmlDocument();
xml.Load(dirName + fntname + ".fnt");//这是在BMFont里得到的那个.fnt文件,因为是xml文件,所以我们就用xml来解析
List<CharacterInfo> chtInfoList = new List<CharacterInfo>();
XmlNode node = xml.SelectSingleNode("font/chars");
foreach (XmlNode nd in node.ChildNodes)
{
XmlElement xe = (XmlElement)nd;
int x = int.Parse(xe.GetAttribute("x"));
int y = int.Parse(xe.GetAttribute("y"));
int width = int.Parse(xe.GetAttribute("width"));
int height = int.Parse(xe.GetAttribute("height"));
int advance = int.Parse(xe.GetAttribute("xadvance"));
CharacterInfo chtInfo = new CharacterInfo();
float texWidth = texture.width;
float texHeight = texture.width;
chtInfo.glyphHeight = texture.height;
chtInfo.glyphWidth = texture.width;
chtInfo.index = int.Parse(xe.GetAttribute("id"));
//这里注意下UV坐标系和从BMFont里得到的信息的坐标系是不一样的哦,前者左下角为(0,0),
//右上角为(1,1)。而后者则是左上角上角为(0,0),右下角为(图宽,图高)
chtInfo.uvTopLeft = new Vector2((float)x / texture.width, 1 - (float)y / texture.height);
chtInfo.uvTopRight = new Vector2((float)(x + width) / texture.width, 1 - (float)y / texture.height);
chtInfo.uvBottomLeft = new Vector2((float)x / texture.width, 1 - (float)(y + height) / texture.height);
chtInfo.uvBottomRight = new Vector2((float)(x + width) / texture.width, 1 - (float)(y + height) / texture.height);
chtInfo.minX = 0;
chtInfo.minY = -height;
chtInfo.maxX = width;
chtInfo.maxY = 0;
chtInfo.advance = advance;
chtInfoList.Add(chtInfo);
}
font.characterInfo = chtInfoList.ToArray();
//创建材质
Material mat = null;
{
Shader shader = Shader.Find("GUI/Text Shader");
mat = new Material(shader);
mat.SetTexture("_MainTex", texture);
AssetDatabase.CreateAsset(mat, dirName + fntname + ".mat");
AssetDatabase.SaveAssets();
}
font.material = mat;
AssetDatabase.Refresh();
Debug.Log("字体制作完成!");
}
public static string SelectObjectPathInfo(ref string dirName)
{
if (UnityEditor.Selection.activeInstanceID < 0)
{
return "";
}
string path = UnityEditor.AssetDatabase.GetAssetPath(UnityEditor.Selection.activeInstanceID);
dirName = Path.GetDirectoryName(path) + "/";
return Path.GetFileName(path);
}
}
右键fnt,FontOption,自动生成字体,在创建的Text引用就可以使用了。