DragonECS-Unity/src/Buildin/UnityComponents.cs

115 lines
4.3 KiB
C#
Raw Normal View History

2023-05-07 00:50:44 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DCFApixels.DragonECS
{
2024-03-06 23:29:54 +08:00
internal static class UnityComponentConsts
{
private const string UNITY_COMPONENT_NAME = "UnityComponent";
public static readonly MetaGroup BaseGroup = new MetaGroupRef(UNITY_COMPONENT_NAME);
public static readonly MetaGroup ColliderGroup = new MetaGroupRef($"{UNITY_COMPONENT_NAME}/Collider/");
public static readonly MetaGroup JointGroup = new MetaGroupRef($"{UNITY_COMPONENT_NAME}/Joint/");
}
2023-05-07 00:50:44 +08:00
[Serializable]
2024-03-03 03:51:49 +08:00
[MetaColor(255 / 3, 255, 0)]
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-03-03 03:51:49 +08:00
public UnityComponent(T obj)
{
this.obj = obj;
}
2023-05-07 00:50:44 +08:00
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack
}
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-03-03 03:51:49 +08:00
public sealed override void Apply(int worldID, int entityID)
{
EcsWorld.GetPool<EcsPool<UnityComponent<T>>>(worldID).TryAddOrGet(entityID) = component;
}
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
}