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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值