|
1 |
| -using System.Collections; |
2 |
| -using System.Collections.Generic; |
3 | 1 | using UnityEngine;
|
4 | 2 |
|
5 | 3 | //
|
@@ -28,43 +26,46 @@ public static SingletonUnity Instance
|
28 | 26 | {
|
29 | 27 | if (instance == null)
|
30 | 28 | {
|
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! |
| 29 | + // Find singleton of this type in the scene |
| 30 | + var instance = GameObject.FindObjectOfType<SingletonUnity>(); |
34 | 31 |
|
35 |
| - //Find all singletons of this type in the scene |
36 |
| - SingletonUnity[] allSingletonsInScene = GameObject.FindObjectsOfType<SingletonUnity>(); |
37 |
| - |
38 |
| - if (allSingletonsInScene != null && allSingletonsInScene.Length > 0) |
| 32 | + // If there is no singleton object in the scene, we have to add one |
| 33 | + if (instance == null) |
39 | 34 | {
|
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]; |
| 35 | + GameObject obj = new GameObject("Unity Singleton"); |
| 36 | + instance = obj.AddComponent<SingletonUnity>(); |
53 | 37 |
|
54 | 38 | //Init the singleton
|
55 | 39 | 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!"); |
| 40 | + |
| 41 | + // The singleton object shouldn't be destroyed when we switch between scenes |
| 42 | + DontDestroyOnLoad(obj); |
61 | 43 | }
|
62 | 44 | }
|
63 | 45 |
|
64 | 46 | return instance;
|
65 | 47 | }
|
66 | 48 | }
|
67 | 49 |
|
| 50 | + void Awake() |
| 51 | + { |
| 52 | + if (instance == null) |
| 53 | + { |
| 54 | + instance = this; |
| 55 | + |
| 56 | + // Init the singleton |
| 57 | + instance.FakeConstructor(); |
| 58 | + |
| 59 | + // The singleton object shouldn't be destroyed when we switch between scenes |
| 60 | + DontDestroyOnLoad(this.gameObject); |
| 61 | + } |
| 62 | + // because we inherit from MonoBehaviour whem might have accidentally added several of them to the scene, |
| 63 | + // which will cause trouble, so we have to make sure we have just one! |
| 64 | + else |
| 65 | + { |
| 66 | + Destroy(gameObject); |
| 67 | + } |
| 68 | + } |
68 | 69 |
|
69 | 70 |
|
70 | 71 | //Because this script inherits from MonoBehaviour, we cant use a constructor, so we have to invent our own
|
|
0 commit comments