DragonECS-Unity/src/Extensions/EcsEntityConnect.cs

152 lines
4.4 KiB
C#
Raw Normal View History

2023-05-07 00:50:44 +08:00
using System.Runtime.CompilerServices;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public class EcsEntityConnect : MonoBehaviour
{
2023-06-22 14:30:03 +08:00
private sealed class Aspect : EcsAspect
2023-05-07 00:50:44 +08:00
{
public readonly EcsPool<UnityGameObject> unityGameObjects;
2023-06-22 14:30:03 +08:00
public Aspect(Builder b)
2023-05-07 00:50:44 +08:00
{
unityGameObjects = b.Include<UnityGameObject>();
}
}
private entlong _entity;
private EcsWorld _world;
[SerializeField]
private EntityTemplatePreset[] _entityTemplatePresets;
[SerializeField]
private EntityTemplate[] _entityTemplates;
internal void SetTemplates_Editor(EntityTemplate[] tempaltes)
{
_entityTemplates = tempaltes;
}
#region Properties
public entlong Entity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity;
}
public EcsWorld World
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _world;
}
public bool IsAlive
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity.IsAlive;
}
#endregion
public void ConnectWith(entlong entity, bool applyTemplates = false)
{
2023-06-22 14:40:26 +08:00
if (_entity.TryGetID(out int oldE) && _world != null)
2023-05-07 00:50:44 +08:00
{
2023-06-22 14:30:03 +08:00
var s = _world.GetAspect<Aspect>();
2023-05-07 00:50:44 +08:00
s.unityGameObjects.Del(oldE);
}
_world = null;
if (entity.TryGetID(out int newE))
{
_entity = entity;
_world = _entity.World;
2023-06-22 14:30:03 +08:00
var s = _world.GetAspect<Aspect>();
2023-05-07 00:50:44 +08:00
if (!s.unityGameObjects.Has(newE)) s.unityGameObjects.Add(newE) = new UnityGameObject(gameObject);
if (applyTemplates)
ApplyTemplates();
}
else
{
_entity = entlong.NULL;
}
}
public void ApplyTemplates() => ApplyTemplatesFor(_entity.ID);
public void ApplyTemplatesFor(int entityID)
{
foreach (var t in _entityTemplatePresets)
t.Apply(_world, entityID);
foreach (var t in _entityTemplates)
t.Apply(_world, entityID);
}
}
2023-06-28 15:08:09 +08:00
}
2023-05-07 00:50:44 +08:00
2023-06-06 17:52:12 +08:00
2023-06-28 15:08:09 +08:00
#if UNITY_EDITOR
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
namespace DCFApixels.DragonECS.Editors
{
using System.Collections.Generic;
using UnityEditor;
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
[CustomEditor(typeof(EcsEntityConnect))]
public class EcsEntityEditor : Editor
{
private EcsEntityConnect Target => (EcsEntityConnect)target;
private GUIStyle _greenStyle;
private GUIStyle _redStyle;
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
private bool _isInit = false;
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
private void Init()
{
if (_isInit)
return;
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
_greenStyle = EcsEditor.GetStyle(new Color32(75, 255, 0, 100));
_redStyle = EcsEditor.GetStyle(new Color32(255, 0, 75, 100));
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
_isInit = true;
}
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
public override void OnInspectorGUI()
{
Init();
if (Target.IsAlive)
GUILayout.Box("Connected", _greenStyle, GUILayout.ExpandWidth(true));
else
GUILayout.Box("Not connected", _redStyle, GUILayout.ExpandWidth(true));
2023-06-22 14:40:26 +08:00
2023-06-28 15:08:09 +08:00
if (Target.Entity.TryGetID(out int id))
EditorGUILayout.IntField(id);
else
EditorGUILayout.IntField(0);
GUILayout.Label(Target.Entity.ToString());
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
base.OnInspectorGUI();
2023-05-07 00:50:44 +08:00
2023-06-28 15:08:09 +08:00
if (GUILayout.Button("Autoset Templates"))
{
Target.SetTemplates_Editor(Target.GetComponents<EntityTemplate>());
2023-06-22 14:40:26 +08:00
2023-06-28 15:08:09 +08:00
EditorUtility.SetDirty(target);
}
if (GUILayout.Button("Autoset Templates Cascade"))
{
foreach (var item in Target.GetComponentsInChildren<EcsEntityConnect>())
2023-05-07 00:50:44 +08:00
{
2023-06-28 15:08:09 +08:00
item.SetTemplates_Editor(item.GetComponents<EntityTemplate>());
EditorUtility.SetDirty(item);
2023-05-07 00:50:44 +08:00
}
2023-06-28 15:08:09 +08:00
}
2023-06-06 17:52:12 +08:00
2023-06-28 15:08:09 +08:00
if (Target.IsAlive)
{
List<object> comps = new List<object>();
Target.World.GetComponents(Target.Entity.ID, comps);
GUILayout.TextArea(string.Join("\r\n", comps));
2023-05-07 00:50:44 +08:00
}
}
}
}
2023-06-28 15:08:09 +08:00
#endif