VPDPersonal / UnityFastTools

0 stars 0 forks source link

ECS Authoring #2

Open paulpach opened 3 months ago

paulpach commented 3 months ago

Unity used to have this, but they removed it. I suggest autogenerating authoring for trivial components. This would save a lot of work for ECS projects. For example:

// An example component for which we want to
// define an authoring component and a baker.
[GenerateAuthoring]
public struct EnergyShield : IComponentData
{
      public int HitPoints;
      public int MaxHitPoints;
      public float RechargeDelay;
      public float RechargeRate;
}

would generate:

// An authoring component for EnergyShield.
// By itself, an authoring component is just an ordinary MonoBehavior.
public partial class EnergyShieldAuthoring : MonoBehaviour
{
  public int HitPoint;
  public int MaxHitPoints;
  public float RechargeDelay;
  public float RechargeRate;

  // The baker for our EnergyShield authoring component.
  // For every GameObject in an entity subscene, baking creates a
  // corresponding entity. This baker is run once for every
  // EnergyShieldAuthoring instance that's attached to any GameObject in
  // the entity subscene.
  public class Baker : Baker<EnergyShieldAuthoring>
  {
      public override void Bake(EnergyShieldAuthoring authoring)
      {
          // The TransformUsageFlags specifies which transform components the
          // entity should have. 'None' means that it doesn't need any.
          var entity = GetEntity(TransformUsageFlags.None);

          // This simple baker adds just one component to the entity.
          AddComponent(entity, new EnergyShield
          {
              HitPoints = authoring.HitPoints,
              MaxHitPoints = authoring.MaxHitPoints,
              RechargeDelay = authoring.RechargeDelay,
              RechargeRate = authoring.RechargeRate,
          });
      }
  }
}

You could add options such as the name of the authoring component.

VPDPersonal commented 3 months ago

As I recall Unity removed this attribute due to performance issues. The problem here is registering the generated MonoBehaviour class due to Unity's own peculiarities