unity将图片转换成字体的方法
作者:醉落尘阳光 发布时间:2023-02-16 22:51:09
标签:unity,图片转换
本文实例为大家分享了unity利用图片来生成字体的具体代码,供大家参考,具体内容如下
开发中,可能会用到需要将图片转换成字体的需求。
BMFONT 插件 导入图片
然后生成 .fnt 和 .png 两个文件 (文件格式可以在设置中更改)
将这两个文件导入unity 将png 切割成精灵
创建材质、将贴图拖上去。
创建字体、将材质拖上去。
数据怎么算出来的公式百度上面有,此处略去。也可以利用代码来生成
using UnityEngine;
using System.Collections;
using System;
using System.Xml;
public class CustomFontImportor : MonoBehaviour {
public Font font;
public TextAsset textAsset;
void Awake()
{
if (font == null || textAsset == null)
{
Debug.LogError("请设置font和textAsset.");
return;
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(textAsset.text);
int totalWidth = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleW"].InnerText);
int totalHeight = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleH"].InnerText);
XmlElement xml = xmlDocument["font"]["chars"];
ArrayList characterInfoList = new ArrayList();
for (int i = 0; i < xml.ChildNodes.Count; ++i)
{
XmlNode node = xml.ChildNodes[i];
if (node.Attributes == null)
{
continue;
}
int index = Convert.ToInt32(node.Attributes["id"].InnerText);
int x = Convert.ToInt32(node.Attributes["x"].InnerText);
int y = Convert.ToInt32(node.Attributes["y"].InnerText);
int width = Convert.ToInt32(node.Attributes["width"].InnerText);
int height = Convert.ToInt32(node.Attributes["height"].InnerText);
int xOffset = Convert.ToInt32(node.Attributes["xoffset"].InnerText);
int yOffset = Convert.ToInt32(node.Attributes["yoffset"].InnerText);
int xAdvance = Convert.ToInt32(node.Attributes["xadvance"].InnerText);
CharacterInfo info = new CharacterInfo();
Rect uv = new Rect();
uv.x = (float)x / totalWidth;
uv.y = (float)(totalHeight - y - height) / totalHeight;
uv.width = (float)width / totalWidth;
uv.height = (float)height / totalHeight;
info.index = index;
info.uvBottomLeft = new Vector2(uv.xMin, uv.yMin);
info.uvBottomRight = new Vector2(uv.xMax, uv.yMin);
info.uvTopLeft = new Vector2(uv.xMin, uv.yMax);
info.uvTopRight = new Vector2(uv.xMax, uv.yMax);
info.minX = xOffset;
info.maxX = xOffset + width;
info.minY = -yOffset - height;
info.maxY = -yOffset;
info.advance = xAdvance;
info.glyphWidth = width;
info.glyphHeight = height;
characterInfoList.Add(info);
}
font.characterInfo = characterInfoList.ToArray(typeof(CharacterInfo)) as CharacterInfo[];
Debug.Log("生成成功.");
}
}
来源:https://blog.csdn.net/weixin_38082526/article/details/78589135


猜你喜欢
- 首先在命令行创建一个PhoneGap工程phonegap create . "jspdf.sample" "J
- 本文实例讲述了C#实现汉字转拼音或转拼音首字母的方法。分享给大家供大家参考。具体实现方法如下:/// <summary>///
- 前言smart-doc 是一款同时支持 java restful api 和 Apache Dubbo rpc 接口文档生成的工具,smar
- springmvc @RequestBody String类型参数通过如下配置: <bean id="mapp
- 相信对于打印三角形都没什么难度,只需要利用for循环嵌套使用就行但是对于打印圆形和三角形不同因为到圆心距离相等的点一般不会横坐标和纵坐标都为
- java.lang.Error: Unresolved compilation problems:出现该问题的原因主要是编译等级跟jdk不一
- 问题遇到问题:在前后端分离跨域访问的项目中shiro进行权限拦截失效 (即使有正确权限的访问也会被拦截) 时造成302重定向错误等问题报错:
- 1 需求描述我们现在要干一个什么事情呢,我们要在浏览器输入一个请求地址,然后我们的后端就给我返回一个User对象即可,并且我希望以Json的
- 一、spring定时任务执行两次问题重现和解析最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时
- 部分同学在使用 idea 时可能会遇到输入 sout 无法出现自动补全 System.out.println();的情况,其实 idea 默
- Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态。Memento模式相
- 1、直接使用getWindow().getDecorView().getRootView()直接使用getWindow().getDecor
- 最近在开发的过程中,一个列表的查询,涉及到了多表的关联查询,由于持久层使用的是mongodb,对这个非关系型数据使用的不是很多,所以在实现此
- 向shell提供命令非常简单,需要学习的注解很少。该命令的实现风格与使用依赖注入的应用程序的开发类相同,您可以利用Spring容器的所有特性
- 字段策略 0:”忽略判断”,1:”非 NULL 判断”),2:”非空判断”问题描述:当字段策略为 0 “忽略判断” 的时候,如果实体和数据库
- 一、C#方法中参数类型有4种参数类型,有时候很难记住它们的不同特征,下图对它们做一个总结,使之更容易比较和对照。二、C#方法中的参数1、值参
- final 类final 类不能被继承,同时,一旦用 final 修饰了类,也就意味着 final 类中的所有方法都被隐式地指定为 fina
- FeignClient设置动态Url1. 需求描述一般情况下,微服务内部调用都是通过注册中心,eureka,zookeeper,nacos等
- MyBatis中PageHelper不生效今天使用pageHelper,发现设置了PageHelper.startPage(page, pa
- 最近在用SpringMvc做Http接口时,对方在调用我接口时发现Date格式的默认转化为long,因此在前端页面看到的是一串数字。我们可以