Destroy physic helper without ref counter

This commit is contained in:
2016-04-18 20:31:28 +06:00
parent 3e5542e38d
commit ae568870c9
4 changed files with 89 additions and 14 deletions

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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