DragonECS-Unity/src/Buildin/UnityComponent.cs

130 lines
4.9 KiB
C#
Raw Normal View History

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-03-06 23:29:54 +08:00
internal static class UnityComponentConsts
{
2024-05-16 19:59:57 +08:00
internal const string UNITY_COMPONENT_NAME = "UnityComponent";
2024-05-01 15:02:54 +08:00
public static readonly MetaGroup BaseGroup = new MetaGroup(UNITY_COMPONENT_NAME);
public static readonly MetaGroup ColliderGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Collider/");
public static readonly MetaGroup JointGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Joint/");
2024-03-06 23:29:54 +08:00
}
2023-05-07 00:50:44 +08:00
[Serializable]
2024-06-13 18:04:47 +08:00
[MetaColor(MetaColor.Cyan)]
[MetaGroup(EcsUnityConsts.PACK_GROUP, EcsConsts.COMPONENTS_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, "Component-reference to Unity object for EcsPool.")]
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;
}
2024-03-09 03:35:01 +08:00
IEnumerator<T> IEnumerable<T>.GetEnumerator() //IntelliSense hack
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator() //IntelliSense hack
{
throw new NotImplementedException();
}
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); }
2023-05-07 00:50:44 +08:00
}
2024-03-03 03:51:49 +08:00
#region Unity Component Templates
public class UnityComponentTemplate<T> : ComponentTemplateBase<UnityComponent<T>> where T : Component
2023-06-28 15:08:09 +08:00
{
2024-03-06 23:29:54 +08:00
public override string Name
{
get { return typeof(T).Name; }
}
public override MetaGroup Group
{
get { return UnityComponentConsts.BaseGroup; }
}
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
}
}
2023-05-07 00:50:44 +08:00
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentRigitBodyInitializer : UnityComponentTemplate<Rigidbody> { }
2023-05-07 00:50:44 +08:00
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentAnimatorInitializer : UnityComponentTemplate<Animator> { }
2023-05-07 00:50:44 +08:00
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentCharacterControllerInitializer : UnityComponentTemplate<CharacterController> { }
#endregion
2023-05-07 00:50:44 +08:00
2024-03-03 03:51:49 +08:00
#region Collider Templates
2023-05-07 00:50:44 +08:00
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentColliderTemplate : UnityComponentTemplate<Collider>
2023-05-07 00:50:44 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.ColliderGroup; } }
2023-05-07 00:50:44 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentBoxColliderTemplate : UnityComponentTemplate<BoxCollider>
2023-05-07 00:50:44 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.ColliderGroup; } }
2023-05-07 00:50:44 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentSphereColliderTemplate : UnityComponentTemplate<SphereCollider>
2023-05-07 00:50:44 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.ColliderGroup; } }
2023-05-07 00:50:44 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentCapsuleColliderTemplate : UnityComponentTemplate<CapsuleCollider>
2023-05-07 00:50:44 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.ColliderGroup; } }
2023-05-07 00:50:44 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentMeshColliderTemplate : UnityComponentTemplate<MeshCollider>
2023-05-07 00:50:44 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.ColliderGroup; } }
2023-05-07 00:50:44 +08:00
}
#endregion
2024-03-03 03:51:49 +08:00
#region Joint Templates
2023-05-23 01:48:54 +08:00
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentJointTemplate : UnityComponentTemplate<Joint>
2023-05-23 01:48:54 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.JointGroup; } }
2023-05-23 01:48:54 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentFixedJointTemplate : UnityComponentTemplate<FixedJoint>
2023-05-23 01:48:54 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.JointGroup; } }
2023-05-23 01:48:54 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentCharacterJointTemplate : UnityComponentTemplate<CharacterJoint>
2023-05-23 01:48:54 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.JointGroup; } }
2023-05-23 01:48:54 +08:00
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentConfigurableJointTemplate : UnityComponentTemplate<ConfigurableJoint>
2023-05-23 01:48:54 +08:00
{
2024-03-06 23:29:54 +08:00
public override MetaGroup Group { get { return UnityComponentConsts.JointGroup; } }
2023-05-23 01:48:54 +08:00
}
#endregion
2023-05-07 00:50:44 +08:00
}