Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to find nodes from different scenes is multi-scene?

Discussion in 'Editor & General Support' started by cris177, Aug 25, 2022.

  1. cris177

    cris177

    Joined:
    Oct 8, 2021
    Posts:
    8
    I want to have a multi-scene in which a main scene is always loaded but when other scenes are loaded the main scene keeps loaded but hidden.

    The way I thought to do this is to create a scene controller interface with the methods hide, show, etc, and that each scene will have an object with a component of a class of that interface.

    So I want to say for example something similar to this:

    Code (CSharp):
    1. scene1.Find("controller").GetComponent<SceneController>().hide()
    2. scene2.Find("controller").GetComponent<SceneController>().hide()
    But the scenes doesn't get saved into variables scene1, scene2, ..., that has a method Find. So what's the way to find these "controller" objects from specific scenes? I am also open to suggestions on how to do this in another way.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I would do something like this in the SceneController script:
    Code (CSharp):
    1. static Dictionary<Scene, SceneController> sceneDict = new();
    2.  
    3. public SceneController GetControllerForScene(Scene s) {
    4.   return sceneDict[s];
    5. }
    6.  
    7. void Awake() {
    8.   sceneDict[gameObject.scene] = this;
    9. }
    10.  
    11. void OnDestroy() {
    12.   sceneDict.Remove(gameObject.scene);
    13. }
    14.  
     
  3. cris177

    cris177

    Joined:
    Oct 8, 2021
    Posts:
    8
    Great, something like this could work, how would you initiate these scenes though? Any idea?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Additive scenes are very powerful in Unity. My advice would be to keep it as simple as possible as you proceed, breaking things up in simple large chunks at first, then perhaps into finer parts as you evolve your solution.

    Here are some additive scene loading notes to consider:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It's a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases, especially when working on a team and with source control (which you SHOULD always be using!).

    Two similar examples of checking if everything is ready to go:

    https://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713

    https://forum.unity.com/threads/uni...on-before-other-scripts.1153739/#post-7401794
     
  6. cris177

    cris177

    Joined:
    Oct 8, 2021
    Posts:
    8
    Very complete answer, this should help, thanks!
     
    Kurt-Dekker likes this.