Skip to content

Commit

Permalink
ManagedMonoBehaviours and WorldBehaviours should now be destroyed upo…
Browse files Browse the repository at this point in the history
…n a DOTS reset
  • Loading branch information
tbg10101 committed Jul 30, 2020
1 parent f5bbcc7 commit 7a4bf0b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Runtime/Scripts/MonoBehaviours/ManagedMonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}
12 changes: 12 additions & 0 deletions Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -125,6 +133,10 @@ public void Dispose() {
PlayerLoopUtil.RemoveSubSystem(systemEntry.Value, systemEntry.Key);
}

if (Disposed) {
return;
}

_world.Dispose();
}

Expand Down
3 changes: 3 additions & 0 deletions Runtime/Scripts/Utils/DotsUtil.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Software10101.DOTS.MonoBehaviours;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
Expand All @@ -14,6 +15,8 @@ public static void Reset() {
PlayerLoopUtil.ResetPlayerLoop();

ReferenceTypeUtil.Clear();

ManagedMonoBehaviour.DestroyAll();
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 7a4bf0b

Please sign in to comment.