How to unify the management of various resources in the game

I have this idea, can I call a method when the game is loaded to cache all the resources of the entire game, and then use the Preload() method in the specific Scene to get the resources that I previously cached In this way, I can ensure that each Scene is coherent and won’t waste time loading resources.

I saw the add method of cache in this document, it seems that various resource objects can be addedhttps://photonstorm.github.io/phaser3-docs/Phaser.Cache.BaseCache.html#get__anchor

Has anyone done this before? I want to know how I load my cached resource objects into the camera. Do I need to create sprites to host resources?

The Texture/Audio Managers already cache everything. If you use preload in your first scene, you can use all those resources in any scene.
The ‘add’ method is useful if you don’t want to use preload, so if you have your own loader, or created a resource dynamically.

1 Like

I want to cache some resources in the first Scene to ensure the smooth playback of the following Scenes, so I implement a method to store resources by myself. Is this method feasible?

I don’t know how often I am going to have to repeat myself :slight_smile:
Preload everything you want in the first Scene. Then they are cached. Why would you want to make another cache?
Any following scene will not have to load anything, because the first scene already did that.

Because there are many resources in my project, if I cache all of them in the first Scene, it will take a lot of time, which is unacceptable :cry:

Then load them whenever you want. Do some in the first. some in the second. Whatever. But you don’t need to make your own cache manager. Whenever you load something, you can reference the cache by its key.

:smile:Thank you

To be clear, you can load resources any time you want. You don’t have to use the preload method. See for instance this example.

Does that mean all loaded resources remain in memory? Is there a way to control cache policy/eviction?

That’s how I interpret the docs, and it appears the class API will let you verify it experimentally:

The TextureManager is an EventEmitter that fires events, presumably when textures are loaded and unloaded. Also, there are methods for removing individual textures by name. I can’t tell how complete the capabilities are (e.g., the docs fail to even enumerate the event names that the TextureManager can emit), but there may be enough of a foundation for custom eviction logic.

hf;gl

1 Like