Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Intended way to manage assets

Discussion in 'Entity Component System' started by Danon5, May 4, 2023.

  1. Danon5

    Danon5

    Joined:
    Apr 12, 2019
    Posts:
    5
    Coming from "normal" Unity, I am very used to using ScriptableObjects to define configuration data for objects in the world. For example, I might have a singular "DroppedItem" prefab, but that same prefab can be assigned a DroppedItemConfig ScriptableObject to define the texture the dropped item has, the height it floats off the ground, the VFX that plays when it drops, etc. I am already used to database of ID -> config for network serialization purposes, as this is immutable data.

    However, when using Entities, I am having a hard time thinking of ways to keep most of the code burst-compatible while still having the flexibility of a configuration system. In my example above, I mentioned that one configuration might hold both blittable and non-blittable data (i.e. a texture and a height parameter). This means that referencing a configuration in a system disallows me from using Burst in the method I reference it in.

    To get around this, I looked at blob assets, as they seem like a great option for immutable data. However, blob assets can only hold value types—for obvious reasons. So, my question is, what is the intended way to configure data when that data might involve asset references? I see that there are WeakObjectReferences, but it seems I would need to manually need to load / release those, adding boilerplate to essentially everything that needs to load at any point in the game.

    I could create hashes for my assets and manually create / initializes databases for them so that I can store the hashes in the blobs, but that seems like a ton of work for something so simple. And where do I even do all this? Would all this database initialization be in a baker? In a system at runtime? I feel like the broad strokes are barely covered in the documentation.

    Some of the github samples cover aspects of what I am asking, but there is tons of code, and usually it's just for one type of asset (like a Mesh)! Imagine needing to make hundreds and hundreds of lines of baking code just to reference some various asset types.

    Literally any advice would be greatly appreciated, because I like many of the features. I am just finding it very difficult to even get a simple project off the ground.
     
    Last edited: May 4, 2023
    sirwhatevers, bb8_1 and apkdev like this.
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    This is not at all going to lead to a performant approach, but you can bake a ScriptableObject into its own Entity. Then your assets also become entities. You could even bake the ScriptableObject into an Entity that contains a managed component to the ScriptableObject (although there have been reports of this not working correctly in builds).
     
  3. Danon5

    Danon5

    Joined:
    Apr 12, 2019
    Posts:
    5
    Yes, but then I have to through the managed API to access the ScriptableObjects. I could just go full managed, but that's not good. I am wondering how to use Burst while also utilizing the flexibility of ScriptableObjects for configuration without creating some overly complex database system. If that's not possible, then I guess I have no choice but to overcomplicate it, but ideally there would be some established workflow for this kind of stuff.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    If the data is managed, then it becomes a managed component and can't be used in Burst. If the data is unmanaged, then bake it as an unmanaged component and use it in Burst.

    If you want more performance, then you need to get specific about your problem. Silver bullets don't exist here.
     
  5. Danon5

    Danon5

    Joined:
    Apr 12, 2019
    Posts:
    5
    Alright, I can see asking this kind of question here is not going to work. I apologize for the wasted time, I will figure it out myself.
     
  6. PolarTron

    PolarTron

    Joined:
    Jun 21, 2013
    Posts:
    87
    I'm just passing through here but I just wanted to say that your question is genuine and that you shouldn't feel sorry for asking it.
     
    sirwhatevers, bb8_1 and apkdev like this.
  7. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Sorry. Didn't mean to come off that way. Your question is valid, and you are not wasting anyone's time here. It is just very difficult to answer that isn't misleading.

    If I understand your question correctly (I'm probably not), you actually have two questions.

    1) How to bake shared resource assets and add them to prefabs?
    As generalized as this is, the answer is "you can't". But, if you were more specific, that answer could very easily turn into "you can". But it is a minefield, and a lot of the solutions for the types of things you may want to do require advanced concepts (notably baking systems) which are very hard to write correctly. Could Unity make this way better? Yes. But I don't want to derail this thread either.

    2) How to interact with VFX, Meshes, and Textures and stuff in Burst? How to bake these into Burst-friendly resources easily?
    If you want a handle to these resources, then what you should do is put these resources on an Entity, and use that Entity as a handle inside components of the other entities. But if you want to interact with those types directly in Burst, you can't. They are managed types. There are workarounds, but those workarounds have a lot nuances and are specific to each type.
     
    apkdev and bb8_1 like this.