码迷,mamicode.com
首页 > 其他好文 > 详细

世界坐标转到UGUI坐标

时间:2018-11-08 14:21:43      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:oca   component   screen   port   get   tran   ipo   world   find   

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坐标

标签:oca   component   screen   port   get   tran   ipo   world   find   

原文地址:https://www.cnblogs.com/unity3ds/p/9928609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!