A demo project that added Addressables Scene Management support to Mirror Networking.
by Shepherd Zhu
- Change scenes from Addressables across the server.
- Host/Server side
- Client side
- Support og's onlineScene and offlineScene.
- Addressables
- Mirror Networking (Demo project is using v90. But it should be good with compatibility as long as no major API change presents. )
- ParrelSync (Not required. Demo project is using this to test without building. )
It's sad that Mirror's Scene Management does not support Addressables. I have been googling a solution for a while and nothing useful.
Someone from Reddit suggested using Fishnet which has Addressables support. But that's not gonna happen during the mid of the project. So I started writing this and it works well for me. I really hope this repo can help/inspire anyone who is also struggling.
And tbh, this is not a really complicated work as long as you know how Mirror manage scenes.
I put all custom scripts and 2 demo scenes (which is derived from TanksDemo) under _Project folder.
If you wish to implement Addressables into Mirror's scene management on your own. You have to do the following:
- Create your own
NetworkManagerby using the Mirror's template. (if you have one, please skip this step. ) - Change the type of 'loadingSceneAsync' to 'AsyncOperationHandle'.
public static new AsyncOperationHandle<SceneInstance> loadingSceneAsync;- Override/rewrite nearly all functions under
Scene Managementcode block including but not limited toServerChangeScene,OnClientSceneChanged,ClientChangeScene,OnClientSceneInternalandUpdateScene.
PS: We mostly replace the line that using SceneManager with Addressable. For example:
loadingSceneAsync = Addressables.LoadSceneAsync(newSceneName);
- One thing you need to pay attention is
NetworkClient.AddPlayercalled by 'UpdateScene' function. SinceloadingSceneAsyncis now a struct. You can't set it to null and it takes a while to makeloadingSceneAsync.IsValid()set to false after finish the loading. So I set the struct todefaultto preventFinishLoadScenefrom getting called multiple times.
Now it should be good to go and don't forget to add your scene into Addressables Group. Happy programming!
- Head to
AddressableTanks_01scene. - Hit Play button.
- Start a host or server.
- Change to
AddressableTanks_01orAddressableTanks_02or whatever you like by typing in the TextField (theOnGUIthingy. Check showcase above for usage. ) on the top left of your screen. - Check whether
MyNetworkManageris working properly.
If you like my work, please give this repo a star and follow my social media.
If you encounter bugs/request a feature, please create a ticket in Issues section.