mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-15 01:12:05 +07:00
Destroy physic helper without ref counter
This commit is contained in:
@@ -1,21 +1,42 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IsoTools.Internal {
|
||||
[DisallowMultipleComponent]
|
||||
[RequireComponent(typeof(IsoObject))]
|
||||
public class IsoPhysicHelper : MonoBehaviour {
|
||||
|
||||
static List<IsoCollider > _tmpColliders = new List<IsoCollider >(7);
|
||||
static List<IsoRigidbody> _tmpRigidbodies = new List<IsoRigidbody>(7);
|
||||
|
||||
GameObject _isoFakeObject = null;
|
||||
public GameObject isoFakeObject {
|
||||
get { return _isoFakeObject; }
|
||||
}
|
||||
|
||||
int _refCounter = 0;
|
||||
public void AddRefCounter() {
|
||||
++_refCounter;
|
||||
}
|
||||
public void DropRefCounter() {
|
||||
if ( --_refCounter <= 0 ) {
|
||||
public void DestroyIfUnnecessary(Component except) {
|
||||
var unnecessary = true;
|
||||
GetComponents<IsoCollider >(_tmpColliders);
|
||||
GetComponents<IsoRigidbody>(_tmpRigidbodies);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
_tmpColliders.Clear();
|
||||
_tmpRigidbodies.Clear();
|
||||
if ( unnecessary ) {
|
||||
Destroy(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,6 @@ namespace IsoTools {
|
||||
_realCollider = CreateRealCollider(fake_collider_go);
|
||||
_realCollider.material = material;
|
||||
_realCollider.isTrigger = isTrigger;
|
||||
physicHelper.AddRefCounter();
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
@@ -117,7 +116,7 @@ namespace IsoTools {
|
||||
Destroy(_realCollider.gameObject);
|
||||
_realCollider = null;
|
||||
}
|
||||
physicHelper.DropRefCounter();
|
||||
physicHelper.DestroyIfUnnecessary(this);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
@@ -327,7 +327,6 @@ namespace IsoTools {
|
||||
_realRigidbody.isKinematic = isKinematic;
|
||||
_realRigidbody.interpolation = interpolation;
|
||||
_realRigidbody.collisionDetectionMode = collisionDetectionMode;
|
||||
physicHelper.AddRefCounter();
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
@@ -348,7 +347,7 @@ namespace IsoTools {
|
||||
Destroy(_realRigidbody);
|
||||
_realRigidbody = null;
|
||||
}
|
||||
physicHelper.DropRefCounter();
|
||||
physicHelper.DestroyIfUnnecessary(this);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
Reference in New Issue
Block a user