您好,登錄后才能下訂單哦!
今天遇到個(gè)問題,要把NGUI的transform坐標(biāo)轉(zhuǎn)成世界坐標(biāo),難住我了可,查了好多資料,總算有點(diǎn)眉目,弄出來和大家分享,其實(shí)發(fā)現(xiàn)NGUI的坐標(biāo)也是三維Vector3的,但是他和世界坐標(biāo)的Vector3的值代表的不一樣。不信大家可以打印出來,看一看。
public GameObject target; public GameoObject objUi; public void ScreenToWord() { //此原理就是先將UI坐標(biāo)轉(zhuǎn)成屏幕坐標(biāo),在把轉(zhuǎn)成的屏幕坐標(biāo)轉(zhuǎn)成世界坐標(biāo): //targetObject.layer就是移動(dòng)GameObject對(duì)象的Layer層 Camera wordCamera = NGUITools.FindCameraForLayer(target.layer); //objUi.layer就是要轉(zhuǎn)的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); } 或者 // 獲取按鈕的屏幕坐標(biāo)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);
可以根據(jù)自己的東西去改,用倒是么改什么。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。