deprimus / Tale

Prop manipulation utility designed for storytelling. Tale is the core of all our projects.
MIT License
3 stars 0 forks source link

Add a way to preload assets more cleanly #19

Open UnexomWid opened 2 years ago

UnexomWid commented 2 years ago

Whenever assets are loaded dynamically, it's a well known fact that freezes can occur. This is because Unity loads assets in a lazy way. While this is fine in some scenarios, Tale is often used in games which feature high quality textures and audio files. These files are large, and therefore induce noticeable freezes during the gameplay.

There should be a way to mitigate this, without having to manually add every single asset to the preload list. A possible solution would be to move the resource loading task from the Setup state to the action constructor. This would work, because Tale actions are usually enqueued in bulk, meaning that all of the resources will be loaded in one go. The actions are also usually enqueued during the transition between two scenes, so the player shouldn't notice any freezes.

In other words, instead of this:

void Run() {
    switch(state) {
        case State.SETUP:
            Resources.Load<...>(...);
    }
   ...
}

...what should be done is this:

DialogAction(...) {
     Resources.Load<...>(...)
}

The difference is that any errors will be caught at the time of enqueueing the actions, and not when the action itself runs. If the user relies on dynamically generating assets, which don't exist at the time of enqueueing the action, then the action should retry loading the resources during the setup state.

Also, there should be a config option for enabling or disabling this behavior (i.e. loading resources at the time of enqueueing).

UnexomWid commented 2 years ago

@AndrewIGD thoughts?

AndrewIGD commented 2 years ago

This works just fine, especially for non-GameObject assets such as sprites and audio clips, which don't require instantiation.

I'm not sure why someone would disable this, but there probably should be an option for this, just in case.

UnexomWid commented 3 months ago

There should also be an option to preload ALL game assets via Resources.LoadAll("", typeof(Object)), and that list should be stored inside TaleMaster such that they aren't unloaded until the game is closed.