| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GrassAreaRecorder : MonoBehaviour
- {
- public static GrassAreaRecorder ins;
- void Awake()
- {
- ins = this;
- init();
- }
- void OnDestroy()
- {
- ins = null;
- }
- void init() {
- areaPosList = new AreaInfo[this.transform.childCount];
- int i = 0;
- foreach (Transform child in this.transform)
- {
- child.gameObject.SetActive(false);
- //顺便记录子物体关键信息
- AreaInfo areaInfo = new AreaInfo();
- areaInfo.pos = child.transform.position;
- areaInfo.radius = child.transform.lossyScale.x / 2;
- areaPosList[i] = areaInfo;
- i++;
- }
- }
- private struct AreaInfo {
- public Vector3 pos;
- public float radius;
- }
- private AreaInfo[] areaPosList;
- public bool isInValidArea(Vector3 worldPosition) {
- foreach (AreaInfo areaInfo in areaPosList)
- {
- if (Vector3.Distance(areaInfo.pos, worldPosition) < areaInfo.radius) {
- return true;
- }
- }
- return false;
- }
- public Vector3 randomOneValidAreaPoint() {
- return areaPosList[Random.Range(0, areaPosList.Length)].pos;
- }
- }
|