Advertisement
Guest User

Unity additive scene loading example

a guest
Dec 16th, 2020
5,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8.  
  9. // @kurtdekker
  10. // designed to be in your content scene where you're editing the level.
  11. // this then loads the player, UI, pause button, music, etc.
  12.  
  13. public class SceneLoader : MonoBehaviour
  14. {
  15.     private void Reset()
  16.     {
  17.         name = "AdditiveSceneLoader";
  18.     }
  19.  
  20.     const string s_LoadingPrefab = "Prefabs/Loading/Canvas_Loading_Prefab";
  21.  
  22.     public List<string> additiveScenes;
  23.     void InitGame()
  24.     {
  25.         // global variables
  26.         DSM.State.Score.iValue = 0;
  27.     }
  28.  
  29.     IEnumerator Start()
  30.     {
  31.         var loading = Instantiate<GameObject>(Resources.Load<GameObject>(s_LoadingPrefab));
  32.  
  33.         InitGame();
  34.         foreach (var additiveScene in additiveScenes)
  35.         {
  36.             if (string.IsNullOrEmpty(additiveScene))
  37.             {
  38.                 continue;
  39.             }
  40.             SceneHelper.GotoScene(additiveScene, true);
  41.         }
  42.  
  43.         // lets all the scenes load
  44.         yield return null;
  45.  
  46.         Destroy(loading);
  47.     }
  48.  
  49.  
  50. #if UNITY_EDITOR
  51.     [CustomEditor(typeof(SceneLoader)), CanEditMultipleObjects]
  52.     public class SceneLoaderEditor : Editor
  53.     {
  54.         public override void OnInspectorGUI()
  55.         {
  56.             SceneLoader sl = (SceneLoader)target;
  57.  
  58.             DrawDefaultInspector();
  59.  
  60.             EditorGUILayout.BeginVertical();
  61.  
  62.             if (GUILayout.Button("EMIT STRINGS TO DEBUG"))
  63.             {
  64.                 var s = "Scenes list:\n\n";
  65.                 for (int i = 0; i < sl.additiveScenes.Count; i++)
  66.                 {
  67.                     s = s + sl.additiveScenes[i] + "\n";
  68.  
  69.                 }
  70.                 Debug.Log(s);
  71.             }
  72.  
  73.             EditorGUILayout.EndVertical();
  74.         }
  75.     }
  76. #endif
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement