Unity3D的幾種座標系



  • 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界面座標系與屏幕座標系之間的關係)。
  1. using UnityEngine;
  2. using System.Collections;

  3. public class ScreenToGUI : MonoBehaviour {

  4.     // Use this for initialization
  5.     void Start () {
  6.     
  7.     }
  8.     
  9.     // Update is called once per frame
  10.     void Update () {
  11.     
  12.     }
  13.   
  14.       //圖片  
  15.     public Texture img;    
  16.      //儲存鼠標的位置座標    
  17.     private Vector2 pos;
  18.     void OnGUI()
  19.     {
  20.         //鼠標左擊,獲取當前鼠標的位置       
  21.         if (Input.GetMouseButton(0))
  22.         {
  23.             pos = Input.mousePosition; //屏幕座標
  24.         }
  25.         //繪製圖片,屏幕座標和GUI界面座標只在Y軸上方向相反,只要被Screen.height減就可以互相轉換。      
  26.         GUI.DrawTexture(new Rect(pos.x, Screen.height - pos.y, 100, 100), img);
  27.     }

  28. }
複製代碼

             

    案例2——角色頭頂的名字(世界座標轉GUI界面座標)先世界座標轉屏幕座標,再屏幕座標轉GUI界面座標

代碼如下:
  1. using UnityEngine;
  2. using System.Collections;

  3. public class Blood : MonoBehaviour {
  4.     public static float ScaleWidht = 0f;
  5.     public static float ScaleHeight = 0f;
  6.     private Rect _drawRect = new Rect();
  7.     public float Width = 0f;
  8.     public float Height = 10f;
  9.     public const float DesignStageWidth = 800;
  10.     public const float DesignStageHeight = 480;

  11.     public Vector2 pos2;
  12.     public float size_z;
  13.     // Use this for initialization
  14.     void Start () {
  15.         ScaleWidht = Screen.width / DesignStageWidth;
  16.         ScaleHeight = Screen.height / DesignStageHeight;
  17.         Height = 2f;

  18.         size_z = transform.gameObject.collider.bounds.size.z;
  19.     }


  20.     // Update is called once per frame
  21.     void Update () {
  22.         //世界座標轉換到屏幕座標
  23.         print(transform.forward);
  24.         pos2 = Camera.main.WorldToScreenPoint(transform.position + transform.forward * (size_z / 2)); 
  25.         //計算角色頭頂座標
  26.         pos2 = new Vector2(pos2.x, Screen.height  - pos2.y - Height); 



  27.         //Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + Height, transform.position.z);
  28.         //worldPosition = Camera.mainCamera.WorldToScreenPoint(worldPosition);
  29.         //_drawRect = new Rect((worldPosition.x - 100 * ScaleWidht) / ScaleWidht, (Screen.height - worldPosition.y - 50 * ScaleHeight) / ScaleHeight, 200, 50);
  30.     }

  31.     void OnGUI()
  32.     {
  33.         //GUILayout.BeginArea(_drawRect);
  34.         //    GUILayout.Label("======哈哈======");
  35.         //GUILayout.EndArea();

  36.         GUI.Label(new Rect(pos2.x, pos2.y, 100, 50), "=BETTER=");
  37.     }
  38. }
複製代碼


   案例3——類似屏幕解鎖功能的實現(屏幕座標轉換爲世界座標)

   首先是創建LineRenderer。GameObject -> Create Empty ->更名爲“LineRendererObj”,給LineRendererObj添加“Line Renderer”組件,Component ->Effects ->Line Renderer;將它的Positions 的size 設置爲0

       
接下來是代碼touch.CS:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class touch : MonoBehaviour {
  5.     private Event e;

  6.     public Texture2D Point;
  7.     public Color c1 = Color.yellow;
  8.     public Color c2 = Color.red;
  9.     public int lengthOfLineRenderer;
  10.     public GameObject LineRendererPrefab;

  11.     private LineRenderer lineRenderer;
  12.     /// <summary>
  13.     /// 保存創建的Line Renderer
  14.     /// </summary>
  15.     private List<LineRenderer> lineRendArray =new List<LineRenderer>();

  16.     private Vector3 screenPoint;
  17.     private Vector3 scanPos;

  18.     private Color[] color;

  19.     /// <summary>
  20.     /// 記錄宮格所在GUI位置
  21.     /// </summary>
  22.     public List<Rect> AreaRect = new List<Rect>();
  23.     /// <summary>
  24.     /// 記錄宮格中心點
  25.     /// </summary>
  26.     public List<Vector2> CenterPointList = new List<Vector2>();
  27.     /// <summary>
  28.     /// 宮格標籤
  29.     /// </summary>
  30.     public int RectFlag;
  31.     /// <summary>
  32.     /// 記錄正確的滑動順序
  33.     /// </summary>
  34.     public List<int> KeyOrder = new List<int>();
  35.     /// <summary>
  36.     /// 記錄玩家滑動順序
  37.     /// </summary>
  38.     public List<int> PlayerKeyOrder = new List<int>();

  39.     /// <summary>
  40.     /// 判斷開始鼠標位置是否可畫
  41.     /// </summary>
  42.     public bool CheckStartRect=false;

  43.     /// <summary>
  44.     /// 判斷結束鼠標位置是否可畫
  45.     /// </summary>
  46.     public bool CheckEndRect = false;

  47.     /// <summary>
  48.     /// 行數
  49.     /// </summary>
  50.     public int Row = 4;
  51.     /// <summary>
  52.     /// 列數
  53.     /// </summary>
  54.     public int Column = 4;

  55.     void Start()
  56.     {
  57.         e = Event.current;

  58.         scanPos = LineRendererPrefab.transform.position;
  59.         lineRenderer = (LineRenderer)LineRendererPrefab.GetComponent("LineRenderer");
  60.         lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
  61.         lengthOfLineRenderer = 0;
  62.         lineRenderer.SetColors(c1, c2);
  63.         lineRenderer.SetWidth(0.7F, 0.7F);
  64.         lineRenderer.SetVertexCount(0);

  65.         color = new Color[8];
  66.         color[0] = Color.yellow;
  67.         color[1] = Color.blue;
  68.         color[2] = Color.cyan;
  69.         color[3] = Color.gray;
  70.         color[4] = Color.green;
  71.         color[5] = Color.grey;
  72.         color[6] = Color.magenta;
  73.         color[7] = Color.red;

  74.         for (int RowCount = 0; RowCount < Row; RowCount++)
  75.         {
  76.             for (int columnCount = 0; columnCount < Column; columnCount++)
  77.             {
  78.                 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);
  79.                 AreaRect.Add(IconRect);

  80.                 Vector2 CenterP = IconRect.center;//得到每個的中心點
  81.                 CenterPointList.Add(CenterP);
  82.             }
  83.         }
  84.     }

  85.     void OnGUI()
  86.     {
  87.         e = Event.current;
  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.                 GUI.Label(IconRect, Point);
  94.             }
  95.         }
  96.     }

  97.     void Update()
  98.     {
  99.         if (e != null)
  100.         {
  101.             if (e.type == EventType.MouseDown)
  102.             {
  103.                 for (int i = 0; i < AreaRect.Count; i++)
  104.                 {
  105.                     if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)))
  106.                     {
  107.                         CheckStartRect = true;
  108.                         print("Contains");
  109.                         PlayerKeyOrder.Add(i);
  110.                         RectFlag = i;
  111.                         break;
  112.                     }
  113.                     else
  114.                     {
  115.                         CheckStartRect = false;
  116.                     }
  117.                 }

  118.                 if (CheckStartRect)
  119.                 {
  120.                     print("MouseDown_____");

  121.                     //Vector3 curPosition = mousePToLineRendererP(); 
  122.                     Vector3 curPosition = centerPToLineRendererP(RectFlag);
  123.                     GameObject newObj;
  124.                     newObj = (GameObject)Instantiate(LineRendererPrefab, LineRendererPrefab.transform.position, LineRendererPrefab.transform.rotation);
  125.                     lineRenderer = (LineRenderer)newObj.GetComponent("LineRenderer");

  126.                     int n = Random.Range(1, 8);
  127.                     c1 = color[n - 1];
  128.                     n = Random.Range(1, 8);
  129.                     c2 = color[n - 1];
  130.                     lineRenderer.SetColors(c1, c2);

  131.                     lineRenderer.SetVertexCount(1);
  132.                     lineRenderer.SetWidth(0.7F, 0.7F);
  133.                     lineRenderer.SetPosition(0, curPosition);
  134.                     lineRendArray.Add(lineRenderer);
  135.                     lengthOfLineRenderer++;
  136.                 }
  137.             }

  138.             if (e.type == EventType.MouseDrag&&CheckStartRect)
  139.             {
  140.                 print("MouseDrag_____");
  141.                 Vector3 curPosition = mousePToLineRendererP(); 
  142.                 DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition);
  143.             }

  144.             if (e.type == EventType.MouseUp && CheckStartRect)
  145.             {
  146.                 for (int i = 0; i < AreaRect.Count; i++)
  147.                 {
  148.                     if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)))
  149.                     {
  150.                         CheckEndRect = true;
  151.                         PlayerKeyOrder.Add(i);
  152.                         RectFlag = i;
  153.                         print("EndContains");
  154.                         break;
  155.                     }
  156.                     else
  157.                     {
  158.                         CheckEndRect = false;
  159.                     }
  160.                 }

  161.                 if (CheckEndRect)
  162.                 {
  163.                     Vector3 curPosition = centerPToLineRendererP(RectFlag);
  164.                     DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition);
  165.                 }
  166.                 else
  167.                 {
  168.                     PlayerKeyOrder.RemoveAt(PlayerKeyOrder.Count - 1);
  169.                     Destroy(lineRendArray[lengthOfLineRenderer - 1].gameObject);
  170.                     //lengthOfLineRenderer--;
  171.                 }

  172.             }
  173.         }        
  174.     }

  175.     void DrawRenderLine(LineRenderer line, Vector3 vect3)
  176.     {
  177.         Vector3 newPos = vect3;
  178.         line.SetVertexCount(2);

  179.         line.SetPosition(1, newPos);
  180.         print("new point: " + newPos);
  181.     }

  182.     //public Vector2 RectCenterPoint(Rect AreaRect)       //計算一個Rect的中心點
  183.     //{
  184.     //    Vector2 CenterPoint=Vector2.zero;
  185.     //    print("Rect:"+AreaRect);
  186.     //    CenterPoint.x=AreaRect.xMin+AreaRect.width/2;
  187.         
  188.     //    CenterPoint.y=AreaRect.yMin+AreaRect.height/2;
  189.     //    print("CenterPoint:"+CenterPoint);
  190.     //    return CenterPoint;
  191.     //}

  192.     /// <summary>
  193.     /// 鼠標所在位置轉換爲LineRenderer的位置
  194.     /// </summary>
  195.     /// <returns></returns>
  196.     public Vector3 mousePToLineRendererP()      
  197.     {
  198.         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
  199.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  200.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
  201.         print("curScreenPoint: " + curScreenPoint);
  202.         print("curPosition: " + curPosition);
  203.         return curPosition;
  204.     }

  205.     /// <summary>
  206.     /// 鼠標所在區域的中心點轉換爲LineRenderer的位置
  207.     /// </summary>
  208.     /// <returns></returns>
  209.     public Vector3 centerPToLineRendererP(int Flag)
  210.     {
  211.         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
  212.         Vector3 curScreenPoint = new Vector3(CenterPointList[Flag].x,Screen.height-CenterPointList[Flag].y,screenPoint.z);
  213.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
  214.         print("curScreenPoint: " + curScreenPoint);
  215.         print("curPosition: " + curPosition);
  216.         return curPosition;
  217.     }
  218. }
