服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - unity将图片转换成字体的方法

unity将图片转换成字体的方法

2022-09-03 14:33醉落尘阳光 C#

这篇文章主要为大家详细介绍了unity将图片转换成字体的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了unity利用图片来生成字体的具体代码,供大家参考,具体内容如下

开发中,可能会用到需要将图片转换成字体的需求。

bmfont 插件 导入图片

unity将图片转换成字体的方法

unity将图片转换成字体的方法

然后生成 .fnt 和 .png 两个文件 (文件格式可以在设置中更改)
将这两个文件导入unity 将png 切割成精灵
创建材质、将贴图拖上去。
创建字体、将材质拖上去。

unity将图片转换成字体的方法

数据怎么算出来的公式百度上面有,此处略去。也可以利用代码来生成

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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

延伸 · 阅读

精彩推荐
  • C#C#使用checkedListBox1控件链接数据库的方法示例

    C#使用checkedListBox1控件链接数据库的方法示例

    这篇文章主要介绍了C#使用checkedListBox1控件链接数据库的方法,结合具体实例形式分析了数据库的创建及checkedListBox1控件连接数据库的相关操作技巧,需要的朋...

    a7719485247522022-01-12
  • C#轻松学习C#的哈希表

    轻松学习C#的哈希表

    轻松学习C#的哈希表,对C#的哈希表感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的哈希表。...

    丿木呈广予口贝11212021-11-04
  • C#C#基础知识之this关键字介绍

    C#基础知识之this关键字介绍

    本文主要介绍this关键字的几种使用方法,this可以代表当前实例,可以调用其他构造函数,还可以用来构建索引器,这里都有一一举例说明。...

    Swich3712021-11-19
  • C#C#实现读取注册表监控当前操作系统已安装软件变化的方法

    C#实现读取注册表监控当前操作系统已安装软件变化的方法

    这篇文章主要介绍了C#实现读取注册表监控当前操作系统已安装软件变化的方法,涉及C#针对注册表的读取与监控技巧,非常具有实用价值,需要的朋友可以参考...

    我心依旧6862021-10-20
  • C#C# 设计模式系列教程-代理模式

    C# 设计模式系列教程-代理模式

    代理模式对客户端来说,隐藏了真实对象的细节及复杂性,实现了客户端(调用者)与真实对象的松耦合,提高了运行速度。...

    Wang Juqiang7722021-11-23
  • C#UnityShader3实现波浪效果

    UnityShader3实现波浪效果

    这篇文章主要为大家详细介绍了UnityShader3实现波浪效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    宏哥19957052022-07-09
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10172021-12-08
  • C#C#编写ActiveX网页截图控件

    C#编写ActiveX网页截图控件

    这篇文章主要介绍了C#编写ActiveX网页截图控件,作为学习C#编写ActiveX的一个简单入门教程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小...

    大萝卜控7982021-11-25