如何将 ui 文本向上移动? [英] How can I move a ui text up?

查看:30
本文介绍了如何将 ui 文本向上移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是层次结构的屏幕截图,我有一个立方体和一个立方体的球体孩子.然后我将画布设置为空间世界叠加层和画布的 UI 文本子项.

This is a screenshot of the hierarchy I have a cube and a sphere child of the cube. Then I have a canvas set to space world overlay and a UI text child of the canvas.

我希望 UI 文本随着球体移动.

I want the UI text to move with the Sphere.

当我运行游戏时,文本位于球体的中心,但只有球体向上移动,文本保持在同一位置.我还尝试了其他画布模式屏幕空间相机和世界空间,但文本没有随球体移动.

When I'm running the game the text is in the center of the sphere but only the sphere is moving up the text stay at the same position. I tried also the other canvas modes screen space camera and world space but the text is not moving with the sphere.

此脚本附加到球体:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Testing : MonoBehaviour
{
    public GameObject myText;
    public float speed;
    public float offset;
    public bool go = false;

    private bool exited = false;
    private Vector3 exitPosition;

    private void Start()
    {
        myText.GetComponent<Text>().alignment = (TextAnchor)TextAlignment.Center;

        Debug.Log("Position : " + transform.position);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "Cube")
        {
            Debug.Log("Exited !");
        }

        exitPosition = new Vector3(transform.position.x,
                transform.position.y + offset, transform.position.z);

        exited = true;
    }

    private void Update()
    {
        if (go)
        {
            if (!exited)
            {
                transform.position += Vector3.up * speed * Time.deltaTime;
                myText.GetComponent<RectTransform>().position += Vector3.up * speed * Time.deltaTime;
            }
            else
            {
                transform.position = Vector3.MoveTowards(transform.position, exitPosition, speed * Time.deltaTime);
                myText.GetComponent<RectTransform>().position = Vector3.MoveTowards(myText.transform.position, exitPosition, speed * Time.deltaTime);
            }
        }
    }
}

我也尝试将带有文本的画布移动为球体的子元素,但文本仍然没有移动.

I tried also to move the Canvas with the Text to be a child of the Sphere but still, the text is not moving.

推荐答案

您需要将 Canvas 设置为 World Space,然后将 Canvas 设置为您的球体.

You are going to want to set the Canvas to World Space and then child the Canvas to your sphere.

另一种选择是将脚本添加到您的 UI 对象,并将其转换为世界空间坐标以跟随球体.类似的东西:

The other option is to add a script to your UI object and have it convert to world space coordinates to follow the sphere. Something along the lines of:

uiObject.transform.position = YourMainCamera.WorldToScreenPoint(theSphere.transform.position);

如果您使用第二个选项,请不要更改画布类型并且不要不要子对象.它将根据相对于您使用的相机的世界位置更改您的对象在球体上显示的位置.

If you use the second option, do not change your canvas type and do not child the object. It will change the position of your object to appear over the sphere based on the world position relative to the camera you are using.

我还应该添加,使用这两种解决方案中的任何一种,删除所有用于移动文本的其他代码.

I should also add, with either of these two solutions, delete all other code you are using to move the text.

这篇关于如何将 ui 文本向上移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