Last active
November 30, 2020 17:11
-
-
Save nekomimi-daimao/bd22bb881d3fa4a25df803aa3bcc8b6e to your computer and use it in GitHub Desktop.
DontDestroyOnLoadCollector
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
using System.Linq; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
namespace Nekomimi.Daimao | |
{ | |
public static class DontDestroyOnLoadCollector | |
{ | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
private static void Register() | |
{ | |
SceneManager.sceneLoaded += (scene, mode) => | |
{ | |
var ddol = CollectDontDestroyOnLoad(); | |
var separator = ", "; | |
var s = string.Join(separator, ddol.Where(go => go != null).Select(go => go.name)); | |
Debug.Log($"DDOL_{scene.name} : {s}"); | |
}; | |
} | |
public static GameObject[] CollectDontDestroyOnLoad() | |
{ | |
var go = new GameObject(string.Empty); | |
Object.DontDestroyOnLoad(go); | |
var ddol = go.scene.GetRootGameObjects(); | |
Object.Destroy(go); | |
return ddol.Where(o => o != null && !string.Equals(o.name, string.Empty)).ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment