DragonECS-Unity/src/Connectors/AutoEntityCreator.cs

98 lines
2.3 KiB
C#
Raw Normal View History

2024-03-04 03:00:45 +08:00
using UnityEngine;
namespace DCFApixels.DragonECS
{
2024-03-04 07:38:38 +08:00
public class AutoEntityCreator : MonoBehaviour
2024-03-04 03:00:45 +08:00
{
2024-03-04 07:38:38 +08:00
[SerializeField]
private EcsEntityConnect _connect;
[SerializeField]
private EcsWorldProviderBase _world;
2024-03-04 03:00:45 +08:00
2024-03-04 07:38:38 +08:00
private bool _created;
2024-03-04 03:00:45 +08:00
2024-03-04 07:38:38 +08:00
#region Properties
public EcsEntityConnect Connect => _connect;
#endregion
2024-03-04 03:00:45 +08:00
2024-03-04 07:38:38 +08:00
#region UnityEvents
private void OnValidate()
{
if (_world == null)
2024-03-04 03:00:45 +08:00
{
2024-03-04 07:38:38 +08:00
AutoResolveWorldProviderDependensy();
2024-03-04 03:00:45 +08:00
}
2024-03-04 07:38:38 +08:00
}
private void Start()
{
2024-03-04 03:00:45 +08:00
2024-03-04 07:38:38 +08:00
CreateEntity();
}
#endregion
2024-03-04 03:00:45 +08:00
2024-03-04 07:38:38 +08:00
private void AutoResolveWorldProviderDependensy()
{
_world = EcsDefaultWorldSingletonProvider.Instance;
}
public void ManualStart()
{
CreateEntity();
}
private void CreateEntity()
{
if (_created)
2024-03-04 03:00:45 +08:00
{
2024-03-04 07:38:38 +08:00
return;
2024-03-04 03:00:45 +08:00
}
2024-03-04 07:38:38 +08:00
if (_world == null)
2024-03-04 03:00:45 +08:00
{
2024-03-04 07:38:38 +08:00
AutoResolveWorldProviderDependensy();
2024-03-04 03:00:45 +08:00
}
2024-03-04 07:38:38 +08:00
else
2024-03-04 03:00:45 +08:00
{
2024-03-04 07:38:38 +08:00
InitConnect(_connect, _world.GetRaw());
2024-03-04 03:00:45 +08:00
}
2024-03-04 07:38:38 +08:00
_created = true;
}
2024-03-04 03:00:45 +08:00
2024-03-04 07:38:38 +08:00
private void InitConnect(EcsEntityConnect connect, EcsWorld world)
{
connect.ConnectWith(world.NewEntityLong());
connect.ApplyTemplates();
}
#if UNITY_EDITOR
internal void Autoset_Editor()
{
_connect = GetComponentInChildren<EcsEntityConnect>();
2024-03-04 08:21:01 +08:00
AutoResolveWorldProviderDependensy();
2024-03-04 07:38:38 +08:00
}
#endif
}
}
#if UNITY_EDITOR
namespace DCFApixels.DragonECS.Unity.Editors
{
using UnityEditor;
[CustomEditor(typeof(AutoEntityCreator))]
[CanEditMultipleObjects]
public class AutoEntityCreatorEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Autoset"))
2024-03-04 03:00:45 +08:00
{
2024-03-04 07:38:38 +08:00
foreach (var tr in targets)
{
AutoEntityCreator creator = (AutoEntityCreator)tr;
creator.Autoset_Editor();
EditorUtility.SetDirty(creator);
}
2024-03-04 03:00:45 +08:00
}
}
}
2024-03-04 07:38:38 +08:00
}
#endif