TreeAreaRecorder.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class TreeAreaRecorder : MonoBehaviour
  5. {
  6. bool inited = false;
  7. public TreeAreaRecorder InitAndGet() {
  8. if (inited) return this;
  9. areaInfos = new AreaInfo[this.transform.childCount];
  10. int i = 0;
  11. foreach (Transform child in this.transform)
  12. {
  13. //顺便记录子物体关键信息
  14. AreaInfo areaInfo = new AreaInfo();
  15. areaInfo.transform = child.transform;
  16. areaInfo.position = areaInfo.transform.position;
  17. Vector3 lossyScale = child.transform.lossyScale;
  18. areaInfo.radius = Mathf.Max(Mathf.Abs(lossyScale.x), Mathf.Abs(lossyScale.z)) / 2;
  19. areaInfo.height = lossyScale.y / 2;
  20. areaInfos[i] = areaInfo;
  21. i++;
  22. }
  23. return this;
  24. }
  25. public class AreaInfo {
  26. public Transform transform;
  27. public Vector3 position;
  28. public float radius;
  29. public float height;
  30. public float teamCompareValue;
  31. public float distanceInHorizontal(Vector3 pos) {
  32. return Mathf.Sqrt(Mathf.Pow(position.x - pos.x, 2) + Mathf.Pow(position.z - pos.z, 2));
  33. }
  34. }
  35. private AreaInfo[] areaInfos;
  36. public int areaInfoLen {
  37. get {
  38. return areaInfos.Length;
  39. }
  40. }
  41. public AreaInfo getValidAreaInfo(int childIndex)
  42. {
  43. return areaInfos[childIndex];
  44. }
  45. public AreaInfo[] copyAreaInfos(int len) {
  46. AreaInfo[] list = new AreaInfo[len];
  47. System.Array.Copy(areaInfos, list, len);
  48. return list;
  49. }
  50. }