using UnityEngine; using System.Collections.Generic; namespace IsoTools.Internal { [DisallowMultipleComponent] [RequireComponent(typeof(IsoObject))] public class IsoPhysicHelper : MonoBehaviour { static List _tmpColliders = new List(7); static List _tmpRigidbodies = new List(7); static List _tmpTriggerListeners = new List(7); static List _tmpCollisionListeners = new List(7); GameObject _isoFakeObject = null; public GameObject isoFakeObject { get { return _isoFakeObject; } } //TODO: fix copy-paste public void DestroyIfUnnecessary(Component except) { var unnecessary = true; GetComponents (_tmpColliders); GetComponents (_tmpRigidbodies); GetComponents (_tmpTriggerListeners); GetComponents(_tmpCollisionListeners); if ( unnecessary ) { for ( int i = 0, e = _tmpColliders.Count; i < e; ++i ) { if ( _tmpColliders[i] != except ) { unnecessary = false; break; } } } if ( unnecessary ) { for ( int i = 0, e = _tmpRigidbodies.Count; i < e; ++i ) { if ( _tmpRigidbodies[i] != except ) { unnecessary = false; break; } } } if ( unnecessary ) { for ( int i = 0, e = _tmpTriggerListeners.Count; i < e; ++i ) { if ( _tmpTriggerListeners[i] != except ) { unnecessary = false; break; } } } if ( unnecessary ) { for ( int i = 0, e = _tmpCollisionListeners.Count; i < e; ++i ) { if ( _tmpCollisionListeners[i] != except ) { unnecessary = false; break; } } } _tmpColliders.Clear(); _tmpRigidbodies.Clear(); _tmpTriggerListeners.Clear(); _tmpCollisionListeners.Clear(); if ( unnecessary ) { Destroy(this); } } void Awake() { hideFlags = HideFlags.HideInInspector; var iso_object = GetComponent(); if ( iso_object ) { _isoFakeObject = new GameObject("_Fake" + gameObject.name); _isoFakeObject.AddComponent().Init(iso_object); _isoFakeObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.NotEditable; GameObject.DontDestroyOnLoad(_isoFakeObject); } } void OnDestroy() { if ( _isoFakeObject ) { DestroyImmediate(_isoFakeObject); _isoFakeObject = null; } } } }