using System.Collections; using System.Collections.Generic; using UnityEngine; //一个可无限伸长的尺子 尺子位置是尺子的起点 通过指向目标 改变自身长度的方式始终保持两端与待测量的两个点重合 public class RulerInstanceCtrl : MonoBehaviour { public Text3DCtrl Text3D_distance; public Transform rulerMesh; // Start is called before the first frame update void Start() { //Text3D_distance = this.GetComponentInChildren(); Text3D_distance.SetLookTarget(Camera.main.transform); } //待测量距离的第一个位置 public void SetFirstPos(Vector3 pos) { this.transform.position = pos; } //待测量距离的第二个位置 public void SetSecondPos(Vector3 pos) { float distance = Vector3.Distance(this.transform.position, pos); this.transform.LookAt(pos); Vector3 localScale = rulerMesh.transform.localScale; localScale.z = distance / 2; rulerMesh.transform.localScale = localScale; Text3D_distance.transform.localPosition = new Vector3(0, 3, localScale.z); Text3D_distance?.SetName(distance.ToString("0.0")); } }