複製代碼

   把touch.CS綁定在Camera上,設置如下:

       

      運行後可以任意點間連線,如圖:

      

  • 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界面座標系與屏幕座標系之間的關係)。
  1. using UnityEngine;
  2. using System.Collections;

  3. public class ScreenToGUI : MonoBehaviour {

  4.     // Use this for initialization
  5.     void Start () {
  6.     
  7.     }
  8.     
  9.     // Update is called once per frame
  10.     void Update () {
  11.     
  12.     }
  13.   
  14.       //圖片  
  15.     public Texture img;    
  16.      //儲存鼠標的位置座標    
  17.     private Vector2 pos;
  18.     void OnGUI()
  19.     {
  20.         //鼠標左擊,獲取當前鼠標的位置       
  21.         if (Input.GetMouseButton(0))
  22.         {
  23.             pos = Input.mousePosition; //屏幕座標
  24.         }
  25.         //繪製圖片,屏幕座標和GUI界面座標只在Y軸上方向相反,只要被Screen.height減就可以互相轉換。      
  26.         GUI.DrawTexture(new Rect(pos.x, Screen.height - pos.y, 100, 100), img);
  27.     }

  28. }
複製代碼

             

    案例2——角色頭頂的名字(世界座標轉GUI界面座標)先世界座標轉屏幕座標,再屏幕座標轉GUI界面座標

