2025-03-14 17:02:34 +08:00
|
|
|
#if DISABLE_DEBUG
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
#endif
|
2024-06-13 18:04:47 +08:00
|
|
|
using DCFApixels.DragonECS.Unity;
|
2024-03-04 03:00:45 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2024-10-19 00:05:07 +08:00
|
|
|
using static EcsConsts;
|
|
|
|
|
|
2024-03-09 09:56:45 +08:00
|
|
|
[DisallowMultipleComponent]
|
2024-10-19 00:05:07 +08:00
|
|
|
[AddComponentMenu(FRAMEWORK_NAME + "/" + nameof(AutoEntityCreator), 30)]
|
2024-10-12 14:37:21 +08:00
|
|
|
[MetaColor(MetaColor.DragonCyan)]
|
2024-06-13 18:04:47 +08:00
|
|
|
[MetaGroup(EcsUnityConsts.PACK_GROUP, EcsUnityConsts.ENTITY_BUILDING_GROUP)]
|
2024-10-19 00:05:07 +08:00
|
|
|
[MetaDescription(AUTHOR, nameof(MonoBehaviour) + ". Automatically creates an entity in the selected world and connects it to EcsEntityConnect.")]
|
2025-03-19 16:31:32 +08:00
|
|
|
[MetaID("DragonECS_D699B3809201285A46DDF91BCF0540A7")]
|
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
|
|
|
|
2025-03-10 13:08:18 +08:00
|
|
|
private bool _started;
|
2024-03-04 03:00:45 +08:00
|
|
|
|
2024-03-04 07:38:38 +08:00
|
|
|
#region Properties
|
2024-03-28 18:43:42 +08:00
|
|
|
public EcsEntityConnect Connect
|
|
|
|
|
{
|
|
|
|
|
get { return _connect; }
|
|
|
|
|
}
|
|
|
|
|
public EcsWorldProviderBase World
|
|
|
|
|
{
|
|
|
|
|
get { return _world; }
|
|
|
|
|
}
|
2024-03-04 07:38:38 +08:00
|
|
|
#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
|
|
|
}
|
2025-03-10 13:08:18 +08:00
|
|
|
private void Start() { ManualStart(); }
|
2024-03-04 07:38:38 +08:00
|
|
|
#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()
|
|
|
|
|
{
|
2024-03-28 18:43:42 +08:00
|
|
|
_world = AutoGetWorldProvider();
|
|
|
|
|
}
|
|
|
|
|
protected virtual EcsWorldProviderBase AutoGetWorldProvider()
|
|
|
|
|
{
|
|
|
|
|
return EcsDefaultWorldSingletonProvider.Instance;
|
2024-03-04 07:38:38 +08:00
|
|
|
}
|
|
|
|
|
public void ManualStart()
|
|
|
|
|
{
|
2025-03-10 13:08:18 +08:00
|
|
|
if (_started) { return; }
|
|
|
|
|
ManualCreate();
|
|
|
|
|
_started = true;
|
2024-03-04 07:38:38 +08:00
|
|
|
}
|
2025-03-10 13:08:18 +08:00
|
|
|
public void ManualCreate()
|
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-10-31 19:13:10 +08:00
|
|
|
InitConnect(_connect, _world.GetRaw());
|
2024-03-04 07:38:38 +08:00
|
|
|
}
|
2024-03-28 18:43:42 +08:00
|
|
|
|
2024-03-04 07:38:38 +08:00
|
|
|
private void InitConnect(EcsEntityConnect connect, EcsWorld world)
|
|
|
|
|
{
|
2024-10-31 19:13:10 +08:00
|
|
|
if (connect.IsConnected) { return; }
|
2024-03-28 18:43:42 +08:00
|
|
|
connect.ConnectWith(CreateEntity(world), true);
|
|
|
|
|
}
|
|
|
|
|
protected virtual entlong CreateEntity(EcsWorld world)
|
|
|
|
|
{
|
|
|
|
|
return world.NewEntityLong();
|
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
|
|
|
}
|