首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将画布和TextMeshPro垂直保持在字符上方

如何将画布和TextMeshPro垂直保持在字符上方
EN

Stack Overflow用户
提问于 2022-01-02 15:15:46
回答 1查看 512关注 0票数 1

我制作了CanvasTextMeshPro,然后制作了这个立方体对象的子对象。

代码语言:javascript
复制
    GameObject myCanvas = new GameObject();
    myCanvas.name = "TestCanvas";
    myCanvas.AddComponent<Canvas>();
    Canvas canvas = myCanvas.GetComponent<Canvas>();
    canvas.renderMode = RenderMode.WorldSpace;
            

    GameObject myText = new GameObject();
    myText.name = "wibble";
    myText.transform.parent = myCanvas.transform;
    myText.AddComponent<TextMeshPro>();
    TextMeshPro text = myText.GetComponent<TextMeshPro>();
    text.text = "Ta-dah!";
    text.fontSize = 12;
    //text.font = (Font)Resources.Load("MyFont");
    myCanvas.transform.parent = cube.transform; // attache canvas as child of cube.

但是,到目前为止,当多维数据集轮回文本时,它仍然有效。

我想保持文字始终略高于字符沿Y轴,即使当旋转发生。

我该怎么做??

EN

回答 1

Stack Overflow用户

发布于 2022-01-02 15:58:32

我会在画布游戏对象中添加一个Billboard组件来处理旋转,并使画布始终面向摄像机。

公告牌部分:

使用UnityEngine;

代码语言:javascript
复制
public class Billboard : MonoBehaviour {
    public Transform cam;
    private void Start() {
        cam = Camera.main.transform;
    }
    void LateUpdate() {
        transform.LookAt(transform.position + cam.forward);
    }
}

使用myCanvas.AddComponent<Billboard>();添加组件,整个脚本如下:

代码语言:javascript
复制
    GameObject myCanvas = new GameObject();
    myCanvas.name = "TestCanvas";
    myCanvas.AddComponent<Canvas>();
    Canvas canvas = myCanvas.GetComponent<Canvas>();
    canvas.renderMode = RenderMode.WorldSpace;

    myCanvas.AddComponent<Billboard>(); //component added here

    GameObject myText = new GameObject();
    myText.name = "wibble";
    myText.transform.parent = myCanvas.transform;
    myText.AddComponent<TextMeshPro>();
    TextMeshPro text = myText.GetComponent<TextMeshPro>();
    text.text = "Ta-dah!";
    text.fontSize = 12;
    //text.font = (Font)Resources.Load("MyFont");
    myCanvas.transform.parent = transform; // attache canvas as child of cube.

编辑:旋转立方体的图像与画布正面进入照相机。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70557147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档