天天看点

UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)

近来在游戏实现一些功能时,原以为很简单的东西却难以实现,需要查大量资料才能去实现它,浪费了时间,降低了开发进度

深感基础之薄弱,究其原因是在学习新知识时浅尝辄止,基础不牢靠,很多知识没有经过验证就跳过它,这对于学习来说是非常忌讳的,因此博主在此警醒自己,今后的学习要举一反三寻根问底。~~欲登高而穷目,勿筑台于浮沙!时间:2019年5月20日(520)

实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),即先将游戏场景中的世界坐标通过游戏场景Camera转化为屏幕坐标(Camera.main.WorldToScreenPoint(point)),再通过UICamera将该屏幕坐标转换为UI本地坐标(RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPoint, uiCamera, out localPoint))

疑问?

那么初学的小白们(博主也是小白)可能会疑惑UGUI为什么会有UICamera,因为UGUI的Canvas的RenderMode有三种模式,一般默认为Overlay(全覆盖),另一种为Camera。

  1. 当RenderMode为Overlay时,UI坐标系和屏幕坐标系是一样的,则不需要通过UICamera来转换,直接将第一步得到的屏幕坐标赋值给Hud的localPosition就可以了。
  2. 一般游戏中要实现3D物体在在UI之上时,就不能用画布全覆盖模式,而是要再创建一个Camera来单独渲染UI,Clear Flags为Depth Only。当RenderMode为Camera时,则需要以上两步来得到本地坐标。

注意:最后赋值给Hud的本地坐标是localPosition而不是position

拓展:世界坐标、本地坐标、视口坐标,各种坐标系要理解清楚,在学习shader时也是有用的

以下是实现hud跟随3D物体的脚本,只是测试用,不是开发中的代码,脚本挂在任意游戏物体上 demo下载

using UnityEngine;

public class SceneFollowUI : MonoBehaviour
{
    public RectTransform hud;            //Hud
    public RectTransform canvas;//UI的父节点
    public Transform parent;    //跟随的3D物体
    public Camera uiCamera;     //UICamera 

    Vector3 offset;      //hud偏移量
    Vector3 cachePoint;
    float originalDistance;
    float factor = 1;
    bool visiable = true;

    void Start()
    {
        offset = hud.localPosition - WorldPointToUILocalPoint(parent.position);
        cachePoint = parent.position;
        originalDistance = GetCameraHudRootDistance();
        UpdateVisible();
    }

    void LateUpdate()
    {
        if (cachePoint != parent.position)
        {
            float curDistance = GetCameraHudRootDistance();
            factor = originalDistance / curDistance;
            UpdatePosition(); //更新Hud位置
            UpdateScale();    //更新Hud的大小
            UpdateVisible();  //更新Hud是否可见,根据需求设置:factor或者根据和相机距离设置,一定范围内可见,相机视野范围内可见 等等
        }
    }


    private void UpdateVisible()
    {
        
    }

    private void UpdatePosition()
    {
        hud.localPosition = WorldPointToUILocalPoint(parent.position) + offset * factor;
        cachePoint = parent.position;
    }

    private void UpdateScale()
    {
        hud.localScale = Vector3.one * factor;
    }

    private float GetCameraHudRootDistance()
    {
        return Vector3.Distance(Camera.main.transform.position, parent.position);
    }

    private Vector3 WorldPointToUILocalPoint(Vector3 point)
    {
        Vector3 screenPoint = Camera.main.WorldToScreenPoint(point);
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPoint, uiCamera, out localPoint);
        return localPoint;
    }
}

           
UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)

继续阅读