代碼如下:
  1. using UnityEngine;
  2. using System.Collections;

  3. public class Blood : MonoBehaviour {
  4.     public static float ScaleWidht = 0f;
  5.     public static float ScaleHeight = 0f;
  6.     private Rect _drawRect = new Rect();
  7.     public float Width = 0f;
  8.     public float Height = 10f;
  9.     public const float DesignStageWidth = 800;
  10.     public const float DesignStageHeight = 480;

  11.     public Vector2 pos2;
  12.     public float size_z;
  13.     // Use this for initialization
  14.     void Start () {
  15.         ScaleWidht = Screen.width / DesignStageWidth;
  16.         ScaleHeight = Screen.height / DesignStageHeight;
  17.         Height = 2f;

  18.         size_z = transform.gameObject.collider.bounds.size.z;
  19.     }


  20.     // Update is called once per frame
  21.     void Update () {
  22.         //世界座標轉換到屏幕座標
  23.         print(transform.forward);
  24.         pos2 = Camera.main.WorldToScreenPoint(transform.position + transform.forward * (size_z / 2)); 
  25.         //計算角色頭頂座標
  26.         pos2 = new Vector2(pos2.x, Screen.height  - pos2.y - Height); 



  27.         //Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + Height, transform.position.z);
  28.         //worldPosition = Camera.mainCamera.WorldToScreenPoint(worldPosition);
  29.         //_drawRect = new Rect((worldPosition.x - 100 * ScaleWidht) / ScaleWidht, (Screen.height - worldPosition.y - 50 * ScaleHeight) / ScaleHeight, 200, 50);
  30.     }

  31.     void OnGUI()
  32.     {
  33.         //GUILayout.BeginArea(_drawRect);
  34.         //    GUILayout.Label("======哈哈======");
  35.         //GUILayout.EndArea();

  36.         GUI.Label(new Rect(pos2.x, pos2.y, 100, 50), "=BETTER=");
  37.     }
  38. }
複製代碼


   案例3——類似屏幕解鎖功能的實現(屏幕座標轉換爲世界座標)

   首先是創建LineRenderer。GameObject -> Create Empty ->更名爲“LineRendererObj”,給LineRendererObj添加“Line Renderer”組件,Component ->Effects ->Line Renderer;將它的Positions 的size 設置爲0

       
