using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using XFramework.Objects; //测量功能实现类 public class Surveyor { GameObject rulerPrefab; RulerInstanceCtrl currentRuler;//当前待测量的点 public List rulers = new List(); public Surveyor(GameObject rulerPrefab) { this.rulerPrefab = rulerPrefab; } /// /// 循环函数 监听测量输入 /// public void Update() { if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject()) return; //射线检测时屏蔽UI部分 if (Input.GetMouseButtonDown(0))//鼠标点击地面时 { Vector3 point = GetRayHitPoint(); if (point != Vector3.zero) SetPoint(point); } if (currentRuler)//当前测量未完成时 不断更新第二个测量点位 { Vector3 point = GetRayHitPoint(); if (point != Vector3.zero) currentRuler?.SetSecondPos(point); } } //结束测量 收起所有的尺子 public void StopCL() { for (int i = 0; i < rulers.Count; i++) { ObjectPool.Instance.Destroy(rulers[i].gameObject); } } /// /// 设置待测量点 第一个点生成测量实例 第二个点完成一次测量 /// void SetPoint(Vector3 point) { if (currentRuler == null)//当前没有等待设置第二个点的尺子 重新复制一把 并将该点位确认为起点 { currentRuler = ObjectPool.Instance.Instantiate(rulerPrefab).GetComponent(); rulers.Add(currentRuler.gameObject); currentRuler.SetFirstPos(point); } else//当前有等待设置第二个点的尺子 确定第二点位 即放下尺子 { currentRuler.SetSecondPos(point); currentRuler = null; } } //获取点击的地面位置 Vector3 GetRayHitPoint() { RaycastHit hit;//鼠标射线检测 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 2000, LayerMask.GetMask("Floor"))) { return hit.point; } return Vector3.zero; } }