Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Resources.Load and Entities

Discussion in 'Entity Component System' started by Naewulf, Mar 8, 2023.

  1. Naewulf

    Naewulf

    Joined:
    Jun 17, 2019
    Posts:
    23
    Hello guys. I'm aware this is probably a pretty noob question but I've been struggling (for quite some time to be honest) with something here and I hope some of you can lend me a hand if possible. I can't find a good way to load resources dynamically and convert them into entities for future instantiation.

    I tried to create a monobehaviour with a baker that has only one job: to loop through my Resources.LoadAll<MY_MONOBEHAVIOUR_TYPE_HERE> array and add them to a Dynamic Buffer. That feels kinda like a hack to me, especially since the baker runs on every change on the object, and because of that the assets will end up loading more than once. For example, every time I stop running the game the baker runs again.

    Can't have those assets loading multiple times... I even created a bool variable in the baker class to control if the baker logic has already been run, to avoid baking again, and I think it worked, but I can't shake the feeling that there are better ways of doing this.

    I also can't use a System to load those assets on its OnCreate or OnUpdate functions (and then disable it) because there's no way (that I'm aware of) to convert from GameObject Prefabs to Entities at runtime.

    Loading the assets with, for example, an Awake() function on a "real" monobehaviour that is outside the subscene, and then set them on the monobehaviour that's inside the subscene to force a baking of those assets, also does not feel like an option. If that's possible, I don't know how to do it, since even inside a System, the Entities.ForEach don't seem to be finding my monobehaviour script on the object that's been converted by the subscene.

    Back when this code worked without ECS, all I needed to do was "Resource.LoadAll", then loop through each of them, while adding them into a "Dictionary<string, GameObject> dictionary". Every time I needed something to pop on the screen I would just access that script and instantiate a 'dictionary["assetkeystring"]', but with ECS I still had no luck. Even if I manage to create my dictionary somehow I'm gonna need to replace the string keys for something like an int index, since stuff like NativeHashMap dont seem to like nullable types very much.

    Anyway... any help will be appreciated. Thanks!
     
    Last edited: Mar 8, 2023
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,349
    Bakers only work in the Editor. So at that point, instead of using Resources.Load, you should be using AssetDatabase APIs and putting your prefabs in some other directory that isn't Resources, since you don't actually want these GameObjects at runtime (you want the entities instead).

    On top of that, you will need some way to tell the Baker that its dependencies are "dirty" so that the baker reruns (subscene open) or the subscene reimports (subscene closed) whenever you add or remove a prefab to your directory.
     
  3. Naewulf

    Naewulf

    Joined:
    Jun 17, 2019
    Posts:
    23
    The AssetDatabase is something that only works with the editor too right? So, the idea would be to use the editor tools to automate the task that otherwise would be done manually like linking the prefabs into a monobehavior inside a subscene to get advantage of the baking instead of dynamically loading the assets like I'm doing? Like, a function that runs when I click a button in the editor? Have you ever seen any examples of something like that using those subscene baked monobehaviours?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,349
    Yes.

    I'm sure some people have them. I don't.
     
    Naewulf likes this.