<Unity3D>Unity3D的四種座標系

來源:互聯網
上載者:User


原文連結:

http://blog.csdn.net/zuoyamin/article/details/8813424

  1. World Space(全局座標):我們在情境中添加物體(如:Cube),他們都是以全局座標顯示在情境中的。transform.position可以獲得該位置座標。            
  2. Screen Space(螢幕座標):以像素來定義的,以螢幕的左下角為(0,0)點,右上方為(Screen.width,Screen.height),Z的位置是以相機的世界單位來衡量的。註:滑鼠位置座標屬於螢幕座標,Input.mousePosition可以獲得該位置座標,手指觸控螢幕幕也為螢幕座標,Input.GetTouch(0).position可以獲得單個手指觸控螢幕幕座標。

  3. ViewPort Space(視口座標):視口座標是標準的和相對於相機的。相機的左下角為(0,0)點,右上方為(1,1)點,Z的位置是以相機的世界單位來衡量的。(用的不多,反正我暫時沒有用到~呵呵~)

  4. 繪製GUI介面的座標系:這個座標系與螢幕座標系相似,不同的是該座標系以螢幕的左上方為(0,0)點,右下角為(Screen.width,Screen.height)。

  • 全局座標→螢幕座標:camera.WorldToScreenPoint(transform.position);這樣可以將全局座標轉換為螢幕座標。其中camera為情境中的camera對象。

  • 螢幕座標→視口座標:camera.ScreenToViewportPoint(Input.GetTouch(0).position);這樣可以將螢幕座標轉換為視口座標。其中camera為情境中的camera對象。

  • 視口座標→螢幕座標:camera.ViewportToScreenPoint();

  • 視口座標→全局座標:camera.ViewportToWorldPoint();
  • 案例1——在滑鼠點擊的位置上繪製一張圖片出來(關於繪製GUI介面座標系與螢幕座標系之間的關係)。[csharp] view plaincopy
    1. using UnityEngine;  
    2.  using System.Collections;  
    3.  public class test : MonoBehaviour   {          
    4.       //圖片    
    5.     public Texture img;      
    6.      //儲存滑鼠的位置座標      
    7.     private Vector2 pos;     
    8.      void OnGUI()     
    9.       {             
    10.           //滑鼠左擊,擷取當前滑鼠的位置         
    11.        if (Input.GetMouseButton(0))           
    12.           {         
    13.                pos = Input.mousePosition;             
    14.            }          
    15.              //繪製圖片        
    16.         GUI.DrawTexture(new Rect(pos.x,Screen.height - pos.y,100,100), img);         
    17.       }     
    18. }  
  • 案例2——座標顯示和座標轉換(這個是觸摸方面的。如果沒有觸控螢幕,那就將那個if去掉吧!)[csharp] view plaincopy
    1. using UnityEngine;     
    2. using System.Collections;     
    3. public class test: MonoBehaviour   {         
    4.      //情境的相機,拖放進來        
    5.     public Camera camera;          
    6.      //情境的物體        
    7.     private GameObject obj;      
    8.       void Start()        
    9.        {          
    10.             //初始化        
    11.         obj = GameObject.Find("Plane");         
    12.         }         
    13.          void Update ()       
    14.          {              
    15.                //有觸摸           
    16.           if (Input.touchCount > 0)              
    17.                {                   
    18.                    print("全局座標" + obj.transform.position);                    
    19.                     print("螢幕座標" + Input.GetTouch(0).position);                    
    20.                     print("全局座標→螢幕座標" + camera.WorldToScreenPoint(obj.transform.position));                    
    21.                     print("螢幕座標→視口座標" + camera.ScreenToViewportPoint(Input.GetTouch(0).position));                   
    22.                    print("全局座標→視口座標" + camera.WorldToViewportPoint(obj.transform.position));               
    23.             }         
    24.       }   
    25. } 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.