2024-06-13 18:04:47 +08:00
using DCFApixels.DragonECS.Unity ;
using System ;
2023-05-07 00:50:44 +08:00
using System.Collections ;
using System.Collections.Generic ;
2024-05-16 19:59:57 +08:00
using System.Runtime.CompilerServices ;
2023-05-07 00:50:44 +08:00
using UnityEngine ;
namespace DCFApixels.DragonECS
{
2024-10-12 14:37:21 +08:00
using static EcsConsts ;
2025-03-23 19:21:36 +08:00
2023-05-07 00:50:44 +08:00
[Serializable]
2024-09-16 19:30:49 +08:00
[MetaColor(MetaColor.DragonCyan)]
2024-10-12 14:37:21 +08:00
[MetaGroup(EcsUnityConsts.PACK_GROUP, COMPONENTS_GROUP)]
[MetaDescription(AUTHOR, "Component-reference to Unity object for EcsPool.")]
2025-03-19 16:31:32 +08:00
[MetaID("DragonECS_734F667C9201B80F1913388C2A8BB689")]
2024-10-11 23:29:48 +08:00
[MetaTags(MetaTags.ENGINE_MEMBER)]
2026-04-14 17:06:58 +08:00
[MetaProxy(typeof(UnityComponent<>.MetaProxy))]
2023-06-30 01:13:49 +08:00
public struct UnityComponent < T > : IEcsComponent , IEnumerable < T > //IntelliSense hack
2023-06-28 15:08:09 +08:00
where T : Component
2023-05-07 00:50:44 +08:00
{
2023-06-30 01:13:49 +08:00
public T obj ;
2024-05-16 19:59:57 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 03:51:49 +08:00
public UnityComponent ( T obj )
{
this . obj = obj ;
}
2025-03-10 13:08:18 +08:00
IEnumerator < T > IEnumerable < T > . GetEnumerator ( ) { throw new NotSupportedException ( ) ; } //IntelliSense hack
IEnumerator IEnumerable . GetEnumerator ( ) { throw new NotSupportedException ( ) ; } //IntelliSense hack
2024-05-16 19:59:57 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator T ( UnityComponent < T > a ) { return a . obj ; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UnityComponent < T > ( T a ) { return new UnityComponent < T > ( a ) ; }
2025-03-10 13:08:18 +08:00
public override string ToString ( )
{
2026-03-15 21:57:40 +08:00
return $"UnityComponent<{typeof(T).GetMeta().TypeName}>" ;
2025-03-10 13:08:18 +08:00
}
2026-04-14 17:06:58 +08:00
private class MetaProxy : MetaProxyBase
{
protected TypeMeta Meta = typeof ( T ) . GetMeta ( ) ;
public override string Name { get { return Meta ? . Name ; } }
public override MetaColor ? Color { get { return Meta ! = null & & Meta . IsCustomColor ? Meta . Color : null ; } }
public override MetaGroup Group { get { return Meta ? . Group ; } }
public override MetaDescription Description { get { return Meta ? . Description ; } }
public override IEnumerable < string > Tags { get { return Meta ? . Tags ; } }
public MetaProxy ( Type type ) : base ( type ) { }
}
2023-05-07 00:50:44 +08:00
}
2025-03-17 16:00:55 +08:00
2025-03-23 19:21:36 +08:00
internal static class UnityComponentConsts
{
internal const string UNITY_COMPONENT_NAME = "UnityComponent" ;
public static readonly MetaGroup BaseGroup = MetaGroup . FromName ( UNITY_COMPONENT_NAME ) ;
}
2024-09-16 19:30:49 +08:00
[MetaColor(MetaColor.DragonCyan)]
2024-10-12 14:37:21 +08:00
[MetaGroup(EcsUnityConsts.PACK_GROUP, OTHER_GROUP)]
2024-10-19 00:05:07 +08:00
[MetaDescription(AUTHOR, "Template for UnityComponent<T>")]
2025-03-19 16:31:32 +08:00
[MetaID("DragonECS_13DAACF9910155DD27F822442987E0AE")]
2026-04-14 10:15:44 +08:00
[MetaProxy(typeof(UnityComponentTemplate<>.UnityComponentMetaProxy))]
2024-09-16 19:30:49 +08:00
public abstract class UnityComponentTemplate < T > : ComponentTemplateBase < UnityComponent < T > > where T : Component
2023-06-28 15:08:09 +08:00
{
2024-04-22 17:20:10 +08:00
public sealed override void Apply ( short worldID , int entityID )
2024-03-03 03:51:49 +08:00
{
2024-03-07 03:24:02 +08:00
EcsWorld . GetPoolInstance < EcsPool < UnityComponent < T > > > ( worldID ) . TryAddOrGet ( entityID ) = component ;
2024-03-03 03:51:49 +08:00
}
public override void OnValidate ( UnityEngine . Object obj )
2023-06-28 15:08:09 +08:00
{
if ( component . obj = = null )
2024-03-03 03:51:49 +08:00
{
if ( obj is GameObject go )
{
component . obj = go . GetComponent < T > ( ) ;
}
}
2023-06-28 15:08:09 +08:00
}
2026-04-14 10:15:44 +08:00
protected class UnityComponentMetaProxy : ComponentTemplateMetaProxy
{
public override string Name { get { return typeof ( T ) . GetMeta ( ) . Name ; } }
public override MetaGroup Group { get { return UnityComponentConsts . BaseGroup ; } }
2026-04-15 11:12:59 +08:00
public override MetaColor ? Color { get { return MetaColor . DragonCyan ; } }
public override MetaDescription Description { get { return new MetaDescription ( AUTHOR , $"IEcsComponent component. Holds a reference to a Unity {Name} component." ) ; } }
2026-04-14 10:15:44 +08:00
public UnityComponentMetaProxy ( Type type ) : base ( type ) { }
}
2023-06-28 15:08:09 +08:00
}
2025-03-23 10:36:18 +08:00
}