Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3KB

  1. using System.Runtime.CompilerServices;
  2. namespace Svelto.ECS.Internal
  3. {
  4. delegate void SetEGIDWithoutBoxingActionCast<T>(ref T target, EGID egid) where T : struct, IEntityComponent;
  5. static class SetEGIDWithoutBoxing<T> where T : struct, IEntityComponent
  6. {
  7. public static readonly SetEGIDWithoutBoxingActionCast<T> SetIDWithoutBoxing = MakeSetter();
  8. public static void Warmup() { }
  9. static SetEGIDWithoutBoxingActionCast<T> MakeSetter()
  10. {
  11. if (ComponentBuilder<T>.HAS_EGID)
  12. {
  13. #if !ENABLE_IL2CPP
  14. var method = typeof(Trick).GetMethod(nameof(Trick.SetEGIDImpl)).MakeGenericMethod(typeof(T));
  15. return (SetEGIDWithoutBoxingActionCast<T>) System.Delegate.CreateDelegate(
  16. typeof(SetEGIDWithoutBoxingActionCast<T>), method);
  17. #else
  18. return (ref T target, EGID egid) =>
  19. {
  20. var needEgid = (target as INeedEGID);
  21. needEgid.ID = egid;
  22. target = (T) needEgid;
  23. };
  24. #endif
  25. }
  26. return null;
  27. }
  28. static class Trick
  29. {
  30. public static void SetEGIDImpl<U>(ref U target, EGID egid) where U : struct, INeedEGID
  31. {
  32. target.ID = egid;
  33. }
  34. }
  35. }
  36. }