Skip to content

Commit

Permalink
Adding support to look for gameobject with the don't save flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sliptrixx committed Aug 2, 2023
1 parent 8dd90a5 commit cb9ec76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions Scripts/Core/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Hibzz.Singletons
{
Expand Down Expand Up @@ -80,20 +79,33 @@ protected static T[] GetAllObjects()
{
#if UNITY_EDITOR

// when the editor is in play mode, FindObjectsOfType will work
// when the editor is in play mode, FindObjectsOfType will work...
// however, if the nothing was found, we can do an extensive search
// with the objects that might be marked with "Don't Save" flags
if (Application.isPlaying)
{
return FindObjectsOfType<T>();
var result = FindObjectsOfType<T>();
if(result.Length > 0)
{
return result;
}
}

// the editor is not in playmode, so in the editor context
// this container will store all objects found of type T in the scene
List<T> singletons = new List<T>();

GameObject[] rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();
foreach (var rootObject in rootObjects)
// resources api needs to be used, else it won't detect objects of
// that have a don't save flag
GameObject[] gameobjects = Resources.FindObjectsOfTypeAll<GameObject>();
foreach (var go in gameobjects)
{
var comps = rootObject.GetComponentsInChildren<T>();
// a persistent gameobject is something stored in the disk, so
// usually a prefab... ignore those type of objects
if(UnityEditor.EditorUtility.IsPersistent(go)) { continue; }

// get all singleton objects of type T and store it in the list of singletons
var comps = go.GetComponentsInChildren<T>();
foreach (var comp in comps)
{
singletons.Add(comp);
Expand All @@ -105,6 +117,9 @@ protected static T[] GetAllObjects()

#else

// only simple gameobject find in scene... To be honest, I don't
// see a point in instantiating objects with the Don't save flags
// in the build version of a game, so this will do fine
return FindObjectsOfType<T>();

#endif
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.hibzz.singletons",
"version": "1.3.0",
"version": "1.3.1",
"displayName": "hibzz.singletons",
"description": "A library of singletons for Unity",
"author": {
Expand Down

0 comments on commit cb9ec76

Please sign in to comment.