Skip to content

Improve the implementation of SingletonUnity.cs #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions Assets/Patterns/5. Singleton/Scripts/SingletonUnity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//
Expand Down Expand Up @@ -28,43 +26,46 @@ public static SingletonUnity Instance
{
if (instance == null)
{
//If a script in Unity inherits from MonoBehaviour, we can't use the new keyword to create a new Singleton as we did before
//So you have to manually add this script to a gameobject in the scene
//But because we inherit from MonoBehaviour whem might have accidentally added several of them to the scene, which will cause trouble, so we have to make sure we have just one!
// Find singleton of this type in the scene
var instance = GameObject.FindObjectOfType<SingletonUnity>();

//Find all singletons of this type in the scene
SingletonUnity[] allSingletonsInScene = GameObject.FindObjectsOfType<SingletonUnity>();

if (allSingletonsInScene != null && allSingletonsInScene.Length > 0)
// If there is no singleton object in the scene, we have to add one
if (instance == null)
{
//Destroy all but one singleton
if (allSingletonsInScene.Length > 1)
{
Debug.LogWarning($"You have more than one SingletonUnity in the scene!");

for (int i = 1; i < allSingletonsInScene.Length; i++)
{
Destroy(allSingletonsInScene[i].gameObject);
}
}

//Now we should have just one singleton in the scene, so pick it
instance = allSingletonsInScene[0];
GameObject obj = new GameObject("Unity Singleton");
instance = obj.AddComponent<SingletonUnity>();

//Init the singleton
instance.FakeConstructor();
}
//We have no singletons in the scene
else
{
Debug.LogError($"You need to add the script SingletonUnity to a gameobject in the scene!");

// The singleton object shouldn't be destroyed when we switch between scenes
DontDestroyOnLoad(obj);
}
}

return instance;
}
}

void Awake()
{
if (instance == null)
{
instance = this;

// Init the singleton
instance.FakeConstructor();

// The singleton object shouldn't be destroyed when we switch between scenes
DontDestroyOnLoad(this.gameObject);
}
// because we inherit from MonoBehaviour whem might have accidentally added several of them to the scene,
// which will cause trouble, so we have to make sure we have just one!
else
{
Destroy(gameObject);
}
}


//Because this script inherits from MonoBehaviour, we cant use a constructor, so we have to invent our own
Expand Down