TreeAreaRecorder.cs 1.8 KB

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