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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Nifty Script for referencing scenes at run time.

Discussion in 'Scripting' started by DouglasPotesta, Sep 14, 2018.

  1. DouglasPotesta

    DouglasPotesta

    Joined:
    Nov 6, 2014
    Posts:
    108
    Don't mind me, I just felt an urge to post about this. Scene loading is notoriously annoying. Why is there no way to reference scenes like a normal unity object. Well today I had enough, I typed up this nifty piece of code. It was so simple. I can't believe I went this long without writing something like this. All those annoying times when I had to go back and fix a typo in a SceneManager.Load().
    The code snippet allows for scenes to be referenced in the editor. No more hard typing scene names. Sorry for all the annoying preprocessor directives.

    Code (CSharp):
    1. using UnityEngine;
    2. #if UNITY_EDITOR
    3. using UnityEditor;
    4. #endif
    5. [System.Serializable]
    6. public struct SceneReference
    7. #if UNITY_EDITOR
    8.     : ISerializationCallbackReceiver
    9. #endif
    10. {
    11. #if UNITY_EDITOR
    12.     public SceneAsset sceneAsset;
    13. #endif
    14.  
    15.     public string SceneName { get
    16.         {
    17. #if UNITY_EDITOR
    18.             if (sceneAsset == null) { return ""; }
    19.             return sceneAsset.name;
    20. #else
    21.             return sceneName;
    22. #endif
    23.         }
    24.     }
    25.  
    26.     [HideInInspector] [SerializeField]
    27.     private string sceneName;
    28.  
    29. #if UNITY_EDITOR
    30.     public void OnBeforeSerialize()
    31.     {
    32.         if (sceneAsset == null) { sceneName = ""; }
    33.         else { sceneName = sceneAsset.name; }
    34.     }
    35.  
    36.     public void OnAfterDeserialize()
    37.     {
    38.     }
    39. #endif
    40. }
    41.  
     
    BlackDragonBE and Deeeds like this.
  2. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    Sorry. I'm just now beginning the battle with scenes. How would/could/should this be used?
     
  3. DouglasPotesta

    DouglasPotesta

    Joined:
    Nov 6, 2014
    Posts:
    108
    Add the script above to your project, then add a SceneReference field to any of your other scripts.

    Code (CSharp):
    1. Using UnityEngine.SceneManagement;
    2. public class MySceneLoadingScript : MonoBehaviour{
    3.  
    4. public SceneReference scene;
    5.  
    6. Public void Start(){
    7. SceneManager.LoadScene(scene.SceneName);
    8. }
    9.  
    10. }
     
    Deeeds likes this.
  4. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    It will take me a few days to get this working, and a few more to understand what it's doing, and what I can do with it. I have not yet done anything with scenes. And suck at coding.
     
  5. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    AND>....

    THANK YOU!!!!

    If you ever need anything designed, sound or visuals, lemme know. I owe!
     
    DouglasPotesta likes this.
  6. BlackDragonBE

    BlackDragonBE

    Joined:
    Jul 23, 2012
    Posts:
    12
    Thanks for this! I just ran into this problem and was googling around for an easy solution. This works like a charm!

    Cheers!
     
    DouglasPotesta likes this.