- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
- 纳金币
- 53488
- 精华
- 316
|
方法一:只有线 没有具体距离- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class CeJu : MonoBehaviour
- {
- private GameObject[] gameObj = new GameObject[2];
- int i = 0;
- void Update()
- {
- //按下鼠标左键时响应该方法
- if (Input.GetMouseButtonDown(0))
- {
- //创建一条射线一摄像机为原点
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- //射线碰撞到游戏地形时
- if (Physics.Raycast(ray, out hit))
- {
- //在点击的位置实例出一个Cube
- Vector3 post = new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z);
- GameObject obj = Instantiate(Resources.Load("1"), hit.point, Quaternion.identity) as GameObject;
- obj.transform.position = post;
- gameObj[i] = obj;
- if (i == 1)
- {
- //两点之间实例出一条线
- LineRenderer linerenderer = obj.AddComponent<LineRenderer>();
- linerenderer.SetPosition(0, gameObj[0].transform.position);
- linerenderer.SetPosition(1, gameObj[1].transform.position);
- linerenderer.SetWidth(0.1f,0.1f);
- i = -1;
- }
- i++;
- }
- }
- }
- }
复制代码 方法二:计算出距离- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Taggle : MonoBehaviour
- {
- //定义一个位置的集合
- List<Vector3> PointTransform = new List<Vector3>();
- //定义一个路标
- public GameObject LuBiao;
- int PositionCount;
- //定义一个实例化出物体的集合
- List<GameObject> InstateGameObject = new List<GameObject>();
- public GameObject Line;
- //连线集合
- List<GameObject> LineList = new List<GameObject>();
- // Use this for initialization
- void Start()
- {
- PositionCount = 0;
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- //从摄像机发出到点击坐标的射线
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hitInfo;
- if (Physics.Raycast(ray, out hitInfo))
- {
- //划出射线,只有在scene视图中才能看到
- Debug.DrawLine(ray.origin, hitInfo.point);
- Vector3 RemeberPosition = hitInfo.point;
- //添加到位置集合中
- PointTransform.Add(RemeberPosition);
- GameObject gam = Instantiate(LuBiao, RemeberPosition, Quaternion.identity) as GameObject;
- //添加到实例标记点的的集合中
- InstateGameObject.Add(gam);
- //定义的初始值小雨实例出来的标记物体个数 并且满足实例出来的物体数量不小于或等与一个
- if (PositionCount < InstateGameObject.Count && InstateGameObject.Count > 1)
- {
- //计算两个点的中点坐标
- Vector3 tempPos = (InstateGameObject[PositionCount].transform.position + InstateGameObject[PositionCount + 1].transform.position) / 2;
- //在两个点的中点处实例化线条,因为对物体的缩放,是从中心向两边延伸
- GameObject go = (GameObject)Instantiate(Line, tempPos, Quaternion.identity);
- go.name = "" + PositionCount;
- //实例出线条并添加到连线的集合中
- LineList.Add(go);
- //改变线条的朝向
- go.transform.right = (go.transform.position - InstateGameObject[PositionCount].transform.position).normalized;
- //计算两点的距离
- float distance = Vector3.Distance(InstateGameObject[PositionCount].transform.position, InstateGameObject[PositionCount + 1].transform.position);
- //延长线条,连接两点。
- go.transform.localScale = new Vector3(distance, 0.01f, 0.01f);
- PositionCount++;
- }
- }
- }
- }
- void OnGUI()
- {
- if (GUI.Button(new Rect(10, 10, 50, 50), "测量"))
- {
- //初始距离为0
- float Number = 0;
- //遍历循环位置的集合
- for (int i = 1; i < PointTransform.Count; i++)
- {
- //当满足位置集合不为空
- if (PointTransform[i] != null && PointTransform[i - 1] != null)
- {
- //计算出长度
- float a = Vector3.Distance(PointTransform[i], PointTransform[i - 1]);
- //
- Number += a;
- Debug.Log(i + " " + a);//其中i表是第几段线条 a表示长度
- }
- }
- //打印出距离的总长度
- Debug.Log(Number);
- //清除所有的位置
- PointTransform.Clear();
- //销毁所有的标记物体
- for (int i = 0; i < InstateGameObject.Count; i++)
- {
- Destroy(InstateGameObject[i]);
- }
- //销毁所有的线条
- for (int i = 0; i < LineList.Count; i++)
- {
- Destroy(LineList[i]);
- }
- //清除所有线条
- InstateGameObject.Clear();
- }
- }
- }
-
复制代码 |
|