今天遇到個問題,要把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);可以根據自己的東西去改,用倒是么改什么。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。