DragonECS-Unity/src/Buildin/UnityComponents.cs

101 lines
3.7 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
{
[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
{
public override string Name => "UnityComponent/" + typeof(T).Name;
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
{
public override string Name => "UnityComponent/Collider/" + nameof(Collider);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentBoxColliderTemplate : UnityComponentTemplate<BoxCollider>
2023-05-07 00:50:44 +08:00
{
public override string Name => "UnityComponent/Collider/" + nameof(BoxCollider);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentSphereColliderTemplate : UnityComponentTemplate<SphereCollider>
2023-05-07 00:50:44 +08:00
{
public override string Name => "UnityComponent/Collider/" + nameof(SphereCollider);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentCapsuleColliderTemplate : UnityComponentTemplate<CapsuleCollider>
2023-05-07 00:50:44 +08:00
{
public override string Name => "UnityComponent/Collider/" + nameof(CapsuleCollider);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentMeshColliderTemplate : UnityComponentTemplate<MeshCollider>
2023-05-07 00:50:44 +08:00
{
public override string Name => "UnityComponent/Collider/" + nameof(MeshCollider);
}
#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
{
public override string Name => "UnityComponent/Joint/" + nameof(Joint);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentFixedJointTemplate : UnityComponentTemplate<FixedJoint>
2023-05-23 01:48:54 +08:00
{
public override string Name => "UnityComponent/Joint/" + nameof(FixedJoint);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentCharacterJointTemplate : UnityComponentTemplate<CharacterJoint>
2023-05-23 01:48:54 +08:00
{
public override string Name => "UnityComponent/Joint/" + nameof(CharacterJoint);
}
[Serializable]
2024-03-03 03:51:49 +08:00
public sealed class UnityComponentConfigurableJointTemplate : UnityComponentTemplate<ConfigurableJoint>
2023-05-23 01:48:54 +08:00
{
public override string Name => "UnityComponent/Joint/" + nameof(ConfigurableJoint);
}
#endregion
2023-05-07 00:50:44 +08:00
}