接下來是代碼touch.CS:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class touch : MonoBehaviour {
  5.     private Event e;

  6.     public Texture2D Point;
  7.     public Color c1 = Color.yellow;
  8.     public Color c2 = Color.red;
  9.     public int lengthOfLineRenderer;
  10.     public GameObject LineRendererPrefab;

  11.     private LineRenderer lineRenderer;
  12.     /// <summary>
  13.     /// 保存創建的Line Renderer
  14.     /// </summary>
  15.     private List<LineRenderer> lineRendArray =new List<LineRenderer>();

  16.     private Vector3 screenPoint;
  17.     private Vector3 scanPos;

  18.     private Color[] color;

  19.     /// <summary>
  20.     /// 記錄宮格所在GUI位置
  21.     /// </summary>
  22.     public List<Rect> AreaRect = new List<Rect>();
  23.     /// <summary>
  24.     /// 記錄宮格中心點
  25.     /// </summary>
  26.     public List<Vector2> CenterPointList = new List<Vector2>();
  27.     /// <summary>
  28.     /// 宮格標籤
  29.     /// </summary>
  30.     public int RectFlag;
  31.     /// <summary>
  32.     /// 記錄正確的滑動順序
  33.     /// </summary>
  34.     public List<int> KeyOrder = new List<int>();
  35.     /// <summary>
  36.     /// 記錄玩家滑動順序
  37.     /// </summary>
  38.     public List<int> PlayerKeyOrder = new List<int>();

  39.     /// <summary>
  40.     /// 判斷開始鼠標位置是否可畫
  41.     /// </summary>
  42.     public bool CheckStartRect=false;

  43.     /// <summary>
  44.     /// 判斷結束鼠標位置是否可畫
  45.     /// </summary>
  46.     public bool CheckEndRect = false;

  47.     /// <summary>
  48.     /// 行數
  49.     /// </summary>
  50.     public int Row = 4;
  51.     /// <summary>
  52.     /// 列數
  53.     /// </summary>
  54.     public int Column = 4;

  55.     void Start()
  56.     {
  57.         e = Event.current;

  58.         scanPos = LineRendererPrefab.transform.position;
  59.         lineRenderer = (LineRenderer)LineRendererPrefab.GetComponent("LineRenderer");
  60.         lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
  61.         lengthOfLineRenderer = 0;
  62.         lineRenderer.SetColors(c1, c2);
  63.         lineRenderer.SetWidth(0.7F, 0.7F);
  64.         lineRenderer.SetVertexCount(0);

  65.         color = new Color[8];
  66.         color[0] = Color.yellow;
  67.         color[1] = Color.blue;
  68.         color[2] = Color.cyan;
  69.         color[3] = Color.gray;
  70.         color[4] = Color.green;
  71.         color[5] = Color.grey;
  72.         color[6] = Color.magenta;
  73.         color[7] = Color.red;

  74.         for (int RowCount = 0; RowCount < Row; RowCount++)
  75.         {
  76.             for (int columnCount = 0; columnCount < Column; columnCount++)
  77.             {
  78.                 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);
  79.                 AreaRect.Add(IconRect);

  80.                 Vector2 CenterP = IconRect.center;//得到每個的中心點
  81.                 CenterPointList.Add(CenterP);
  82.             }
  83.         }
  84.     }

  85.     void OnGUI()
  86.     {
  87.         e = Event.current;
  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.                 GUI.Label(IconRect, Point);
  94.             }
  95.         }
  96.     }

  97.     void Update()
  98.     {
  99.         if (e != null)
  100.         {
  101.             if (e.type == EventType.MouseDown)
  102.             {
  103.                 for (int i = 0; i < AreaRect.Count; i++)
  104.                 {
  105.                     if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)))
  106.                     {
  107.                         CheckStartRect = true;
  108.                         print("Contains");
  109.                         PlayerKeyOrder.Add(i);
  110.                         RectFlag = i;
  111.                         break;
  112.                     }
  113.                     else
  114.                     {
  115.                         CheckStartRect = false;
  116.                     }
  117.                 }

  118.                 if (CheckStartRect)
  119.                 {
  120.                     print("MouseDown_____");

  121.                     //Vector3 curPosition = mousePToLineRendererP(); 
  122.                     Vector3 curPosition = centerPToLineRendererP(RectFlag);
  123.                     GameObject newObj;
  124.                     newObj = (GameObject)Instantiate(LineRendererPrefab, LineRendererPrefab.transform.position, LineRendererPrefab.transform.rotation);
  125.                     lineRenderer = (LineRenderer)newObj.GetComponent("LineRenderer");

  126.                     int n = Random.Range(1, 8);
  127.                     c1 = color[n - 1];
  128.                     n = Random.Range(1, 8);
  129.                     c2 = color[n - 1];
  130.                     lineRenderer.SetColors(c1, c2);

  131.                     lineRenderer.SetVertexCount(1);
  132.                     lineRenderer.SetWidth(0.7F, 0.7F);
  133.                     lineRenderer.SetPosition(0, curPosition);
  134.                     lineRendArray.Add(lineRenderer);
  135.                     lengthOfLineRenderer++;
  136.                 }
  137.             }

  138.             if (e.type == EventType.MouseDrag&&CheckStartRect)
  139.             {
  140.                 print("MouseDrag_____");
  141.                 Vector3 curPosition = mousePToLineRendererP(); 
  142.                 DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition);
  143.             }

  144.             if (e.type == EventType.MouseUp && CheckStartRect)
  145.             {
  146.                 for (int i = 0; i < AreaRect.Count; i++)
  147.                 {
  148.                     if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)))
  149.                     {
  150.                         CheckEndRect = true;
  151.                         PlayerKeyOrder.Add(i);
  152.                         RectFlag = i;
  153.                         print("EndContains");
  154.                         break;
  155.                     }
  156.                     else
  157.                     {
  158.                         CheckEndRect = false;
  159.                     }
  160.                 }

  161.                 if (CheckEndRect)
  162.                 {
  163.                     Vector3 curPosition = centerPToLineRendererP(RectFlag);
  164.                     DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition);
  165.                 }
  166.                 else
  167.                 {
  168.                     PlayerKeyOrder.RemoveAt(PlayerKeyOrder.Count - 1);
  169.                     Destroy(lineRendArray[lengthOfLineRenderer - 1].gameObject);
  170.                     //lengthOfLineRenderer--;
  171.                 }

  172.             }
  173.         }        
  174.     }

  175.     void DrawRenderLine(LineRenderer line, Vector3 vect3)
  176.     {
  177.         Vector3 newPos = vect3;
  178.         line.SetVertexCount(2);

  179.         line.SetPosition(1, newPos);
  180.         print("new point: " + newPos);
  181.     }

  182.     //public Vector2 RectCenterPoint(Rect AreaRect)       //計算一個Rect的中心點
  183.     //{
  184.     //    Vector2 CenterPoint=Vector2.zero;
  185.     //    print("Rect:"+AreaRect);
  186.     //    CenterPoint.x=AreaRect.xMin+AreaRect.width/2;
  187.         
  188.     //    CenterPoint.y=AreaRect.yMin+AreaRect.height/2;
  189.     //    print("CenterPoint:"+CenterPoint);
  190.     //    return CenterPoint;
  191.     //}

  192.     /// <summary>
  193.     /// 鼠標所在位置轉換爲LineRenderer的位置
  194.     /// </summary>
  195.     /// <returns></returns>
  196.     public Vector3 mousePToLineRendererP()      
  197.     {
  198.         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
  199.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  200.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
  201.         print("curScreenPoint: " + curScreenPoint);
  202.         print("curPosition: " + curPosition);
  203.         return curPosition;
  204.     }

  205.     /// <summary>
  206.     /// 鼠標所在區域的中心點轉換爲LineRenderer的位置
  207.     /// </summary>
  208.     /// <returns></returns>
  209.     public Vector3 centerPToLineRendererP(int Flag)
  210.     {
  211.         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
  212.         Vector3 curScreenPoint = new Vector3(CenterPointList[Flag].x,Screen.height-CenterPointList[Flag].y,screenPoint.z);
  213.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
  214.         print("curScreenPoint: " + curScreenPoint);
  215.         print("curPosition: " + curPosition);
  216.         return curPosition;
  217.     }
  218. }
複製代碼

   把touch.CS綁定在Camera上,設置如下:

       

      運行後可以任意點間連線,如圖:

      

發佈了0 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章