zoukankan      html  css  js  c++  java
  • 转:Unity3D的四种坐标系

    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界面坐标系与屏幕坐标系之间的关系)
    using UnityEngine;
     using System.Collections;
     public class test : MonoBehaviour   {        
          //图片  
        public Texture img;    
         //储存鼠标的位置坐标    
        private Vector2 pos;   
         void OnGUI()   
          {           
              //鼠标左击,获取当前鼠标的位置       
           if (Input.GetMouseButton(0))         
              {       
                   pos = Input.mousePosition;           
               }        
                 //绘制图片      
            GUI.DrawTexture(new Rect(pos.x,Screen.height - pos.y,100,100), img);       
          }   
    }

    案例2——坐标显示和坐标转换(这个是触摸方面的。如果没有触摸屏,那就将那个if去掉吧!)

    using UnityEngine;   
    using System.Collections;   
    public class test: MonoBehaviour   {       
         //场景的相机,拖放进来      
        public Camera camera;        
         //场景的物体      
        private GameObject obj;    
          void Start()      
           {        
                //初始化      
            obj = GameObject.Find("Plane");       
            }       
             void Update ()     
             {            
                   //有触摸         
              if (Input.touchCount > 0)            
                   {                 
                       print("世界坐标" + obj.transform.position);                  
                        print("屏幕坐标" + Input.GetTouch(0).position);                  
                        print("世界坐标→屏幕坐标" + camera.WorldToScreenPoint(obj.transform.position));                  
                        print("屏幕坐标→视口坐标" + camera.ScreenToViewportPoint(Input.GetTouch(0).position));                 
                       print("世界坐标→视口坐标" + camera.WorldToViewportPoint(obj.transform.position));             
                }       
          } 
    }
    

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

  • 相关阅读:
    异或运算实现两数交换
    安装Keepalived namespaces.c:187: error: ‘SYS_setns’ undeclared (first use in this function)
    安装keepalived OpenSSL is not properly installed on your system. !!!
    jackson json转实体对象 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
    solr集群Server refused connection at: http://127.0.0.1:6060/solr/ego 注册zookeepr自动使用内网ip
    Solr java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to this MySQL server
    Jackson中@JsonProperty等常用注解
    java.lang.ClassNotFoundException: XXX (no security manager: RMI class loader disabled)
    ActiveMQ学习总结------Spring整合ActiveMQ 04
    为什么嵌入式开发用memcpy()而不用strncpy()
  • 原文地址:https://www.cnblogs.com/zhibolife/p/3636836.html
Copyright © 2011-2022 走看看