characters | 需要位于该字体纹理中的字符。 |
size | 所请求的字符的大小(默认值 0 将使用该字体的默认大小)。 |
style | 所请求的字符的样式。 |
请求将被添加到该字体纹理(仅限动态字体)的字符。
注意:仅当需要实现自己的文本渲染时使用。
调用此函数,请求 Unity 确保字符串 characters
中的所有字符都可用于
该字体的字体纹理(及其 characterInfo
属性)。当您想要实现自己的代码以渲染动态字体时,
这非常有用。您可以为这些字符提供自定义字体大小和样式。如果 size
为零
(默认值),其将对该字体使用默认大小。
如果该字体纹理没有空间来添加所有请求的字符,则 RequestCharactersInTexture 可能导致
重新生成该字体纹理。如果重新生成该字体纹理,将仅包含已使用的字符(使用 Font.RequestCharactersInTexture
或者在上一帧中使用 Unity 的文本渲染函数)。因此,
对于要使用自定义字体渲染函数渲染的屏幕上的任何文本,建议始终调用 RequestCharactersInTexture,
即使这些字符当前存在于该纹理中也是如此,以确保它们在纹理重新构建期间
不会被清除。
另请参阅:textureRebuilt、GetCharacterInfo。
using UnityEngine; using System.Collections;
public class CustomFontMeshGenerator : MonoBehaviour { Font font; string str = "Hello World"; Mesh mesh;
void OnFontTextureRebuilt(Font changedFont) { if (changedFont != font) return;
RebuildMesh(); }
void RebuildMesh() { // Generate a mesh for the characters we want to print. var vertices = new Vector3[str.Length * 4]; var triangles = new int[str.Length * 6]; var uv = new Vector2[str.Length * 4]; Vector3 pos = Vector3.zero; for (int i = 0; i < str.Length; i++) { // Get character rendering information from the font CharacterInfo ch; font.GetCharacterInfo(str[i], out ch);
vertices[4 * i + 0] = pos + new Vector3(ch.minX, ch.maxY, 0); vertices[4 * i + 1] = pos + new Vector3(ch.maxX, ch.maxY, 0); vertices[4 * i + 2] = pos + new Vector3(ch.maxX, ch.minY, 0); vertices[4 * i + 3] = pos + new Vector3(ch.minX, ch.minY, 0);
uv[4 * i + 0] = ch.uvTopLeft; uv[4 * i + 1] = ch.uvTopRight; uv[4 * i + 2] = ch.uvBottomRight; uv[4 * i + 3] = ch.uvBottomLeft;
triangles[6 * i + 0] = 4 * i + 0; triangles[6 * i + 1] = 4 * i + 1; triangles[6 * i + 2] = 4 * i + 2;
triangles[6 * i + 3] = 4 * i + 0; triangles[6 * i + 4] = 4 * i + 2; triangles[6 * i + 5] = 4 * i + 3;
// Advance character position pos += new Vector3(ch.advance, 0, 0); } mesh.vertices = vertices; mesh.triangles = triangles; mesh.uv = uv; }
void Start() { font = Font.CreateDynamicFontFromOSFont("Helvetica", 16); // Set the rebuild callback so that the mesh is regenerated on font changes. Font.textureRebuilt += OnFontTextureRebuilt;
// Request characters. font.RequestCharactersInTexture(str);
// Set up mesh. mesh = new Mesh(); GetComponent<MeshFilter>().mesh = mesh; GetComponent<MeshRenderer>().material = font.material;
// Generate font mesh. RebuildMesh(); }
void Update() { // Keep requesting our characters each frame, so Unity will make sure that they stay in the font when regenerating the font texture. font.RequestCharactersInTexture(str); }
void OnDestroy() { Font.textureRebuilt -= OnFontTextureRebuilt; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.