mirror of
https://github.com/DCFApixels/DragonECS-Unity.git
synced 2026-04-22 04:35:55 +08:00
85 lines
3.8 KiB
C#
85 lines
3.8 KiB
C#
using DCFApixels.DragonECS.Unity;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using UnityEngine;
|
|
|
|
namespace DCFApixels.DragonECS
|
|
{
|
|
using static EcsConsts;
|
|
|
|
[Serializable]
|
|
[MetaColor(MetaColor.DragonCyan)]
|
|
[MetaGroup(EcsUnityConsts.PACK_GROUP, COMPONENTS_GROUP)]
|
|
[MetaDescription(AUTHOR, "Component-reference to Unity object for EcsPool.")]
|
|
[MetaID("DragonECS_734F667C9201B80F1913388C2A8BB689")]
|
|
[MetaTags(MetaTags.ENGINE_MEMBER)]
|
|
[MetaProxy(typeof(UnityComponent<>.MetaProxy))]
|
|
public struct UnityComponent<T> : IEcsComponent, IEnumerable<T>//IntelliSense hack
|
|
where T : Component
|
|
{
|
|
public T obj;
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public UnityComponent(T obj)
|
|
{
|
|
this.obj = obj;
|
|
}
|
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() { throw new NotSupportedException(); }//IntelliSense hack
|
|
IEnumerator IEnumerable.GetEnumerator() { throw new NotSupportedException(); }//IntelliSense hack
|
|
[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); }
|
|
public override string ToString()
|
|
{
|
|
return $"UnityComponent<{typeof(T).GetMeta().TypeName}>";
|
|
}
|
|
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) { }
|
|
}
|
|
}
|
|
|
|
internal static class UnityComponentConsts
|
|
{
|
|
internal const string UNITY_COMPONENT_NAME = "UnityComponent";
|
|
public static readonly MetaGroup BaseGroup = MetaGroup.FromName(UNITY_COMPONENT_NAME);
|
|
}
|
|
[MetaColor(MetaColor.DragonCyan)]
|
|
[MetaGroup(EcsUnityConsts.PACK_GROUP, OTHER_GROUP)]
|
|
[MetaDescription(AUTHOR, "Template for UnityComponent<T>")]
|
|
[MetaID("DragonECS_13DAACF9910155DD27F822442987E0AE")]
|
|
[MetaProxy(typeof(UnityComponentTemplate<>.UnityComponentMetaProxy))]
|
|
public abstract class UnityComponentTemplate<T> : ComponentTemplateBase<UnityComponent<T>> where T : Component
|
|
{
|
|
public sealed override void Apply(short worldID, int entityID)
|
|
{
|
|
EcsWorld.GetPoolInstance<EcsPool<UnityComponent<T>>>(worldID).TryAddOrGet(entityID) = component;
|
|
}
|
|
public override void OnValidate(UnityEngine.Object obj)
|
|
{
|
|
if (component.obj == null)
|
|
{
|
|
if (obj is GameObject go)
|
|
{
|
|
component.obj = go.GetComponent<T>();
|
|
}
|
|
}
|
|
}
|
|
protected class UnityComponentMetaProxy : ComponentTemplateMetaProxy
|
|
{
|
|
public override string Name { get { return typeof(T).GetMeta().Name; } }
|
|
public override MetaGroup Group { get { return UnityComponentConsts.BaseGroup; } }
|
|
public override MetaColor? Color { get { return MetaColor.DragonCyan; } }
|
|
public override MetaDescription Description { get { return new MetaDescription(AUTHOR, $"Template for IEcsComponent component. Holds a reference to a Unity {Name} component."); } }
|
|
public UnityComponentMetaProxy(Type type) : base(type) { }
|
|
}
|
|
}
|
|
} |