HolyFot

Map/Scene Truly Loaded Detector C# Unity

May 22nd, 2020
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Attach this script to an object in your 1st scene
  2. //This script was created since SceneManager.sceneLoaded delegate triggers way too early when loading scenes/maps
  3. using UnityEngine;
  4. using System;
  5. using System.Collections;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.Events;
  8.  
  9. public class MapLoadDetector : MonoBehaviour
  10. {
  11.     //Settings
  12.     public string map1Scene = "CR_Map1";
  13.     public string map2Scene = "CR_Map2";
  14.     public int fpsTrigger = 10;
  15.    
  16.     public event EventMapLoad onMap1LoadedCall;
  17.     public event EventMapLoad onMap2LoadedCall;
  18.     public delegate void EventMapLoad();
  19.     //Info
  20.     public bool isMapLoaded1;
  21.     public bool isMapLoaded2;
  22.     public bool isEnabled = false;
  23.  
  24.     //Internal
  25.     private float fpsTimer = 1.0f;
  26.     private float currTimer;
  27.     private int fpsCount = 0;
  28.     private int currentFPS = 0;
  29.     private static MapLoadDetector _Instance;
  30.     private bool alreadyTriggered1 = false;
  31.     private bool alreadyTriggered2 = false;
  32.  
  33.     public static MapLoadDetector Instance
  34.     {
  35.         get
  36.         {
  37.             if (_Instance == null)
  38.                 _Instance = GameObject.FindObjectOfType<MapLoadDetector>();
  39.             return _Instance;
  40.         }
  41.     }
  42.  
  43.     void Awake()
  44.     {
  45.         DontDestroyOnLoad(this.gameObject);
  46.         currTimer = 1f;
  47.         isEnabled = true;
  48.     }
  49.  
  50.     void OnEnable()
  51.     {
  52.         isEnabled = true;
  53.         alreadyTriggered1 = false;
  54.         alreadyTriggered2 = false;
  55.         StartCoroutine("FPSCalc");
  56.         currTimer = 1f;
  57.     }
  58.  
  59.     void OnDisable()
  60.     {
  61.         isEnabled = false;
  62.         alreadyTriggered1 = false;
  63.         alreadyTriggered2 = false;
  64.         StopCoroutine("FPSCalc");
  65.     }
  66.  
  67.     void Update()
  68.     {
  69.         currTimer -= Time.deltaTime;
  70.         if (currTimer <= 0.0f) //1 Second
  71.         {
  72.             //Calculate Accurate FPS & Reset
  73.             currentFPS = fpsCount;
  74.             fpsCount = 0;
  75.  
  76.             if (SceneManager.GetActiveScene().name == map1Scene)
  77.             {
  78.                 if (currentFPS > fpsTrigger && !alreadyTriggered1) //Over 10fps & Map is loaded
  79.                 {
  80.                     if (onMap1LoadedCall != null)
  81.                     {
  82.                         onMap1LoadedCall.Invoke();
  83.                     }
  84.                     isMapLoaded1 = true;
  85.                     alreadyTriggered1 = true;
  86.                 }
  87.                 else
  88.                     isMapLoaded1 = false;
  89.             }
  90.             else
  91.                 isMapLoaded1 = false;
  92.  
  93.             if (SceneManager.GetActiveScene().name == map2Scene)
  94.             {
  95.                 if (currentFPS > fpsTrigger && !alreadyTriggered2) //Over 10fps & Map is loaded
  96.                 {
  97.                     if (onMap2LoadedCall != null)
  98.                     {
  99.                         onMap2LoadedCall.Invoke();
  100.                     }
  101.                     isMapLoaded2 = true;
  102.                     alreadyTriggered2 = true;
  103.                 }
  104.                 else
  105.                     isMapLoaded2 = false;
  106.             }
  107.             else
  108.                 isMapLoaded2 = false;
  109.  
  110.             currTimer = fpsTimer; //Reset Timer
  111.         }
  112.     }
  113.  
  114.     IEnumerator FPSCalc()
  115.     {
  116.         while (isEnabled)
  117.         {
  118.             yield return 0; //Count Each Frame
  119.             fpsCount++;
  120.         }
  121.     }
  122. }
  123.  
  124. //EXAMPLE USAGE:
  125. void OnEnable()
  126. {
  127.     if (MapLoadDetector.Instance != null)
  128.         MapLoadDetector.Instance.onMap1LoadedCall += OnMapLoaded;
  129. }
  130.  
  131. void OnDisable()
  132. {
  133.     if (MapLoadDetector.Instance != null)
  134.         MapLoadDetector.Instance.onMap1LoadedCall -= OnMapLoaded;
  135. }
  136.  
  137. void OnMapLoaded()
  138. {
  139.     Debug.Log("Map 1 completely loaded.");
  140. }
RAW Paste Data