ref counter for IsoPhysicHelper

This commit is contained in:
2016-04-18 02:24:23 +06:00
parent 7656f3c565
commit 3e5542e38d
3 changed files with 26 additions and 10 deletions

View File

@@ -10,6 +10,16 @@ namespace IsoTools.Internal {
get { return _isoFakeObject; }
}
int _refCounter = 0;
public void AddRefCounter() {
++_refCounter;
}
public void DropRefCounter() {
if ( --_refCounter <= 0 ) {
Destroy(this);
}
}
void Awake() {
hideFlags = HideFlags.HideInInspector;
var iso_object = GetComponent<IsoObject>();

View File

@@ -16,10 +16,11 @@ namespace IsoTools {
}
protected GameObject fakeObject {
get {
var helper = IsoUtils.GetOrCreateComponent<IsoPhysicHelper>(gameObject);
return helper ? helper.isoFakeObject : null;
}
get { return physicHelper.isoFakeObject; }
}
protected IsoPhysicHelper physicHelper {
get { return IsoUtils.GetOrCreateComponent<IsoPhysicHelper>(gameObject); }
}
[SerializeField]
@@ -96,6 +97,7 @@ namespace IsoTools {
_realCollider = CreateRealCollider(fake_collider_go);
_realCollider.material = material;
_realCollider.isTrigger = isTrigger;
physicHelper.AddRefCounter();
}
void OnEnable() {
@@ -115,6 +117,7 @@ namespace IsoTools {
Destroy(_realCollider.gameObject);
_realCollider = null;
}
physicHelper.DropRefCounter();
}
#if UNITY_EDITOR

View File

@@ -11,15 +11,16 @@ namespace IsoTools {
public class IsoRigidbody : MonoBehaviour {
Rigidbody _realRigidbody = null;
protected Rigidbody realRigidbody {
Rigidbody realRigidbody {
get { return _realRigidbody; }
}
protected GameObject fakeObject {
get {
var helper = IsoUtils.GetOrCreateComponent<IsoPhysicHelper>(gameObject);
return helper ? helper.isoFakeObject : null;
}
GameObject fakeObject {
get { return physicHelper.isoFakeObject; }
}
IsoPhysicHelper physicHelper {
get { return IsoUtils.GetOrCreateComponent<IsoPhysicHelper>(gameObject); }
}
[SerializeField]
@@ -326,6 +327,7 @@ namespace IsoTools {
_realRigidbody.isKinematic = isKinematic;
_realRigidbody.interpolation = interpolation;
_realRigidbody.collisionDetectionMode = collisionDetectionMode;
physicHelper.AddRefCounter();
}
void OnEnable() {
@@ -346,6 +348,7 @@ namespace IsoTools {
Destroy(_realRigidbody);
_realRigidbody = null;
}
physicHelper.DropRefCounter();
}
#if UNITY_EDITOR