123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [AddComponentMenu("Pathfinding/Navmesh/RecastMeshObj")]
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_recast_mesh_obj.php")]
- public class RecastMeshObj : VersionedMonoBehaviour {
-
- protected static RecastBBTree tree = new RecastBBTree();
-
- protected static List<RecastMeshObj> dynamicMeshObjs = new List<RecastMeshObj>();
-
- public static void GetAllInBounds (List<RecastMeshObj> buffer, Bounds bounds) {
- if (!Application.isPlaying) {
- var objs = FindObjectsOfType(typeof(RecastMeshObj)) as RecastMeshObj[];
- for (int i = 0; i < objs.Length; i++) {
- objs[i].RecalculateBounds();
- if (objs[i].GetBounds().Intersects(bounds)) {
- buffer.Add(objs[i]);
- }
- }
- return;
- } else if (Time.timeSinceLevelLoad == 0) {
-
-
- var objs = FindObjectsOfType(typeof(RecastMeshObj)) as RecastMeshObj[];
- for (int i = 0; i < objs.Length; i++) objs[i].Register();
- }
- for (int q = 0; q < dynamicMeshObjs.Count; q++) {
- if (dynamicMeshObjs[q].GetBounds().Intersects(bounds)) {
- buffer.Add(dynamicMeshObjs[q]);
- }
- }
- Rect r = Rect.MinMaxRect(bounds.min.x, bounds.min.z, bounds.max.x, bounds.max.z);
- tree.QueryInBounds(r, buffer);
- }
- [HideInInspector]
- public Bounds bounds;
-
-
-
-
-
-
-
-
-
-
- public bool dynamic = true;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int area = 0;
- bool _dynamic;
- bool registered;
- void OnEnable () {
- Register();
- }
- void Register () {
- if (registered) return;
- registered = true;
-
- area = Mathf.Clamp(area, -1, 1 << 25);
- Renderer rend = GetComponent<Renderer>();
- Collider coll = GetComponent<Collider>();
- if (rend == null && coll == null) throw new System.Exception("A renderer or a collider should be attached to the GameObject");
- MeshFilter filter = GetComponent<MeshFilter>();
- if (rend != null && filter == null) throw new System.Exception("A renderer was attached but no mesh filter");
-
- bounds = rend != null ? rend.bounds : coll.bounds;
- _dynamic = dynamic;
- if (_dynamic) {
- dynamicMeshObjs.Add(this);
- } else {
- tree.Insert(this);
- }
- }
-
- private void RecalculateBounds () {
- Renderer rend = GetComponent<Renderer>();
- Collider coll = GetCollider();
- if (rend == null && coll == null) throw new System.Exception("A renderer or a collider should be attached to the GameObject");
- MeshFilter filter = GetComponent<MeshFilter>();
- if (rend != null && filter == null) throw new System.Exception("A renderer was attached but no mesh filter");
-
- bounds = rend != null ? rend.bounds : coll.bounds;
- }
-
- public Bounds GetBounds () {
- if (_dynamic) {
- RecalculateBounds();
- }
- return bounds;
- }
- public MeshFilter GetMeshFilter () {
- return GetComponent<MeshFilter>();
- }
- public Collider GetCollider () {
- return GetComponent<Collider>();
- }
- void OnDisable () {
- registered = false;
- if (_dynamic) {
- dynamicMeshObjs.Remove(this);
- } else {
- if (!tree.Remove(this)) {
- throw new System.Exception("Could not remove RecastMeshObj from tree even though it should exist in it. Has the object moved without being marked as dynamic?");
- }
- }
- _dynamic = dynamic;
- }
- }
- }
|