Skip to content

Instantly share code, notes, and snippets.

@gamemachine
Last active December 12, 2019 08:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gamemachine/054b6087154214a9742514165fca961e to your computer and use it in GitHub Desktop.
using AiGame.Ecs.Systems;
using AiNav;
using System;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
namespace Unity.Entities
{
public class EditorModeSystemConfig
{
public Type Type;
public bool Update;
}
public class EcsWorld : MonoBehaviour
{
public static bool EditorModeEnabled { get; private set; }
private static List<EditorModeSystemConfig> SystemConfigs = new List<EditorModeSystemConfig>()
{
new EditorModeSystemConfig {Type = typeof(AiNavSystem), Update = true},
new EditorModeSystemConfig {Type = typeof(PhysicsSystem)}
};
private static bool IsPlaying;
public static World Active
{
get
{
#if UNITY_EDITOR
if (!IsPlaying)
{
return GetEditorModeWorld();
}
#endif
return World.Active;
}
}
#if UNITY_EDITOR
public static void StopEditorSystems()
{
if (UnityEditor.EditorApplication.isPlaying) return;
if (World.Active == null) return;
if (!EditorModeEnabled) return;
foreach (var config in SystemConfigs)
{
var system = World.Active.GetExistingSystem(config.Type);
if (config.Update)
{
UnityEditor.EditorApplication.update -= system.Update;
}
World.Active.DestroySystem(system);
}
var entities = World.Active.EntityManager.GetAllEntities(Allocator.Temp);
World.Active.EntityManager.DestroyEntity(entities);
entities.Dispose();
EditorModeEnabled = false;
Debug.Log("Editor systems stopped");
}
private static World GetEditorModeWorld()
{
if (World.Active == null)
{
DefaultWorldInitialization.DefaultLazyEditModeInitialize();
}
if (!EditorModeEnabled)
{
EditorModeEnabled = true;
foreach (var config in SystemConfigs)
{
var system = World.Active.GetOrCreateSystem(config.Type);
if (config.Update)
{
system.Enabled = true;
UnityEditor.EditorApplication.update += system.Update;
}
}
Debug.Log("Editor systems started");
}
return World.Active;
}
private static void OnPlaymodeChanged(UnityEditor.PlayModeStateChange change)
{
switch (change)
{
case UnityEditor.PlayModeStateChange.EnteredEditMode:
IsPlaying = false;
break;
}
}
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Init()
{
Debug.Log("Init");
if (EditorModeEnabled)
{
StopEditorSystems();
}
EditorModeEnabled = false;
IsPlaying = true;
#if UNITY_EDITOR
UnityEditor.EditorApplication.playModeStateChanged -= OnPlaymodeChanged;
UnityEditor.EditorApplication.playModeStateChanged += OnPlaymodeChanged;
#endif
}
}
[CustomEditor(typeof(EcsWorld))]
public class EcsWorldEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (!EditorApplication.isPlaying)
{
if (EcsWorld.EditorModeEnabled)
{
GUILayout.Label("Ecs is running");
if (GUILayout.Button("StopEditorSystems"))
{
EcsWorld.StopEditorSystems();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment