溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

將NGUI的坐標(biāo)轉(zhuǎn)換Unity中的世界坐標(biāo)

發(fā)布時(shí)間:2020-07-08 22:47:51 來源:網(wǎng)絡(luò) 閱讀:1810 作者:速度速度撒 欄目:開發(fā)技術(shù)

今天遇到個(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ù)自己的東西去改,用倒是么改什么。

向AI問一下細(xì)節(jié)

免責(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)容。

AI