Unity3D的幾種坐標(biāo)系.doc_第1頁(yè)
Unity3D的幾種坐標(biāo)系.doc_第2頁(yè)
Unity3D的幾種坐標(biāo)系.doc_第3頁(yè)
Unity3D的幾種坐標(biāo)系.doc_第4頁(yè)
Unity3D的幾種坐標(biāo)系.doc_第5頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

World Space(世界坐標(biāo)):我們?cè)趫?chǎng)景中添加物體(如:Cube),他們都是以世界坐標(biāo)顯示在場(chǎng)景中的。transform.position可以獲得該位置坐標(biāo)。該文章出自【狗刨學(xué)習(xí)網(wǎng)】Screen Space(屏幕坐標(biāo),鼠標(biāo)坐標(biāo)):以像素來(lái)定義的,以屏幕的左下角為(0,0)點(diǎn),右上角為(Screen.width,Screen.height),Z的位置是以相機(jī)的世界單位來(lái)衡量的。注:鼠標(biāo)位置坐標(biāo)屬于屏幕坐標(biāo),Input.mousePosition可以獲得該位置坐標(biāo),手指觸摸屏幕也為屏幕坐標(biāo),Input.GetTouch(0).position可以獲得單個(gè)手指觸摸屏幕坐標(biāo)。ViewPort Space(視口坐標(biāo)):視口坐標(biāo)是標(biāo)準(zhǔn)的和相對(duì)于相機(jī)的。相機(jī)的左下角為(0,0)點(diǎn),右上角為(1,1)點(diǎn),Z的位置是以相機(jī)的世界單位來(lái)衡量的。(用的不多,反正我暫時(shí)沒(méi)有用到呵呵)繪制GUI界面的坐標(biāo)系:這個(gè)坐標(biāo)系與屏幕坐標(biāo)系相似,不同的是該坐標(biāo)系以屏幕的左上角為(0,0)點(diǎn),右下角為(Screen.width,Screen.height)。 LineRender坐標(biāo):以屏幕中心為原點(diǎn),向上向右增加。世界坐標(biāo)屏幕坐標(biāo):camera.WorldToScreenPoint(transform.position);這樣可以將世界坐標(biāo)轉(zhuǎn)換為屏幕坐標(biāo)。其中camera為場(chǎng)景中的camera對(duì)象。屏幕坐標(biāo)視口坐標(biāo):camera.ScreenToViewportPoint(Input.GetTouch(0).position);這樣可以將屏幕坐標(biāo)轉(zhuǎn)換為視口坐標(biāo)。其中camera為場(chǎng)景中的camera對(duì)象。視口坐標(biāo)屏幕坐標(biāo):camera.ViewportToScreenPoint(); 視口坐標(biāo)世界坐標(biāo):camera.ViewportToWorldPoint(); 案例1在鼠標(biāo)點(diǎn)擊的位置上繪制一張圖片出來(lái)(關(guān)于繪制GUI界面坐標(biāo)系與屏幕坐標(biāo)系之間的關(guān)系)。1. using UnityEngine;2. using System.Collections;3.4. public class ScreenToGUI : MonoBehaviour 5.6. / Use this for initialization7. void Start () 8. 9. 10. 11. / Update is called once per frame12. void Update () 13. 14. 15. 16. /圖片17. public Texture img; 18. /儲(chǔ)存鼠標(biāo)的位置坐標(biāo) 19. private Vector2 pos;20. void OnGUI()21. 22. /鼠標(biāo)左擊,獲取當(dāng)前鼠標(biāo)的位置 23. if (Input.GetMouseButton(0)24. 25. pos = Input.mousePosition; /屏幕坐標(biāo)26. 27. /繪制圖片,屏幕坐標(biāo)和GUI界面坐標(biāo)只在Y軸上方向相反,只要被Screen.height減就可以互相轉(zhuǎn)換。 28. GUI.DrawTexture(new Rect(pos.x, Screen.height - pos.y, 100, 100), img);29. 30.31. 復(fù)制代碼 案例2角色頭頂?shù)拿郑ㄊ澜缱鴺?biāo)轉(zhuǎn)GUI界面坐標(biāo))先世界坐標(biāo)轉(zhuǎn)屏幕坐標(biāo),再屏幕坐標(biāo)轉(zhuǎn)GUI界面坐標(biāo)代碼如下:1. using UnityEngine;2. using System.Collections;3.4. public class Blood : MonoBehaviour 5. public static float ScaleWidht = 0f;6. public static float ScaleHeight = 0f;7. private Rect _drawRect = new Rect();8. public float Width = 0f;9. public float Height = 10f;10. public const float DesignStageWidth = 800;11. public const float DesignStageHeight = 480;12.13. public Vector2 pos2;14. public float size_z;15. / Use this for initialization16. void Start () 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. / Update is called once per frame26. void Update () 27. /世界坐標(biāo)轉(zhuǎn)換到屏幕坐標(biāo)28. print(transform.forward);29. pos2 = Camera.main.WorldToScreenPoint(transform.position + transform.forward * (size_z / 2);30. /計(jì)算角色頭頂坐標(biāo)31. pos2 = new Vector2(pos2.x, Screen.height- pos2.y - Height);32.33.34.35. /Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + Height, transform.position.z);36. /worldPosition = Camera.mainCamera.WorldToScreenPoint(worldPosition);37. /_drawRect = new Rect(worldPosition.x - 100 * ScaleWidht) / ScaleWidht, (Screen.height - worldPosition.y - 50 * ScaleHeight) / ScaleHeight, 200, 50);38. 39.40. void OnGUI()41. 42. /GUILayout.BeginArea(_drawRect);43. / GUILayout.Label(=哈哈=);44. /GUILayout.EndArea();45.46. GUI.Label(new Rect(pos2.x, pos2.y, 100, 50), =BETTER=);47. 48. 復(fù)制代碼 案例3類似屏幕解鎖功能的實(shí)現(xiàn)(屏幕坐標(biāo)轉(zhuǎn)換為世界坐標(biāo)) 首先是創(chuàng)建LineRenderer。GameObject - Create Empty -更名為“LineRendererObj”,給LineRendererObj添加“Line Renderer”組件,Component -Effects -Line Renderer;將它的Positions 的size 設(shè)置為0 接下來(lái)是代碼touch.CS:1.2. using UnityEngine;3. using System.Collections;4. using System.Collections.Generic;5.6. public class touch : MonoBehaviour 7. private Event e;8.9. public Texture2D Point;10. public Color c1 = Color.yellow;11. public Color c2 = Color.red;12. public int lengthOfLineRenderer;13. public GameObject LineRendererPrefab;14.15. private LineRenderer lineRenderer;16. / 17. / 保存創(chuàng)建的Line Renderer18. / 19. private List lineRendArray =new List();20.21. private Vector3 screenPoint;22. private Vector3 scanPos;23.24. private Color color;25.26. / 27. / 記錄宮格所在GUI位置28. / 29. public List AreaRect = new List();30. / 31. / 記錄宮格中心點(diǎn)32. / 33. public List CenterPointList = new List();34. / 35. / 宮格標(biāo)簽36. / 37. public int RectFlag;38. / 39. / 記錄正確的滑動(dòng)順序40. / 41. public List KeyOrder = new List();42. / 43. / 記錄玩家滑動(dòng)順序44. / 45. public List PlayerKeyOrder = new List();46.47. / 48. / 判斷開(kāi)始鼠標(biāo)位置是否可畫(huà)49. / 50. public bool CheckStartRect=false;51.52. / 53. / 判斷結(jié)束鼠標(biāo)位置是否可畫(huà)54. / 55. public bool CheckEndRect = false;56.57. / 58. / 行數(shù)59. / 60. public int Row = 4;61. / 62. / 列數(shù)63. / 64. public int Column = 4;65.66. void Start()67. 68. e = Event.current;69.70. scanPos = LineRendererPrefab.transform.position;71. lineRenderer = (LineRenderer)LineRendererPrefab.GetComponent(LineRenderer);72. lineRenderer.material = new Material(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 = new Color8;79. color0 = Color.yellow;80. color1 = Color.blue;81. color2 = Color.cyan;82. color3 = Color.gray;83. color4 = Color.green;84. color5 = Color.grey;85. color6 = Color.magenta;86. color7 = Color.red;87.88. for (int RowCount = 0; RowCount Row; RowCount+)89. 90. for (int columnCount = 0; columnCount Column; columnCount+)91. 92. Rect IconRect = new Rect(columnCount * Screen.width / Column + Screen.width / Column / 2 - Point.width / 2, RowCount * Screen.height / Row + Screen.height / Row / 2 - Point.height / 2, Point.width, Point.height);93. AreaRect.Add(IconRect);94.95. Vector2 CenterP = IconRect.center;/得到每個(gè)的中心點(diǎn)96. CenterPointList.Add(CenterP);97. 98. 99. 100.101. void OnGUI()102. 103. e = Event.current;104. for (int RowCount = 0; RowCount Row; RowCount+)105. 106. for (int columnCount = 0; columnCount Column; columnCount+)107. 108. Rect IconRect = new Rect(columnCount * Screen.width / Column + Screen.width / Column / 2 - Point.width / 2, RowCount * Screen.height / Row + Screen.height / Row / 2 - Point.height / 2, Point.width, Point.height);109. GUI.Label(IconRect, Point);110. 111. 112. 113.114. void Update()115. 116. if (e != null)117. 118. if (e.type = EventType.MouseDown)119. 120. for (int i = 0; i AreaRect.Count; i+)121. 122. if (AreaRecti.Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)123. 124. CheckStartRect = true;125. print(Contains);126. PlayerKeyOrder.Add(i);127. RectFlag = i;128. break;129. 130. else131. 132. CheckStartRect = false;133. 134. 135.136. if (CheckStartRect)137. 138. print(MouseDown_);139.140. /Vector3 curPosition = mousePToLineRendererP();141. Vector3 curPosition = centerPToLineRendererP(RectFlag);142. GameObject newObj;143. newObj = (GameObject)Instantiate(LineRendererPrefab, LineRendererPrefab.transform.position, LineRendererPrefab.transform.rotation);144. lineRenderer = (LineRenderer)newObj.GetComponent(LineRenderer);145.146. int n = Random.Range(1, 8);147. c1 = colorn - 1;148. n = Random.Range(1, 8);149. c2 = colorn - 1;150. lineRenderer.SetColors(c1, c2);151.152. lineRenderer.SetVertexCount(1);153. lineRenderer.SetWidth(0.7F, 0.7F);154. lineRenderer.SetPosition(0, curPosition);155. lineRendArray.Add(lineRenderer);156. lengthOfLineRenderer+;157. 158. 159.160. if (e.type = EventType.MouseDrag&CheckStartRect)161. 162. print(MouseDrag_);163. Vector3 curPosition = mousePToLineRendererP();164. DrawRenderLine(lineRendArraylengthOfLineRenderer - 1, curPosition);165. 166.167. if (e.type = EventType.MouseUp & CheckStartRect)168. 169. for (int i = 0; i AreaRect.Count; i+)170. 171. if (AreaRecti.Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)172. 173. CheckEndRect = true;174. PlayerKeyOrder.Add(i);175. RectFlag = i;176. print(EndContains);177. break;178. 179. else180. 181. CheckEndRect = false;182. 183. 184.185. if (CheckEndRect)186. 187. Vector3 curPosition = centerPToLineRendererP(RectFlag);188. DrawRenderLine(lineRendArraylengthOfLineRenderer - 1, curPosition);189. 190. else191. 192. PlayerKeyOrder.RemoveAt(PlayerKeyOrder.Count - 1);193. Destroy(lineRendArraylengthOfLineRenderer - 1.gameObject);194. /lengthOfLineRenderer-;195. 196.197. 198. 199. 200.201. void DrawRenderLine(LineRenderer line, Vector3 vect3)202. 203. Vector3 newPos = vect3;204. line.SetVertexCount(2);205.206. line.SetPosition(1, newPos);207. print(new point: + newPos);208. 209.210. /public Vector2 RectCenterPoint(Rect AreaRect) /計(jì)算一個(gè)Rect的中心點(diǎn)211. /212. / Vector2 CenterPoint=Vector2.zero;213. / print(Rect:+AreaRect);214. / CenterPoint.x=AreaRect.xMin+AreaRect.width/2;215. 216. / CenterPoint.y=AreaRect.yMin+AreaRect.height/2;217. / print(CenterPoint:+CenterPoint);218. / return CenterPoint;219. /220.221. / 222. / 鼠標(biāo)所在位置轉(zhuǎn)換為L(zhǎng)ineRenderer的位置223. / 2

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論