首页 技术 正文
技术 2022年11月14日
0 收藏 577 点赞 4,266 浏览 1656 个字

来自:http://blog.csdn.net/luxiaoyu_sdc/article/details/13168497

1, World Space(世界坐标):

我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的。transform.position可以获得该位置坐标。

2, Screen Space(屏幕坐标):

以像素来定义的,以屏幕的左下角为(0,0)点,右上角为(Screen.width,Screen.height),Z的位置是以相机的世界单位来衡量的。

注:鼠标位置坐标属于屏幕坐标,Input.mousePosition可以获得该位置坐标,手指触摸屏幕也为屏幕坐标,Input.GetTouch(0).position可以获得单个手指触摸屏幕坐标。

ViewPort Space(视口坐标):视口坐标是标准的和相对于相机的。相机的左下角为(0,0)点,右上角为(1,1)点,Z的位置是以相机的世界单位来衡量的。

3, 绘制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()) { pos = Input.mousePosition; } //绘制图片
GUI.DrawTexture(new Rect(pos.x,Screen.height - pos.y,,), 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 > )                             {                                      print("世界坐标" + obj.transform.position);                                        print("屏幕坐标" + Input.GetTouch().position);                                        print("世界坐标→屏幕坐标" + camera.WorldToScreenPoint(obj.transform.position));                                        print("屏幕坐标→视口坐标" + camera.ScreenToViewportPoint(Input.GetTouch().position));                                      print("世界坐标→视口坐标" + camera.WorldToViewportPoint(obj.transform.position));                           }               }    }  
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848