TreeAreaRecorder.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. child.gameObject.SetActive(false);
  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. }
  36. private AreaInfo[] areaInfos;
  37. public int areaInfoLen {
  38. get {
  39. return areaInfos.Length;
  40. }
  41. }
  42. public AreaInfo getValidAreaInfo(int childIndex)
  43. {
  44. return areaInfos[childIndex];
  45. }
  46. public AreaInfo[] copyAreaInfos(int len) {
  47. AreaInfo[] list = new AreaInfo[len];
  48. System.Array.Copy(areaInfos, list, len);
  49. return list;
  50. }
  51. }