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.

ECSResources.cs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Svelto.DataStructures;
  2. namespace Svelto.ECS.Experimental
  3. {
  4. public struct ECSResources<T>
  5. {
  6. internal uint id;
  7. public static implicit operator T(ECSResources<T> ecsString) { return ResourcesECSDB<T>.FromECS(ecsString.id); }
  8. }
  9. static class ResourcesECSDB<T>
  10. {
  11. internal static readonly FasterList<T> _resources = new FasterList<T>();
  12. internal static uint ToECS(T resource)
  13. {
  14. _resources.Add(resource);
  15. return (uint)_resources.Count;
  16. }
  17. public static T FromECS(uint id)
  18. {
  19. if (id - 1 < _resources.Count)
  20. return _resources[(int) id - 1];
  21. return default(T);
  22. }
  23. }
  24. public static class ResourceExtensions
  25. {
  26. public static void Set<T>(ref this ECSResources<T> resource, T newText)
  27. {
  28. if (resource.id != 0)
  29. ResourcesECSDB<T>._resources[(int) resource.id] = newText;
  30. else
  31. resource.id = ResourcesECSDB<T>.ToECS(newText);
  32. }
  33. public static void Set(ref this ECSString resource, string newText)
  34. {
  35. if (resource.id != 0)
  36. ResourcesECSDB<string>._resources[(int) resource.id] = newText;
  37. else
  38. resource.id = ResourcesECSDB<string>.ToECS(newText);
  39. }
  40. }
  41. }