Created
July 15, 2020 06:33
-
-
Save HolyFot/a8707231af6343de988f6bb96a01ad68 to your computer and use it in GitHub Desktop.
Map/Scene Truly Loaded Detector C# Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//EXAMPLE USAGE: | |
void OnEnable() | |
{ | |
if (MapLoadDetector.Instance != null) | |
MapLoadDetector.Instance.onMap1LoadedCall += OnMapLoaded; | |
} | |
void OnDisable() | |
{ | |
if (MapLoadDetector.Instance != null) | |
MapLoadDetector.Instance.onMap1LoadedCall -= OnMapLoaded; | |
} | |
void OnMapLoaded() | |
{ | |
Debug.Log("Map 1 completely loaded."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Made by: HolyFot | |
//License: CC0 - https://creativecommons.org/share-your-work/public-domain/cc0/ | |
//Note: Attach this script to an object in your first scene | |
//Note: This script was created since SceneManager.sceneLoaded delegate triggers way too early when loading scenes/maps | |
using UnityEngine; | |
using System; | |
using System.Collections; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.Events; | |
public class MapLoadDetector : MonoBehaviour | |
{ | |
//Settings | |
public string map1Scene = "CR_Map1"; | |
public string map2Scene = "CR_Map2"; | |
public delegate void EventMapLoad(); | |
public event EventMapLoad onMap1LoadedCall; | |
//Info | |
public bool isMapLoaded1; | |
public bool isEnabled = false; | |
//Internal | |
private float fpsTimer = 1.0f; | |
private float currTimer; | |
private int fpsCount = 0; | |
private int currentFPS = 0; | |
private static MapLoadDetector _Instance; | |
private bool alreadyTriggered1 = false; | |
public static MapLoadDetector Instance | |
{ | |
get | |
{ | |
if (_Instance == null) | |
_Instance = GameObject.FindObjectOfType<MapLoadDetector>(); | |
return _Instance; | |
} | |
} | |
void Awake() | |
{ | |
DontDestroyOnLoad(this.gameObject); | |
currTimer = 1f; | |
isEnabled = true; | |
} | |
void OnEnable() | |
{ | |
isEnabled = true; | |
alreadyTriggered1 = false; | |
StartCoroutine("FPSCalc"); | |
currTimer = 1f; | |
} | |
void OnDisable() | |
{ | |
isEnabled = false; | |
alreadyTriggered1 = false; | |
StopCoroutine("FPSCalc"); | |
} | |
void Update() | |
{ | |
currTimer -= Time.deltaTime; | |
if (currTimer <= 0.0f) //1 Second | |
{ | |
//Calculate Accurate FPS & Reset | |
currentFPS = fpsCount; | |
fpsCount = 0; | |
if (SceneManager.GetActiveScene().name == map1Scene || SceneManager.GetActiveScene().name == map2Scene) | |
{ | |
if (currentFPS > 10 && !alreadyTriggered1) //Over 10fps & Map is loaded | |
{ | |
if (onMap1LoadedCall != null) | |
{ | |
onMap1LoadedCall.Invoke(); | |
} | |
isMapLoaded1 = true; | |
alreadyTriggered1 = true; | |
} | |
else | |
isMapLoaded1 = false; | |
} | |
else | |
isMapLoaded1 = false; | |
currTimer = fpsTimer; //Reset Timer | |
} | |
} | |
IEnumerator FPSCalc() | |
{ | |
while (isEnabled) | |
{ | |
yield return 0; //Count Each Frame | |
fpsCount++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment