Search Unity

Self-updating SceneReference type

Discussion in 'Made With Unity' started by gametr4x, Oct 2, 2014.

  1. gametr4x

    gametr4x

    Joined:
    Apr 22, 2009
    Posts:
    86
    I was looking into ways to prevent having to load scenes using string/int, because if you rename the scene the string is incorrect, or if the build settings change the int would be incorrect.

    What I came up with was a SceneReference class that stores the GUID, gets the correct name, and a PostProcessScene function that auto-updates the name compile-time. All that's required is using it as a public variable, and dragging a scene file onto it. It implicitly casts to string and Object, so you use it directly as input into Application.LoadLevel. For example:

    Code (CSharp):
    1.  
    2. public SceneReference myScene;
    3.  
    4. void Update()
    5. {
    6.    Application.LoadLevel( myScene );
    7. }
    8.  
    Here's a link to the unitypackage: http://bit.ly/1uh53fj

    The way the PostProcessScene currently works is a little bit of a hassle (get every MonoBehaviour, find all SceneReference fields, update them), so if anybody has any ideas how to do that more effectively I'm all ears.