自己写的BMFont导入工具,快速把图片转换为美术字体
配合BMFont工具使用可以快速把图片转换为美术字,在项目中使用美术字体,需要准备一个材质球 一个空白字体,然后自己改一下路径,这三个脚本是分开的 ,先点art。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
public class ArtFontTool
{
static Dictionary<Image, string> imageDic = null;
[MenuItem("Font/Make ArtFont")]
static public void GenAllFont()
{
imageDic = new Dictionary<Image, string>();
string prefabs = Application.dataPath + "/BundleResources/UI/Prefabs/";
foreach (var dir in Directory.GetFiles(prefabs,"*.prefab"))
{
ProcPrefab(dir.Substring(prefabs.Length));
}
AssetDatabase.SaveAssets();
}
static void ProcPrefab(string prefab)
{
Debug.Log("------------------------------------");
Debug.Log(prefab);
var prefabName = "Assets/BundleResources/UI/Prefabs/" + prefab;
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(prefabName);
if(go == null)
{
return;
}
imageDic.Clear();
Image[] imgs = go.GetComponentsInChildren<Image>(true);
for(int i = 0; i < imgs.Length; ++i)
{
Image img = imgs[i];
//var hash = img.mainTexture.imageContentsHash;
//var sobj = new SerializedObject(img.sprite);
Sprite s = img.sprite;
string path = AssetDatabase.GetAssetPath(s.GetInstanceID());
string relativeUrl = path.Replace("Assets/BundleResources/", "");
if(relativeUrl.StartsWith("UI/ArtFont/Atlas"))
{
Debug.Log(path + "------------->" + img.name);
imageDic.Add(img,relativeUrl);
img.sprite = null;
}
}
if(imageDic.Count > 0)
{
ArtFontParser parser = go.GetComponent<ArtFontParser>();
if(parser == null)
{
parser = go.AddComponent<ArtFontParser>();
}
var it = imageDic.GetEnumerator();
while(it.MoveNext())
{
if(!parser.ImageDic.ContainsKey(it.Current.Key))
{
parser.ImageDic.Add(it.Current.Key, it.Current.Value);
}
}
}
AssetDatabase.Refresh();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ArtFontParser : MonoBehaviour , ISerializationCallbackReceiver
{
public List<string> UrlList = new List<string>();
public List<Image> ImageList = new List<Image>();
//Unity doesn't know how to serialize a Dictionary
public Dictionary<Image, string> ImageDic = new Dictionary<Image, string>();
private List<KEngine.SpriteLoader> loaders = new List<KEngine.SpriteLoader>();
public void OnBeforeSerialize()
{
UrlList.Clear();
ImageList.Clear();
foreach (var kvp in ImageDic)
{
UrlList.Add(kvp.Value);
ImageList.Add(kvp.Key);
}
}
public void OnAfterDeserialize()
{
ImageDic = new Dictionary<Image, string>();
for (int i = 0; i != Math.Min(UrlList.Count, ImageList.Count); i++)
{
ImageDic.Add(ImageList[i], UrlList[i]);
}
}
void Awake()
{
string lan = KEngine.KTool.GetLanguageSimplify();
lan = string.Format("/{0}/", lan);
var it = ImageDic.GetEnumerator();
while (it.MoveNext())
{
var key = it.Current.Value;
key = key.Replace("/CH/", lan);
var loader = KEngine.SpriteLoader.Load(key, (bool isOk, Sprite tex) =>
{
if (isOk)
{
it.Current.Key.sprite = tex;
it.Current.Key.SetNativeSize();
}
});
loaders.Add(loader);
}
}
void OnDestroy()
{
for (int i = 0; i < loaders.Count; ++i)
{
var loader = loaders[i];
if (loader != null)
{
loader.Release();
}
}
loaders = null;
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEngine;
public class FontTool
{
[MenuItem("Font/Make BmFont")]
static void GenAllFont()
{
string fontPath = Application.dataPath + "/BundleResources/BMFont/";
foreach (var dir in Directory.GetDirectories(fontPath))
{
GetFont(dir.Substring(fontPath.Length));
}
}
static void GetFont(string fontName)
{
var root = string.Format("Assets/BundleResources/BMFont/{0}/", fontName);
Material mtr = AssetDatabase.LoadAssetAtPath<Material>(string.Format("{0}{1}.mat",root,fontName));
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(string.Format("{0}{1}_0.png",root,fontName));
mtr.mainTexture = texture;
Font font = AssetDatabase.LoadAssetAtPath<Font>(string.Format("{0}{1}.fontsettings",root,fontName));
XmlDocument xml = new XmlDocument();
xml.Load(string.Format("{0}/BundleResources/BMFont/{1}/{2}.fnt", Application.dataPath,fontName,fontName));
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.height;//width
chtInfo.glyphHeight = texture.height;
chtInfo.glyphWidth = texture.width;
chtInfo.index = int.Parse(xe.GetAttribute("id"));
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 / 2);
chtInfo.maxX = width;
chtInfo.maxY = height / 2;
chtInfo.advance = width;
chtInfoList.Add(chtInfo);
}
font.characterInfo = chtInfoList.ToArray();
EditorUtility.SetDirty(font);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("BMFont gen Done ->" + root);
}
}
自己写的BMFont导入工具,快速把图片转换为美术字体相关推荐
- 计算机画图软件技巧,Win7系统自带画图工具快速浏览图片的技巧
Ghost Win7系统中的自带的画图功能其实非常好用,但是却被大部分用户忽略,尤其是在 win7旗舰版系统 中其界面和功能比其它系统又得到了进一步的提升,能在空白绘图区域或在现有图片上创建绘图.利用 ...
- 教你用OCR文字识别工具快速识别图片上的文字
OCR文字识别工具,指利用OCR(Optical Character Recognition,光学字符识别) 技术,将图片.照片上的文字内容,直接转换为可编辑文本的工具.软件可以把图片 转换成可以编辑 ...
- 使用windows默认工具快速修改图片尺寸
工具:画图软件 1. 随意找一张图片 2. 右击选择编辑(Edit) 3. 单击Resize 4.选择Pixels像素,输入图片尺寸大小即可修改
- 工具推荐#简单图片转换为ASCII图(基于字符)
一.场景需求 场景需求:在做笔记的过程中,有时遇到一些简单的流程图.结构图.时序图等.不想以图片的方式保存在笔记中(>10kB),而想用ASCII图,直接用文字表示图片(<1kB). 占用 ...
- vvv在线文档导出工具_使用ApiPost工具快速生成在线接口文档
ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 .使用者不仅可以利用apio ...
- 你必须知道的linux开发快捷键,熟知工具快速开发
前言 节省时间就是提高效率,时间就是金钱,时间就是生命. 鲁迅名言:时间就是生命,无端地空耗别人的时间,其实是无异于谋财害命的.那如果你看到这篇文章不学习不点赞,无异于自杀. 你看了又不转发分享,无异 ...
- 如何使用录音文件转换成文字工具快速完成转换
如何使用录音文件转换成文字工具快速完成转换 长时间的音频文件转换成文字相信让很多人头疼过.一句一句的听译浪费时间十分麻烦还很容易跟不上语速需要返回重新听,那么这里就来和大家讲讲如何只需要几分钟就可以将 ...
- elasticdump安装_elasticsearch导出、导入工具-elasticdump
elasticsearch导出.导入工具-elasticdump elasticsearch 数据导入到本地,或本地数据导入到elasticsearch中,或集群间的数据迁移,可以用elasticse ...
- Linux工具快速上手,Linux很实用命令
Linux工具快速上手 前言 Linux下有很多命令行工具供我们使用,每个工具总是提供了大量参数供我们选择: 实际工作中,我们用到的工具,最常用的总是那么几个参数组合: 为此,我写了这本书相对实用的书 ...
- mysql数据万能导入工具下载_mysql数据导入工具下载 - 数据导入工具(EMS Data Export for MySQL) v3.7.0多语特别版下载 - 第九软件网...
EMS Data Export for MySQL多语特别版是一款交叉平台的数据库导入工具,可以快速地从MS Excel 97-2007, MS Access, DBF, XML, TXT, CSV, ...
最新文章
- python把光标定义到指定的位置并删除之前的字符_python 批量修改预定字符串并将修改后的字符串插入文件指定位置...
- JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor
- Android在一个APP中通过包名或类名启动另一个APP
- linux系列服务总结之四:SAMBA共享设置完整介绍
- python观察日志(part7)--可变长参数元祖
- 《Access 2007开发指南(修订版)》一一1.5 什么是数据库对象
- ubuntu生成密钥和证书_基于浏览器的密钥生成以及与浏览器的密钥/证书存储的交互...
- 在ubuntu16.04安装hadoop集群时ssh不成功
- pythonmatplotlib共享绘图区域_在matplotlib绘图中,y标记右侧,共享x和y
- unlocker解锁虚拟机安装黑苹果出现权限错误问题permission denied
- 最小巧最简单最安全的KMS模拟器vlmcsd,最新支持Win10 server2016和Office2016!
- 值得终身背诵的道家名言50句,拔高人生境界
- Feign客户端415错误:FeignException$UnsupportedMediaType: [415 ]
- WebRTC源码研究(46)WebRCT统计信息
- strcmp, strcasecmp, memcmp
- c语言strtok用法详细解释
- Purdue, Mathematics Area Examination 学习笔记(1)
- 生活随记-冬天的馈赠
- 通俗讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
- Windows PC上创建大数据职业技能竞赛实验环境之五--hadoop、hive和spark编程
热门文章
- JS base64 加密和 后台 base64解密(防止中文乱码)
- java毕业设计蔚蓝在线学习平台源码+lw文档+mybatis+系统+mysql数据库+调试
- CMOS工作原理和概念
- WPS Office 绿色版|WPS Office 2019绿色专业版下载 v11.8.2.8053(免注册)
- Mac M1 百度网盘客户端无法打开,网络连接不上
- 如何打开.jar文件?
- 【VS】错误1error LNK1168: 无法打开 F:\C++6\prob\ConsoleApplication1\Debug\ConsoleApplication1.exe 进行写入
- java解析搜狗词库scel文件到txt
- 【最全】《数据库原理及应用》知识点整理+习题
- 软件项目管理 2.1.项目立项