using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// 空间测距脚本 /// public class SpaceRanging : RangingLineType { bool ControlRanging = false;//控制重复生成数字显示 private void Start() { variableX = 5f; variableY = 18f; variableZ = 5f; } public override void HandleOther(bool flag) { if (ControlRanging) { EstimatePosY("Height"); EstimatePosY("Width"); ControlRanging = false; InstanceMeshText(placementPos, placementPos2); placementPos = new Vector3(0, 0, 0); placementPos2 = new Vector3(0, 0, 0); } else { ControlRanging = true; } if (!flag) { RangSpecialTreat(); } } /// /// 检查第一个碰撞点和第二个碰撞点的高度 /// 从而判断哪个点是基础点 /// Vector3 NewLine; public void EstimatePosY(string Value) { //根据新获取的点绘制线段 if (placementPos.y < placementPos2.y) { if (Value.Equals("Height")) { NewLine = new Vector3(placementPos.x, placementPos2.y, placementPos.z); } else { NewLine = new Vector3(placementPos2.x, placementPos.y, placementPos2.z); } CreateLine(placementPos, NewLine); InstanceMeshText(placementPos, NewLine); } else { if (Value.Equals("Height")) { NewLine = new Vector3(placementPos2.x, placementPos.y, placementPos2.z); } else { NewLine = new Vector3(placementPos.x, placementPos2.y, placementPos.z); } CreateLine(placementPos2, NewLine); InstanceMeshText(placementPos2, NewLine); } } /// /// 实例化完成后显示米数 /// /// 第一个碰撞点 /// 第二个碰撞点 /// 标记起点米数 public void InstanceMeshText(Vector3 placementPos_, Vector3 placementPos2_) { float dis = Vector3.Distance(placementPos_, placementPos2_); float level = Vector2.Distance(new Vector2(placementPos_.x, placementPos_.z), new Vector2(placementPos2_.x, placementPos2_.z)); float height = Mathf.Abs(placementPos_.y - placementPos2_.y); Vector3 vethight = new Vector3(); Vector3 vetLow = new Vector3(); //获取高低点 if (placementPos_.y < placementPos2_.y) { vetLow = placementPos_; vethight = placementPos2_; } else { if (placementPos_.y == placementPos2_.y)//判断Y水平情况下 { vetLow = placementPos_; vethight = placementPos2_; } else { vetLow = placementPos2_; vethight = placementPos_; } } if (ControlRanging) { //交叉点 GameObject PointMeshText = Instantiate(Resources.Load("Prefab/Lines/Ranging"), vetLow, Quaternion.identity) as GameObject; PointMeshText.transform.parent = transform; PointMeshText.transform.localScale = new Vector3(2, 2, 1); PointMeshText.transform.Find("text").GetComponent().text = "0"; ControlRanging = false; RangList.Add(PointMeshText); } //顶点 GameObject PointMeshText_ = Instantiate(Resources.Load("Prefab/Lines/Ranging"), vethight, Quaternion.identity) as GameObject; PointMeshText_.transform.parent = transform; PointMeshText_.transform.localScale = new Vector3(2, 2, 1); float distance = Vector3.Distance(vetLow, vethight); PointMeshText_.transform.Find("text").GetComponent().text = Math.Round(distance, 2).ToString(); RangList.Add(PointMeshText_); } }