今天遇到个问题,要把NGUI的transform坐标转成世界坐标,难住我了可,查了好多资料,总算有点眉目,弄出来和大家分享,其实发现NGUI的坐标也是三维Vector3的,但是他和世界坐标的Vector3的值代表的不一样。不信大家可以打印出来,看一看。

 public GameObject target;
 public GameoObject objUi;
 
 public void ScreenToWord()
    {
    
    //此原理就是先将UI坐标转成屏幕坐标,在把转成的屏幕坐标转成世界坐标:
    
            //targetObject.layer就是移动GameObject对象的Layer层
        Camera wordCamera = NGUITools.FindCameraForLayer(target.layer);
        //objUi.layer就是要转的NGUI的UI;
        Camera guiCamera = NGUITools.FindCameraForLayer(objUi.layer);
        if (wordCamera == null || guiCamera == null )
        {
            return;
        }
        Vector3 pos = guiCamera.WorldToScreenPoint(objUi.transform.position);
        pos.z = 1.0f;
        pos = wordCamera.ScreenToWorldPoint(pos);
        pos.y = 0.0f;
        targetObject.transform.position = new Vector3(pos.x, pos.y, pos.z);
    }
    
    
    
    或者
    
    
    
    // 获取按钮的屏幕坐标Vector3 pos = UICamera.currentCamera.WorldToScreenPoint(_button.transform.position);
pos.z = 1;

pos = Camera.main.ScreenToWorldPoint(pos);
_cube.transform.position = new Vector3(pos.x,pos.y,pos.z);

可以根据自己的东西去改,用倒是么改什么。