Demigiant

Get and Sort DontDestroy gameObjects

Oct 25th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2020/10/25
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using Object = UnityEngine.Object;
  9.  
  10. namespace Demigiant.DemiTools.DeUnityExtended
  11. {
  12.     public static class DeSceneUtils
  13.     {
  14.         #region Public Methods
  15.  
  16.         /// <summary>
  17.         /// Returns all root DontDestroyOnLoad GameObjects, or an empty list if none was found.<para/>
  18.         /// WARNING, expensive method with allocations (can be partially avoided by passing an existing DontDestroy GameObject):<para/>
  19.         /// - new list creation<para/>
  20.         /// - new GameObject creation-then-destruction (necessary to detect the DontDestroy scene, avoidable if anyDontDestroyGameObject is provided)
  21.         /// </summary>
  22.         /// <param name="anyDontDestroyGameObject">Providing this will avoid having to create and destroy a temporary gameObject during this process</param>
  23.         public static List<GameObject> GetDontDestroyRootGameObjects(GameObject anyDontDestroyGameObject = null)
  24.         {
  25.             List<GameObject> result = new List<GameObject>();
  26.             Scene dontDestroyScene;
  27.             if (anyDontDestroyGameObject != null) {
  28.                 dontDestroyScene = anyDontDestroyGameObject.scene;
  29.                 dontDestroyScene.GetRootGameObjects(result);
  30.             } else {
  31.                 const string tmpGuid = "bb179a0d-94b2-4008-ad78-f3871dc82ba6";
  32.                 GameObject tmpGO = new GameObject(tmpGuid);
  33.                 Object.DontDestroyOnLoad(tmpGO);
  34.                 dontDestroyScene = tmpGO.scene;
  35.                 Object.Destroy(tmpGO);
  36.                 dontDestroyScene.GetRootGameObjects(result);
  37.                 int tot = result.Count;
  38.                 for (int i = 0; i < tot; ++i) {
  39.                     if (result[i].name != tmpGuid) continue;
  40.                     result.RemoveAt(i);
  41.                     break;
  42.                 }
  43.             }
  44.             return result;
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Sorts all GameObjects in the DontDestroy scene.<para/>
  49.         /// WARNING, expensive method with allocations (can be partially avoided by passing an existing DontDestroy GameObject):<para/>
  50.         /// - new list creation<para/>
  51.         /// - new GameObject creation-then-destruction (necessary to detect the DontDestroy scene, avoidable if anyDontDestroyGameObject is provided)
  52.         /// </summary>
  53.         /// <param name="anyDontDestroyGameObject">Providing this will avoid having to create and destroy a temporary gameObject during this process</param>
  54.         public static void SortDontDestroyGameObjectsAlphabetically(GameObject anyDontDestroyGameObject = null)
  55.         {
  56.             List<GameObject> allRootDontDestroyGos = GetDontDestroyRootGameObjects(anyDontDestroyGameObject);
  57.             allRootDontDestroyGos.Sort(SortGameObjectsByName);
  58.             int tot = allRootDontDestroyGos.Count;
  59.             for (int i = 0; i < tot; ++i) allRootDontDestroyGos[i].transform.SetSiblingIndex(i);
  60.         }
  61.  
  62.         #endregion
  63.  
  64.         #region Methods
  65.  
  66.         static int SortGameObjectsByName(GameObject a, GameObject b)
  67.         {
  68.             return string.Compare(a.name, b.name, StringComparison.InvariantCultureIgnoreCase);
  69.         }
  70.  
  71.         #endregion
  72.     }
  73. }
Add Comment
Please, Sign In to add comment