世界坐标转到UGUI坐标

Posted unity3ds

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了世界坐标转到UGUI坐标相关的知识,希望对你有一定的参考价值。

public static Vector3 WorldToUI(Camera camera,Vector3 pos){
        CanvasScaler scaler = GameObject.Find("UIRoot").GetComponent<CanvasScaler>();
 
        float resolutionX = scaler.referenceResolution.x;
        float resolutionY = scaler.referenceResolution.y;
 
        Vector3 viewportPos = camera.WorldToViewportPoint(pos);
 
        Vector3 uiPos = new Vector3(viewportPos.x * resolutionX - resolutionX * 0.5f,
            viewportPos.y * resolutionY - resolutionY * 0.5f,0);
 
        return uiPos;
}
public static Vector2 WorldToUIPoint(Transform worldGo)
    {
        Vector2 pos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
            gameCamera.WorldToScreenPoint(worldGo.transform.position), canvas.worldCamera, out pos);
        RectTransform rect = transform.transform as RectTransform;
        return pos;
}

 

以上是关于世界坐标转到UGUI坐标的主要内容,如果未能解决你的问题,请参考以下文章

UGUI ScrollView中显示模型和特效

Unity中UGUI人物血条跟随的几种实现方式

UGUI与NGUI的区别与优缺点

Unity UGUI 实现简单拖拽功能

转:UGUI与NGUI的区别与优缺点

NGUI与UGUI的区别