您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > Unity3D的几种坐标系
WorldSpace(世界坐标):我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的。transform.position可以获得该位置坐标。该文章出自【狗刨学习网】ScreenSpace(屏幕坐标,鼠标坐标):以像素来定义的,以屏幕的左下角为(0,0)点,右上角为(Screen.width,Screen.height),Z的位置是以相机的世界单位来衡量的。注:鼠标位置坐标属于屏幕坐标,Input.mousePosition可以获得该位置坐标,手指触摸屏幕也为屏幕坐标,Input.GetTouch(0).position可以获得单个手指触摸屏幕坐标。ViewPortSpace(视口坐标):视口坐标是标准的和相对于相机的。相机的左下角为(0,0)点,右上角为(1,1)点,Z的位置是以相机的世界单位来衡量的。(用的不多,反正我暂时没有用到~呵呵~)绘制GUI界面的坐标系:这个坐标系与屏幕坐标系相似,不同的是该坐标系以屏幕的左上角为(0,0)点,右下角为(Screen.width,Screen.height)。LineRender坐标:以屏幕中心为原点,向上向右增加。世界坐标→屏幕坐标:camera.WorldToScreenPoint(transform.position);这样可以将世界坐标转换为屏幕坐标。其中camera为场景中的camera对象。屏幕坐标→视口坐标:camera.ScreenToViewportPoint(Input.GetTouch(0).position);这样可以将屏幕坐标转换为视口坐标。其中camera为场景中的camera对象。视口坐标→屏幕坐标:camera.ViewportToScreenPoint();视口坐标→世界坐标:camera.ViewportToWorldPoint();案例1——在鼠标点击的位置上绘制一张图片出来(关于绘制GUI界面坐标系与屏幕坐标系之间的关系)。1.usingUnityEngine;2.usingSystem.Collections;3.4.publicclassScreenToGUI:MonoBehaviour{5.6.//Usethisforinitialization7.voidStart(){8.9.}10.11.//Updateiscalledonceperframe12.voidUpdate(){13.14.}15.16.//图片17.publicTextureimg;18.//储存鼠标的位置坐标19.privateVector2pos;20.voidOnGUI()21.{22.//鼠标左击,获取当前鼠标的位置23.if(Input.GetMouseButton(0))24.{25.pos=Input.mousePosition;//屏幕坐标26.}27.//绘制图片,屏幕坐标和GUI界面坐标只在Y轴上方向相反,只要被Screen.height减就可以互相转换。28.GUI.DrawTexture(newRect(pos.x,Screen.height-pos.y,100,100),img);29.}30.31.}复制代码案例2——角色头顶的名字(世界坐标转GUI界面坐标)先世界坐标转屏幕坐标,再屏幕坐标转GUI界面坐标代码如下:1.usingUnityEngine;2.usingSystem.Collections;3.4.publicclassBlood:MonoBehaviour{5.publicstaticfloatScaleWidht=0f;6.publicstaticfloatScaleHeight=0f;7.privateRect_drawRect=newRect();8.publicfloatWidth=0f;9.publicfloatHeight=10f;10.publicconstfloatDesignStageWidth=800;11.publicconstfloatDesignStageHeight=480;12.13.publicVector2pos2;14.publicfloatsize_z;15.//Usethisforinitialization16.voidStart(){17.ScaleWidht=Screen.width/DesignStageWidth;18.ScaleHeight=Screen.height/DesignStageHeight;19.Height=2f;20.21.size_z=transform.gameObject.collider.bounds.size.z;22.}23.24.25.//Updateiscalledonceperframe26.voidUpdate(){27.//世界坐标转换到屏幕坐标28.print(transform.forward);29.pos2=Camera.main.WorldToScreenPoint(transform.position+transform.forward*(size_z/2));30.//计算角色头顶坐标31.pos2=newVector2(pos2.x,Screen.height-pos2.y-Height);32.33.34.35.//Vector3worldPosition=newVector3(transform.position.x,transform.position.y+Height,transform.position.z);36.//worldPosition=Camera.mainCamera.WorldToScreenPoint(worldPosition);37.//_drawRect=newRect((worldPosition.x-100*ScaleWidht)/ScaleWidht,(Screen.height-worldPosition.y-50*ScaleHeight)/ScaleHeight,200,50);38.}39.40.voidOnGUI()41.{42.//GUILayout.BeginArea(_drawRect);43.//GUILayout.Label(======哈哈======);44.//GUILayout.EndArea();45.46.GUI.Label(newRect(pos2.x,pos2.y,100,50),=BETTER=);47.}48.}复制代码案例3——类似屏幕解锁功能的实现(屏幕坐标转换为世界坐标)首先是创建LineRenderer。GameObject-CreateEmpty-更名为“LineRendererObj”,给LineRendererObj添加“LineRenderer”组件,Component-Effects-LineRenderer;将它的Positions的size设置为0接下来是代码touch.CS:1.2.usingUnityEngine;3.usingSystem.Collections;4.usingSystem.Collections.Generic;5.6.publicclasstouch:MonoBehaviour{7.privateEvente;8.9.publicTexture2DPoint;10.publicColorc1=Color.yellow;11.publicColorc2=Color.red;12.publicintlengthOfLineRenderer;13.publicGameObjectLineRendererPrefab;14.15.privateLineRendererlineRenderer;16.///summary17.///保存创建的LineRenderer18.////summary19.privateListLineRendererlineRendArray=newListLineRenderer();20.21.privateVector3screenPoint;22.privateVector3scanPos;23.24.privateColor[]color;25.26.///summary27.///记录宫格所在GUI位置28.////summary29.publicListRectAreaRect=newListRect();30.///summary31.///记录宫格中心点32.////summary33.publicListVector2CenterPointList=newListVector2();34.///summary35.///宫格标签36.////summary37.publicintRectFlag;38.///summary39.///记录正确的滑动顺序40.////summary41.publicListintKeyOrder=newListint();42.///summary43.///记录玩家滑动顺序44.////summary45.publicListintPlayerKeyOrder=newListint();46.47.///summary48.///判断开始鼠标位置是否可画49.////summary50.publicboolCheckStartRect=false;51.52.///summary53.///判断结束鼠标位置是否可画54.////summary55.publicboolCheckEndRect=false;56.57.///summary58.///行数59.////summary60.publicintRow=4;61.///summary62.///列数63.////summary64.publicintColumn=4;65.66.voidStart()67.{68.e=Event.current;69.70.scanPos=LineRendererPrefab.transform.position;71.lineRenderer=(LineRenderer)LineRendererPrefab.GetComponent(LineRenderer);72.lineRenderer.material=newMaterial(Shader.Find(Particles/Additive));73.lengthOfLineRenderer=0;74.lineRenderer.SetColors(c1,c2);75.lineRenderer.SetWidth(0.7F,0.7F);76.lineRenderer.SetVertexCount(0);77.78.color=newColor[8];79.color[0]=Color.yellow;80.color[1]=Color.blue;81.color[2]=Color.cyan;82.color[3]=Color.gray;83.color[4]=Color.green;84.color[5]=Color.grey;85.color[6]=Color.magenta;86.color[7]=Color.red;87.88.for(intRowCount=0;RowCountRow;RowCount++)89.{90.for(intcolumnCount=0;columnCountColumn;columnCount++)91.{92.RectIconRect=newRect(columnCount*Screen.width/Column+Screen.width/Column/2-Point.width/2,RowCount*Screen.height/Row+Screen.
本文标题:Unity3D的几种坐标系
链接地址:https://www.777doc.com/doc-4044964 .html