c# – 将UI RectTransform移动到世界位置

我有一个名为Player的GameObject和一个Canvas. HealthBar GameObject是Canvas(Canvas> HealthBar)的子项.我有一个HP栏跟随玩家的脚本.它应该跟随玩家在其上方的固定位置,因此它位于精灵的顶部附近.
这是HP Follow Script的“跟随”部分.

        void Update () 
{
    Vector3 currentPos = transform.position;
    Vector3 playerPos = Camera.main.WorldToViewportPoint (Player.transform.position);
    transform.position = playerPos;

问题是HP栏随着角色一起移动,但只是玩家移动速度的一小部分.例如,如果玩家移动一个单位,则该条移动0.1个单位.

错误视频:https://streamable.com/vaz7h

最佳答案
你想让一个UI对象(Image)跟随一个GameObject是一个SpriteRenderer或MeshRenderer.

换句话说,这可以被描述为将世界点转换为UI点.

下面这个简单的函数将世界位置转换为UI空间.它将UI的Canvas作为参数,然后将您要转换为UI位置的位置(在您的情况下为Player.transform.position变量).

移动UI对象的关键是RectTransformUtility.ScreenPointToLocalPointInRectangle功能,它将屏幕点转换为ui矩形本地点.

public Vector3 worldToUISpace(Canvas parentCanvas, Vector3 worldPos)
{
    //Convert the world for screen point so that it can be used with ScreenPointToLocalPointInRectangle function
    Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
    Vector2 movePos;

    //Convert the screenpoint to ui rectangle local point
    RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, screenPos, parentCanvas.worldCamera, out movePos);
    //Convert the local point to world point
    return parentCanvas.transform.TransformPoint(movePos);
}

用法:

public GameObject Player;
public Canvas canvas;
void Update()
{
    //Convert the player's position to the UI space then apply the offset
    transform.position = worldToUISpace(canvas, Player.transform.position);
}

现在,假设您已经将UI定位到它应该位于的位置,并且您现在希望它跟随另一个Object(播放器),您需要使用上面的代码实现一个简单的偏移.这真的很容易.以下是它应该是什么样子:

public GameObject Player;
public Canvas canvas;


Vector3 Offset = Vector3.zero;

void Start()
{
    Offset = transform.position - worldToUISpace(canvas, Player.transform.position);
}

void Update()
{
    //Convert the player's position to the UI space then apply the offset
    transform.position = worldToUISpace(canvas, Player.transform.position) + Offset;
}

public Vector3 worldToUISpace(Canvas parentCanvas, Vector3 worldPos)
{
    //Convert the world for screen point so that it can be used with ScreenPointToLocalPointInRectangle function
    Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
    Vector2 movePos;

    //Convert the screenpoint to ui rectangle local point
    RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, screenPos, parentCanvas.worldCamera, out movePos);
    //Convert the local point to world point
    return parentCanvas.transform.TransformPoint(movePos);
}

转载注明原文:c# – 将UI RectTransform移动到世界位置 - 代码日志