简体   繁体   中英

Unity Scene-to-Scene inter-object communication: HOW?

Unity has an ability to have one scene act as a host for other sub-scenes, additively and asynchronously loaded, later in the game.

What are the ways an object in a host scene can know of and be sure of communicating with an object in an additively loaded sub-scene?

And the inverse, how can an object in the additively loaded scene communicate with an object in its host scene?

First, in Unity's UnityEngine.SceneManagement package, you can use SceneManager.GetSceneByName("scene name here") which returns Scene . If the scene is null, then the scene is not yet loaded. If it is, you can use isLoaded to check if it is loaded, SceneManager . You can either use Find after that, but I do not recommend this as it is a slow process.

Second, if the object you are referring to is a GameObject and you need a component attached to it, you can use Singleton pattern so you can access the only instance of the script. You can just assign instance = null under OnDestroy so that the instance will be gone when the scene is unloaded. This option is viable if the script is in the host scene since, I assume, it is not unloaded/destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM