Mirror icon indicating copy to clipboard operation
Mirror copied to clipboard

Async spawning to better support Addressables

Open James-Frowen opened this issue 4 years ago • 4 comments

Please explain the suggested feature in detail.

Addressables allows prefabs to be loaded from asset bundles at runtime. Prefabs are normally spawned via

Addressables.InstantiateAsync("AssetAddress");

or

Addressables.LoadAssetAsync<GameObject>("AssetAddress");

Both of these methods are asynchronous and have a completed event when they are finished.

How exactly does this keep you from releasing your game right now?

With the current spawn handlers within mirror it is not possible to call these methods as the handlers return a result right away.

The alternative is to list the prefab early to make sure they exist by the time mirror wants to spawn them, this reducing some of the benefits of being able to load and unload assets when they are needed.

Can you suggest a possible solution?

Add another version of the spawnHandlers that does not return the game object right away, but instead passes a callback into the handler. That callback would then be called by the user's handler after InstantiateAsync has finished. The callback would then apply the spawn message.

One possible problem with this may be future messages being received on the client before they have finished loading the prefab. We may need to add a way to queue up messages for a netId and apply them after the callback is called.

James-Frowen avatar Jul 03 '20 09:07 James-Frowen

Until we do something more concrete on this, WaitForCompletion makes it synchronous, so your custom spawn handler can effectively block for the loading of the addressable and return it.

MrGadget1024 avatar Feb 09 '23 15:02 MrGadget1024

WaitForCompletion will only work if you have pre-loaded the bundle, otherwise it will block the main thread till it is loaded (which could take seconds).

I've found the best thing to do is load the prefabs ahead of time and then register them with Mirror when they are loaded.

James-Frowen avatar Feb 10 '23 13:02 James-Frowen

WaitForCompletion will only work if you have pre-loaded the bundle, otherwise it will block the main thread till it is loaded (which could take seconds).

I've found the best thing to do is load the prefabs ahead of time and then register them with Mirror when they are loaded.

We load the catalog and then use WaitForCompletion to load specific prefabs from bundles based on their paths, it is fast for us, but I guess it depends on what you load.

FakeByte avatar Feb 10 '23 15:02 FakeByte