- World Space(世界坐標):我們在場景中添加物體(如:Cube),他們都是以世界坐標顯示在場景中的。transform.position可以獲得該位置坐標。
-
Screen Space(屏幕坐標,鼠標坐標):以像素來定義的,以屏幕的左下角為(0,0)點,右上角為(Screen.width,Screen.height),Z的位置是以相機的世界單位來衡量的。注:鼠標位置坐標屬於屏幕坐標,Input.mousePosition可以獲得該位置坐標,手指觸摸屏幕也為屏幕坐標,Input.GetTouch(0).position可以獲得單個手指觸摸屏幕坐標。
-
ViewPort Space(視口坐標):視口坐標是標准的和相對於相機的。相機的左下角為(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界面坐標系與屏幕坐標系之間的關系)。
using UnityEngine; using System.Collections; public class ScreenToGUI : MonoBehaviour { // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } //圖片
public Texture img; //儲存鼠標的位置坐標
private Vector2 pos; void OnGUI() { //鼠標左擊,獲取當前鼠標的位置
if (Input.GetMouseButton(0)) { pos = Input.mousePosition; //屏幕坐標
} //繪制圖片,屏幕坐標和GUI界面坐標只在Y軸上方向相反,只要被Screen.height減就可以互相轉換。
GUI.DrawTexture(new Rect(pos.x, Screen.height - pos.y, 100, 100), img); } }
案例2——角色頭頂的名字(世界坐標轉GUI界面坐標)
先世界坐標轉屏幕坐標,再屏幕坐標轉GUI界面坐標
代碼如下:
using UnityEngine; using System.Collections; public class Blood : MonoBehaviour { public static float ScaleWidht = 0f; public static float ScaleHeight = 0f; private Rect _drawRect = new Rect(); public float Width = 0f; public float Height = 10f; public const float DesignStageWidth = 800; public const float DesignStageHeight = 480; public Vector2 pos2; public float size_z; // Use this for initialization
void Start () { ScaleWidht = Screen.width / DesignStageWidth; ScaleHeight = Screen.height / DesignStageHeight; Height = 2f; size_z = transform.gameObject.collider.bounds.size.z; } // Update is called once per frame
void Update () { //世界坐標轉換到屏幕坐標
print(transform.forward); pos2 = Camera.main.WorldToScreenPoint(transform.position + transform.forward * (size_z / 2)); //計算角色頭頂坐標
pos2 = new Vector2(pos2.x, Screen.height - pos2.y - Height); //Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + Height, transform.position.z); //worldPosition = Camera.mainCamera.WorldToScreenPoint(worldPosition); //_drawRect = new Rect((worldPosition.x - 100 * ScaleWidht) / ScaleWidht, (Screen.height - worldPosition.y - 50 * ScaleHeight) / ScaleHeight, 200, 50);
} void OnGUI() { //GUILayout.BeginArea(_drawRect); // GUILayout.Label("======哈哈======"); //GUILayout.EndArea();
GUI.Label(new Rect(pos2.x, pos2.y, 100, 50), "=BETTER="); } }
案例3——類似屏幕解鎖功能的實現(屏幕坐標轉換為世界坐標)
首先是創建LineRenderer。GameObject -> Create Empty ->更名為“LineRendererObj”,
給LineRendererObj添加“Line Renderer”組件,Component ->Effects ->Line Renderer;
將它的Positions 的size 設置為0
接下來是代碼touch.CS:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class touch : MonoBehaviour { private Event e; public Texture2D Point; public Color c1 = Color.yellow; public Color c2 = Color.red; public int lengthOfLineRenderer; public GameObject LineRendererPrefab; private LineRenderer lineRenderer; /// <summary>
/// 保存創建的Line Renderer /// </summary>
private List<LineRenderer> lineRendArray =new List<LineRenderer>(); private Vector3 screenPoint; private Vector3 scanPos; private Color[] color; /// <summary>
/// 記錄宮格所在GUI位置 /// </summary>
public List<Rect> AreaRect = new List<Rect>(); /// <summary>
/// 記錄宮格中心點 /// </summary>
public List<Vector2> CenterPointList = new List<Vector2>(); /// <summary>
/// 宮格標簽 /// </summary>
public int RectFlag; /// <summary>
/// 記錄正確的滑動順序 /// </summary>
public List<int> KeyOrder = new List<int>(); /// <summary>
/// 記錄玩家滑動順序 /// </summary>
public List<int> PlayerKeyOrder = new List<int>(); /// <summary>
/// 判斷開始鼠標位置是否可畫 /// </summary>
public bool CheckStartRect=false; /// <summary>
/// 判斷結束鼠標位置是否可畫 /// </summary>
public bool CheckEndRect = false; /// <summary>
/// 行數 /// </summary>
public int Row = 4; /// <summary>
/// 列數 /// </summary>
public int Column = 4; void Start() { e = Event.current; scanPos = LineRendererPrefab.transform.position; lineRenderer = (LineRenderer)LineRendererPrefab.GetComponent("LineRenderer"); lineRenderer.material = new Material(Shader.Find("Particles/Additive")); lengthOfLineRenderer = 0; lineRenderer.SetColors(c1, c2); lineRenderer.SetWidth(0.7F, 0.7F); lineRenderer.SetVertexCount(0); color = new Color[8]; color[0] = Color.yellow; color[1] = Color.blue; color[2] = Color.cyan; color[3] = Color.gray; color[4] = Color.green; color[5] = Color.grey; color[6] = Color.magenta; color[7] = Color.red; for (int RowCount = 0; RowCount < Row; RowCount++) { for (int columnCount = 0; columnCount < Column; columnCount++) { 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); AreaRect.Add(IconRect); Vector2 CenterP = IconRect.center;//得到每個的中心點
CenterPointList.Add(CenterP); } } } void OnGUI() { e = Event.current; for (int RowCount = 0; RowCount < Row; RowCount++) { for (int columnCount = 0; columnCount < Column; columnCount++) { 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); GUI.Label(IconRect, Point); } } } void Update() { if (e != null) { if (e.type == EventType.MouseDown) { for (int i = 0; i < AreaRect.Count; i++) { if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z))) { CheckStartRect = true; print("Contains"); PlayerKeyOrder.Add(i); RectFlag = i; break; } else { CheckStartRect = false; } } if (CheckStartRect) { print("MouseDown_____"); //Vector3 curPosition = mousePToLineRendererP();
Vector3 curPosition = centerPToLineRendererP(RectFlag); GameObject newObj; newObj = (GameObject)Instantiate(LineRendererPrefab, LineRendererPrefab.transform.position, LineRendererPrefab.transform.rotation); lineRenderer = (LineRenderer)newObj.GetComponent("LineRenderer"); int n = Random.Range(1, 8); c1 = color[n - 1]; n = Random.Range(1, 8); c2 = color[n - 1]; lineRenderer.SetColors(c1, c2); lineRenderer.SetVertexCount(1); lineRenderer.SetWidth(0.7F, 0.7F); lineRenderer.SetPosition(0, curPosition); lineRendArray.Add(lineRenderer); lengthOfLineRenderer++; } } if (e.type == EventType.MouseDrag&&CheckStartRect) { print("MouseDrag_____"); Vector3 curPosition = mousePToLineRendererP(); DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition); } if (e.type == EventType.MouseUp && CheckStartRect) { for (int i = 0; i < AreaRect.Count; i++) { if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z))) { CheckEndRect = true; PlayerKeyOrder.Add(i); RectFlag = i; print("EndContains"); break; } else { CheckEndRect = false; } } if (CheckEndRect) { Vector3 curPosition = centerPToLineRendererP(RectFlag); DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition); } else { PlayerKeyOrder.RemoveAt(PlayerKeyOrder.Count - 1); Destroy(lineRendArray[lengthOfLineRenderer - 1].gameObject); //lengthOfLineRenderer--;
} } } } void DrawRenderLine(LineRenderer line, Vector3 vect3) { Vector3 newPos = vect3; line.SetVertexCount(2); line.SetPosition(1, newPos); print("new point: " + newPos); } //public Vector2 RectCenterPoint(Rect AreaRect) //計算一個Rect的中心點 //{ // Vector2 CenterPoint=Vector2.zero; // print("Rect:"+AreaRect); // CenterPoint.x=AreaRect.xMin+AreaRect.width/2; // CenterPoint.y=AreaRect.yMin+AreaRect.height/2; // print("CenterPoint:"+CenterPoint); // return CenterPoint; //}
/// <summary>
/// 鼠標所在位置轉換為LineRenderer的位置 /// </summary>
/// <returns></returns>
public Vector3 mousePToLineRendererP() { screenPoint = Camera.main.WorldToScreenPoint(scanPos); Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); print("curScreenPoint: " + curScreenPoint); print("curPosition: " + curPosition); return curPosition; } /// <summary>
/// 鼠標所在區域的中心點轉換為LineRenderer的位置 /// </summary>
/// <returns></returns>
public Vector3 centerPToLineRendererP(int Flag) { screenPoint = Camera.main.WorldToScreenPoint(scanPos); Vector3 curScreenPoint = new Vector3(CenterPointList[Flag].x,Screen.height-CenterPointList[Flag].y,screenPoint.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); print("curScreenPoint: " + curScreenPoint); print("curPosition: " + curPosition); return curPosition; } }
把touch.CS綁定在Camera上,設置如下:
運行后可以任意點間連線,如圖: