本文实例为大家分享了unity利用图片来生成字体的具体代码,供大家参考,具体内容如下
开发中,可能会用到需要将图片转换成字体的需求。
bmfont 插件 导入图片
然后生成 .fnt 和 .png 两个文件 (文件格式可以在设置中更改)
将这两个文件导入unity 将png 切割成精灵
创建材质、将贴图拖上去。
创建字体、将材质拖上去。
数据怎么算出来的公式百度上面有,此处略去。也可以利用代码来生成
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