From 7a4bf0b91aa899491a5fba89da4e34223b5d4ca8 Mon Sep 17 00:00:00 2001 From: Tristan Bellman-Greenwood Date: Wed, 29 Jul 2020 23:02:16 -0400 Subject: [PATCH] ManagedMonoBehaviours and WorldBehaviours should now be destroyed upon a DOTS reset --- .../Scripts/MonoBehaviours/ManagedMonoBehaviour.cs | 8 +++++++- Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs | 12 ++++++++++++ Runtime/Scripts/Utils/DotsUtil.cs | 3 +++ package.json | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Runtime/Scripts/MonoBehaviours/ManagedMonoBehaviour.cs b/Runtime/Scripts/MonoBehaviours/ManagedMonoBehaviour.cs index 05c187b..e1eaa90 100644 --- a/Runtime/Scripts/MonoBehaviours/ManagedMonoBehaviour.cs +++ b/Runtime/Scripts/MonoBehaviours/ManagedMonoBehaviour.cs @@ -46,7 +46,13 @@ internal static void DoUpdate() { internal virtual void Destroy() { _destroyed = true; Instances.Remove(this); - Object.Destroy(gameObject); + Destroy(gameObject); + } + + internal static void DestroyAll() { + while (Instances.Count > 0) { + Instances[Instances.Count - 1].Destroy(); + } } } } diff --git a/Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs b/Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs index 792eec9..091382a 100644 --- a/Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs +++ b/Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs @@ -14,6 +14,12 @@ protected virtual void Awake() { _world = new WorldWrapper(name); } + protected void Update() { + if (_world.Disposed) { + Destroy(this); + } + } + protected virtual void OnDestroy() { _world?.Dispose(); } @@ -58,6 +64,8 @@ public class WorldWrapper : IDisposable { public EntityManager EntityManager => _world.EntityManager; + public bool Disposed => !_world.IsCreated; + public WorldWrapper(string name) { _world = new World(name); } @@ -125,6 +133,10 @@ public void Dispose() { PlayerLoopUtil.RemoveSubSystem(systemEntry.Value, systemEntry.Key); } + if (Disposed) { + return; + } + _world.Dispose(); } diff --git a/Runtime/Scripts/Utils/DotsUtil.cs b/Runtime/Scripts/Utils/DotsUtil.cs index bc498b2..5b8187c 100644 --- a/Runtime/Scripts/Utils/DotsUtil.cs +++ b/Runtime/Scripts/Utils/DotsUtil.cs @@ -1,3 +1,4 @@ +using Software10101.DOTS.MonoBehaviours; using Unity.Collections; using Unity.Entities; using UnityEngine; @@ -14,6 +15,8 @@ public static void Reset() { PlayerLoopUtil.ResetPlayerLoop(); ReferenceTypeUtil.Clear(); + + ManagedMonoBehaviour.DestroyAll(); } } } diff --git a/package.json b/package.json index dce8a89..2470df7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.10101software.dots.hybridsimulation", "description": "A framework for using FixedUpdate in a simulation world which is linked to a GameObject-based presentation layer.", - "version": "0.6.1", + "version": "0.6.2", "unity": "2020.1", "displayName": "DOTS Hybrid Simulation Worlds", "dependencies": {