Skip to content

Commit 7beeef9

Browse files
committed
Added singleton pattern
1 parent d750114 commit 7beeef9

11 files changed

+721
-3
lines changed

Assets/Patterns/5. Singleton.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/5. Singleton/Scripts.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace SingletonPattern
6+
{
7+
public class GameController : MonoBehaviour
8+
{
9+
10+
void Start()
11+
{
12+
TestCSharpSingleton();
13+
14+
//TestUnitySingleton();
15+
}
16+
17+
18+
void Update()
19+
{
20+
21+
}
22+
23+
24+
25+
private void TestCSharpSingleton()
26+
{
27+
//Is not working because this is a singleton with a private constructor
28+
//SingletonCSharp singletonCSharp = new SingletonCSharp();
29+
30+
//This is working
31+
SingletonCSharp instance = SingletonCSharp.Instance;
32+
33+
instance.TestSingleton();
34+
35+
SingletonCSharp instance2 = SingletonCSharp.Instance;
36+
37+
instance2.TestSingleton();
38+
}
39+
40+
41+
42+
private void TestUnitySingleton()
43+
{
44+
SingletonUnity instance = SingletonUnity.Instance;
45+
46+
instance.TestSingleton();
47+
48+
SingletonUnity instance2 = SingletonUnity.Instance;
49+
50+
instance2.TestSingleton();
51+
}
52+
}
53+
}

Assets/Patterns/5. Singleton/Scripts/GameController.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
//
6+
// Basic Singleton pattern implementation in C#
7+
//
8+
9+
namespace SingletonPattern
10+
{
11+
//This is the most simplest Singleton pattern. The problem is that it's not thread safe. If you want a thread safe Singleton, then visit the link: https://csharpindepth.com/articles/singleton
12+
public class SingletonCSharp
13+
{
14+
//A static variable which holds a reference to the single created instance
15+
private static SingletonCSharp instance = null;
16+
17+
18+
19+
//For testing that we only call the constructor once
20+
private float randomNumber;
21+
22+
23+
24+
//A public static means of getting the reference to the single created instance, creating one if necessary
25+
public static SingletonCSharp Instance
26+
{
27+
get
28+
{
29+
if (instance == null)
30+
{
31+
instance = new SingletonCSharp();
32+
}
33+
34+
return instance;
35+
}
36+
}
37+
38+
39+
40+
//A single constructor, which is private and parameterless (singletons are not allowed to have parameters)
41+
//This prevents other classes from instantiating it and it also prevents subclassing (which both are violating the pattern)
42+
//But some argue that you should be able to inherit from singletons...
43+
private SingletonCSharp()
44+
{
45+
randomNumber = Random.Range(0f, 1f);
46+
}
47+
48+
49+
50+
//For testing
51+
public void TestSingleton()
52+
{
53+
Debug.Log($"Hello this is Singleton, my random number is: {randomNumber}");
54+
}
55+
}
56+
}

Assets/Patterns/5. Singleton/Scripts/SingletonCSharp.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
//
6+
// Basic Singleton pattern implementation in Unity
7+
//
8+
9+
namespace SingletonPattern
10+
{
11+
//A translation of the most basic Singleton pattern to Unity script which inherits from MonoBehaviour
12+
public class SingletonUnity : MonoBehaviour
13+
{
14+
//A static variable which holds a reference to the single created instance
15+
private static SingletonUnity instance = null;
16+
17+
18+
19+
//For testing that we only call the constructor once
20+
private float randomNumber;
21+
22+
23+
24+
//A public static means of getting the reference to the single created instance, creating one if necessary
25+
public static SingletonUnity Instance
26+
{
27+
get
28+
{
29+
if (instance == null)
30+
{
31+
//If a script in Unity inherits from MonoBehaviour, we can't use the new keyword to create a new Singleton as we did before
32+
//So you have to manually add this script to a gameobject in the scene
33+
//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!
34+
35+
//Find all singletons of this type in the scene
36+
SingletonUnity[] allSingletonsInScene = GameObject.FindObjectsOfType<SingletonUnity>();
37+
38+
if (allSingletonsInScene != null && allSingletonsInScene.Length > 0)
39+
{
40+
//Destroy all but one singleton
41+
if (allSingletonsInScene.Length > 1)
42+
{
43+
Debug.LogWarning($"You have more than one SingletonUnity in the scene!");
44+
45+
for (int i = 1; i < allSingletonsInScene.Length; i++)
46+
{
47+
Destroy(allSingletonsInScene[i].gameObject);
48+
}
49+
}
50+
51+
//Now we should have just one singleton in the scene, so pick it
52+
instance = allSingletonsInScene[0];
53+
54+
//Init the singleton
55+
instance.FakeConstructor();
56+
}
57+
//We have no singletons in the scene
58+
else
59+
{
60+
Debug.LogError($"You need to add the script SingletonUnity to a gameobject in the scene!");
61+
}
62+
}
63+
64+
return instance;
65+
}
66+
}
67+
68+
69+
70+
//Because this script inherits from MonoBehaviour, we cant use a constructor, so we have to invent our own
71+
private void FakeConstructor()
72+
{
73+
randomNumber = Random.Range(0f, 1f);
74+
}
75+
76+
77+
78+
//For testing
79+
public void TestSingleton()
80+
{
81+
Debug.Log($"Hello this is Singleton, my random number is: {randomNumber}");
82+
}
83+
}
84+
}

Assets/Patterns/5. Singleton/Scripts/SingletonUnity.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)