DragonECS-Unity/src/Connectors/AutoEntityCreator.cs

92 lines
2.2 KiB
C#
Raw Normal View History

2024-03-04 03:00:45 +08:00
using UnityEngine;
namespace DCFApixels.DragonECS
{
2024-03-09 09:56:45 +08:00
[DisallowMultipleComponent]
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-09 09:42:04 +08:00
#region Methods
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;
}
private void InitConnect(EcsEntityConnect connect, EcsWorld world)
{
2024-03-10 09:50:20 +08:00
connect.ConnectWith(world.NewEntityLong(), true);
2024-03-04 07:38:38 +08:00
}
2024-03-09 09:42:04 +08:00
#endregion
2024-03-04 07:38:38 +08:00
2024-03-09 09:42:04 +08:00
#region Editor
2024-03-04 07:38:38 +08:00
#if UNITY_EDITOR
2024-03-09 22:36:35 +08:00
[ContextMenu("Autoset")]
2024-03-04 07:38:38 +08:00
internal void Autoset_Editor()
{
2024-03-09 22:36:35 +08:00
foreach (var connect in GetComponentsInChildren<EcsEntityConnect>())
{
if (connect.GetComponentInParent<AutoEntityCreator>() == this)
{
_connect = connect;
AutoResolveWorldProviderDependensy();
break;
}
}
}
[ContextMenu("Autoset Cascade")]
internal void AutosetCascade_Editor()
{
foreach (var target in GetComponentsInChildren<AutoEntityCreator>())
{
target.Autoset_Editor();
}
2024-03-04 07:38:38 +08:00
}
#endif
2024-03-09 09:42:04 +08:00
#endregion
2024-03-04 07:38:38 +08:00
}
2024-03-06 21:37:21 +08:00
}