Search Unity

Addressables AssetReference in IComponentData

Discussion in 'Entity Component System' started by Justin_Larrabee, Jun 4, 2019.

  1. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    I know that AssetReference is a reference type so it cannot be directly embedded, but I was wondering if there is a plan to have a built-in ECS type that can integrate with the Addressables system.

    Right now I intend to use a NativeString to stick the AssetReference GUID into, but it's not very safe since it is assuming that the RuntimeKey will remain a string (it's already changed before from a Hash128). Also, the NativeString containers built into the ECS runtime aren't ideal for storing a GUID. NativeString64 is too small and NativeString512 is far too big. I will end up copying NativeString64 and making it slightly bigger.
     
    lclemens likes this.
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Blob assets. The entities package has a BlobificationTests.cs that shows usage.
     
  3. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    Blob assets do not solve this problem as they are also for pure value types, a BlobPtr to a value type, or a BlobArray of value types.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ya I was thinking it has a string. But for a GUID why not just store a NativeArray<byte> ? That should be future proof.
     
  5. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    You can't embed a NativeArray into IComponentData or Blob assets.

    The problem as I mentioned above is that 1) assuming the AssetReference.RuntimeKey from the Addressables package is actually a string (the API is object) breaks encapsulation and could very easily break in a future release of that package, and 2) the only hack that works currently is to dump the string GUID from the AssetReference into a NativeString512 and waste hundreds of bytes or do what I did and copy NativeString64 and make it basically NativeString66 so a full GUID will fit (you could of course use a byte representation but the problem is the same).
     
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    BlobArray won't work?
     
  7. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    You could do a BlobArray<char> I suppose, but it ends up being an identical solution to using a NativeString but with more code verbosity dealing with the Blob API.
     
    lclemens likes this.