diff --git a/src/Fetures.meta b/src/Connectors.meta similarity index 77% rename from src/Fetures.meta rename to src/Connectors.meta index c6ada3a..93be778 100644 --- a/src/Fetures.meta +++ b/src/Connectors.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a20b387d9272da846b2a1206bfb6d53a +guid: 2b32116c3998f6742a35e492a88176be folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/src/Connectors/EcsDefaultWorldProvider.cs b/src/Connectors/EcsDefaultWorldProvider.cs new file mode 100644 index 0000000..40b1351 --- /dev/null +++ b/src/Connectors/EcsDefaultWorldProvider.cs @@ -0,0 +1,19 @@ +using UnityEngine; + +namespace DCFApixels.DragonECS +{ + [CreateAssetMenu(fileName = nameof(EcsDefaultWorldProvider), menuName = EcsConsts.FRAMEWORK_NAME + "/WorldProviders/" + nameof(EcsDefaultWorldProvider), order = 1)] + public class EcsDefaultWorldProvider : EcsWorldProvider + { + private static EcsDefaultWorldProvider _single; + public static EcsDefaultWorldProvider Single + { + get + { + if (_single == null) + _single = FindOrCreateSingle(); + return _single; + } + } + } +} diff --git a/src/Fetures/UnityWorldProvider.cs.meta b/src/Connectors/EcsDefaultWorldProvider.cs.meta similarity index 83% rename from src/Fetures/UnityWorldProvider.cs.meta rename to src/Connectors/EcsDefaultWorldProvider.cs.meta index 107dcbc..a5fb423 100644 --- a/src/Fetures/UnityWorldProvider.cs.meta +++ b/src/Connectors/EcsDefaultWorldProvider.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: d0af8ddc3edb89242a26c1d308a18c87 +guid: 15b6f990a7e05b34a937a9a850d7c68c MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/src/Extensions/EcsEntityConnect.cs b/src/Connectors/EcsEntityConnect.cs similarity index 89% rename from src/Extensions/EcsEntityConnect.cs rename to src/Connectors/EcsEntityConnect.cs index 97d1ea7..ba4d384 100644 --- a/src/Extensions/EcsEntityConnect.cs +++ b/src/Connectors/EcsEntityConnect.cs @@ -93,8 +93,6 @@ namespace DCFApixels.DragonECS.Editors public class EcsEntityEditor : Editor { private EcsEntityConnect Target => (EcsEntityConnect)target; - private GUIStyle _greenStyle; - private GUIStyle _redStyle; private bool _isInit = false; @@ -103,19 +101,13 @@ namespace DCFApixels.DragonECS.Editors if (_isInit) return; - _greenStyle = EcsEditor.GetStyle(new Color32(75, 255, 0, 100)); - _redStyle = EcsEditor.GetStyle(new Color32(255, 0, 75, 100)); - _isInit = true; } public override void OnInspectorGUI() { Init(); - if (Target.IsAlive) - GUILayout.Box("Connected", _greenStyle, GUILayout.ExpandWidth(true)); - else - GUILayout.Box("Not connected", _redStyle, GUILayout.ExpandWidth(true)); + EcsGUI.Layout.DrawConnectStatus(Target.IsAlive); if (Target.Entity.TryGetID(out int id)) EditorGUILayout.IntField(id); diff --git a/src/Extensions/EcsEntityConnect.cs.meta b/src/Connectors/EcsEntityConnect.cs.meta similarity index 100% rename from src/Extensions/EcsEntityConnect.cs.meta rename to src/Connectors/EcsEntityConnect.cs.meta diff --git a/src/Connectors/EcsWorldProvider.cs b/src/Connectors/EcsWorldProvider.cs new file mode 100644 index 0000000..3217499 --- /dev/null +++ b/src/Connectors/EcsWorldProvider.cs @@ -0,0 +1,66 @@ +using System; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif + +namespace DCFApixels.DragonECS +{ + [Serializable] + public abstract class EcsWorldProviderBase : ScriptableObject + { + public abstract EcsWorld WorldRaw { get; } + public abstract EcsWorld GetRaw(Func builder = null); + } + [Serializable] + public abstract class EcsWorldProvider : EcsWorldProviderBase where TWorld : EcsWorld + { + private static TWorld _world; + public sealed override EcsWorld WorldRaw => _world; + public override EcsWorld GetRaw(Func builder = null) + { + if (_world == null || _world.IsDestroyed) + { + if (builder != null) + _world = (TWorld)builder(); + else + _world = (TWorld)Activator.CreateInstance(typeof(TWorld)); + OnWorldCreated(_world); + } + return _world; + } + public TWorld Get(Func builder = null) + { + if (_world == null || _world.IsDestroyed) + { + if(builder != null) + _world = builder(); + else + _world = (TWorld)Activator.CreateInstance(typeof(TWorld)); + OnWorldCreated(_world); + } + return _world; + } + protected virtual void OnWorldCreated(TWorld world) { } + + protected static TProvider FindOrCreateSingle() where TProvider : EcsWorldProvider + { + string name = typeof(TProvider).Name + "Single"; + TProvider instance = Resources.Load(name); + if (instance == null) + { + instance = CreateInstance(); +#if UNITY_EDITOR + if (AssetDatabase.IsValidFolder("Assets/Resources/") == false) + { + System.IO.Directory.CreateDirectory(Application.dataPath + "/Resources/"); + AssetDatabase.Refresh(); + } + AssetDatabase.CreateAsset(instance, "Assets/Resources/" + name + ".asset"); + AssetDatabase.Refresh(); +#endif + } + return instance; + } + } +} \ No newline at end of file diff --git a/src/Connectors/EcsWorldProvider.cs.meta b/src/Connectors/EcsWorldProvider.cs.meta new file mode 100644 index 0000000..0929240 --- /dev/null +++ b/src/Connectors/EcsWorldProvider.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b041a062a2a5b2643bec37be4fad79f1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Debug/Editor/EcsEditor.cs b/src/Debug/Editor/EcsEditor.cs index 9266277..4fb00c7 100644 --- a/src/Debug/Editor/EcsEditor.cs +++ b/src/Debug/Editor/EcsEditor.cs @@ -6,6 +6,44 @@ using UnityEngine; namespace DCFApixels.DragonECS.Editors { + public static class EcsGUI + { + private static GUIStyle _greenStyle; + private static GUIStyle _redStyle; + + private static bool _isInit = false; + private static void Init() + { + if (_isInit) + return; + + _greenStyle = EcsEditor.GetStyle(new Color32(75, 255, 0, 100)); + _redStyle = EcsEditor.GetStyle(new Color32(255, 0, 75, 100)); + _isInit = true; + } + + public static void DrawConnectStatus(Rect position, bool status) + { + Init(); + if (status) + GUI.Box(position, "Connected", _greenStyle); + else + GUI.Box(position, "Not connected", _redStyle); + } + + + public static class Layout + { + public static void DrawConnectStatus(bool status, params GUILayoutOption[] options) + { + Init(); + if (status) + GUILayout.Box("Connected", _greenStyle, GUILayout.ExpandWidth(true)); + else + GUILayout.Box("Not connected", _redStyle, GUILayout.ExpandWidth(true)); + } + } + } [InitializeOnLoad] public static class EcsEditor { @@ -40,10 +78,11 @@ namespace DCFApixels.DragonECS.Editors { GUIStyle result = new GUIStyle(GUI.skin.box); Color componentColor = color32; - result.normal.background = CreateTexture(2, 2, componentColor); - result.active.background = CreateTexture(2, 2, componentColor); - result.hover.background = CreateTexture(2, 2, componentColor); - result.focused.background = CreateTexture(2, 2, componentColor); + Texture2D texture2D = CreateTexture(2, 2, componentColor); + result.hover.background = texture2D; + result.focused.background = texture2D; + result.active.background = texture2D; + result.normal.background = texture2D; return result; } private static Texture2D CreateTexture(int width, int height, Color color) @@ -58,7 +97,6 @@ namespace DCFApixels.DragonECS.Editors return result; } - public static string GetGenericName(Type type) => EcsDebugUtility.GetGenericTypeName(type); public static string GetName() => GetName(typeof(T)); diff --git a/src/Extensions/UnityComponents.cs b/src/Extensions/UnityComponents.cs index 120219a..603f036 100644 --- a/src/Extensions/UnityComponents.cs +++ b/src/Extensions/UnityComponents.cs @@ -7,10 +7,11 @@ namespace DCFApixels.DragonECS { [Serializable] [DebugColor(255 / 3, 255, 0)] - public struct UnityComponent : IEcsComponent, IEnumerable//IntelliSense hack + public readonly struct UnityComponent : IEcsComponent, IEnumerable//IntelliSense hack where T : Component { - public T obj; + public readonly T obj; + public UnityComponent(T obj) => this.obj = obj; IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack } @@ -23,7 +24,7 @@ namespace DCFApixels.DragonECS public override void OnValidate(GameObject gameObject) { if (component.obj == null) - component.obj = gameObject.GetComponent(); + component = new UnityComponent(gameObject.GetComponent()); } } diff --git a/src/Extensions/UnityGameObject.cs b/src/Extensions/UnityGameObject.cs index b1b9614..83959b4 100644 --- a/src/Extensions/UnityGameObject.cs +++ b/src/Extensions/UnityGameObject.cs @@ -7,10 +7,10 @@ using UnityEditor; namespace DCFApixels.DragonECS { [DebugColor(DebugColor.Cyan)] - public struct UnityGameObject : IEcsComponent + public readonly struct UnityGameObject : IEcsComponent { - public GameObject gameObject; - public Transform transform; + public readonly GameObject gameObject; + public readonly Transform transform; public string Name { diff --git a/src/Fetures/UnityWorldProvider.cs b/src/Fetures/UnityWorldProvider.cs deleted file mode 100644 index ed67222..0000000 --- a/src/Fetures/UnityWorldProvider.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -namespace DCFApixels.DragonECS -{ - public static class UnityWorldProvider - { - private static TWorld _world; - - public static TWorld Get(Func builder) - { - if (builder == null) - throw new ArgumentNullException(); - - if (_world == null) - _world = builder(); - - return _world; - } - - public static TWorld Get() - { - if (_world == null) - _world = (TWorld)Activator.CreateInstance(typeof(TWorld)); - return _world; - } - } - -}