This commit is contained in:
Mikhail 2023-06-29 14:26:01 +08:00
parent 5b3bce5476
commit 2a4978c907
11 changed files with 149 additions and 50 deletions

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a20b387d9272da846b2a1206bfb6d53a
guid: 2b32116c3998f6742a35e492a88176be
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -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<EcsDefaultWorld>
{
private static EcsDefaultWorldProvider _single;
public static EcsDefaultWorldProvider Single
{
get
{
if (_single == null)
_single = FindOrCreateSingle<EcsDefaultWorldProvider>();
return _single;
}
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d0af8ddc3edb89242a26c1d308a18c87
guid: 15b6f990a7e05b34a937a9a850d7c68c
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -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);

View File

@ -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<EcsWorld> builder = null);
}
[Serializable]
public abstract class EcsWorldProvider<TWorld> : EcsWorldProviderBase where TWorld : EcsWorld
{
private static TWorld _world;
public sealed override EcsWorld WorldRaw => _world;
public override EcsWorld GetRaw(Func<EcsWorld> 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<TWorld> 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<TProvider>() where TProvider : EcsWorldProvider<TWorld>
{
string name = typeof(TProvider).Name + "Single";
TProvider instance = Resources.Load<TProvider>(name);
if (instance == null)
{
instance = CreateInstance<TProvider>();
#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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b041a062a2a5b2643bec37be4fad79f1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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<T>() => GetName(typeof(T));

View File

@ -7,10 +7,11 @@ namespace DCFApixels.DragonECS
{
[Serializable]
[DebugColor(255 / 3, 255, 0)]
public struct UnityComponent<T> : IEcsComponent, IEnumerable<T>//IntelliSense hack
public readonly struct UnityComponent<T> : IEcsComponent, IEnumerable<T>//IntelliSense hack
where T : Component
{
public T obj;
public readonly T obj;
public UnityComponent(T obj) => this.obj = obj;
IEnumerator<T> IEnumerable<T>.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<T>();
component = new UnityComponent<T>(gameObject.GetComponent<T>());
}
}

View File

@ -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
{

View File

@ -1,28 +0,0 @@
using System;
namespace DCFApixels.DragonECS
{
public static class UnityWorldProvider<TWorld>
{
private static TWorld _world;
public static TWorld Get(Func<TWorld> 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;
}
}
}