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-03-09 09:56:45 +08:00
[DisallowMultipleComponent]
2024-03-11 05:43:56 +08:00
[AddComponentMenu(EcsConsts.FRAMEWORK_NAME + "/" + nameof(AutoEntityCreator), 30)]
2024-06-11 02:30:10 +08:00
[MetaColor(MetaColor.Cyan)]
2024-06-13 18:04:47 +08:00
[MetaGroup(EcsUnityConsts.PACK_GROUP, EcsUnityConsts.ENTITY_BUILDING_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, nameof(MonoBehaviour) + ". Automatically creates an entity in the selected world and connects it to EcsEntityConnect.")]
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
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
}
private void Start ( )
{
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 ( )
{
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 ( )
{
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-28 18:43:42 +08:00
2024-03-04 07:38:38 +08:00
private void InitConnect ( EcsEntityConnect connect , EcsWorld world )
{
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
}