Skip to content

Commit cdf7d0b

Browse files
committed
Upgraded utilities package
1 parent 3dee2e8 commit cdf7d0b

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

Assets/Plugins/AdncUtility/Scripts/AnimatorPlayback.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine;
22

33
namespace Adnc.Utility {
4+
[System.Obsolete("Please use Adnc.AnimatorHelpers.AnimatorPlayback instead")]
45
[System.Serializable]
56
public class AnimatorPlayback {
67
// When playing an animation twice it will always reset

Assets/Plugins/AdncUtility/Scripts/Singleton.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Adnc.Utility.Singletons {
6+
// @SRC http://wiki.unity3d.com/index.php/Singleton
7+
public abstract class SingletonBase<T> : MonoBehaviour where T : MonoBehaviour {
8+
private static T _instance;
9+
10+
private static string SingletonName {
11+
get { return typeof(T).Name; }
12+
}
13+
14+
public static T Instance {
15+
get {
16+
if (_instance != null) {
17+
return _instance;
18+
}
19+
20+
var instances = FindObjectsOfType(typeof(T));
21+
if (instances.Length >= 1) {
22+
if (Application.isPlaying) {
23+
Debug.AssertFormat(
24+
instances.Length == 1,
25+
"{1} {0} singletons detected. There should only ever be one present",
26+
SingletonName,
27+
instances.Length);
28+
}
29+
30+
_instance = (T)instances[0];
31+
return _instance;
32+
}
33+
34+
var singleton = new GameObject(SingletonName);
35+
_instance = singleton.AddComponent<T>();
36+
37+
// Only add DontDestroyOnLoad if the user fails to manually implement the component in a scene
38+
// Thereby giving the user more control over the singleton lifecycle
39+
if (Application.isPlaying) {
40+
DontDestroyOnLoad(singleton);
41+
}
42+
43+
return _instance;
44+
}
45+
}
46+
47+
protected virtual void OnDestroy () {
48+
if (_instance == this) {
49+
_instance = null;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Test friendly method to prevent persisting singletons with editor tests
55+
/// </summary>
56+
public static void ClearSingleton () {
57+
if (Application.isPlaying || _instance == null) {
58+
return;
59+
}
60+
61+
DestroyImmediate(_instance.gameObject);
62+
_instance = null;
63+
}
64+
}
65+
}

Assets/Plugins/AdncUtility/Scripts/Singleton/SingletonBase.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Adnc.Utility.Singletons {
2+
/// <summary>
3+
/// Testing only class
4+
/// </summary>
5+
public class SingletonStub : SingletonBase<SingletonStub> {
6+
[InfoBox("For testing purposes only. Do not implement", InfoBoxType.Error)]
7+
public string globalString = "test";
8+
}
9+
}

Assets/Plugins/AdncUtility/Scripts/Singleton/SingletonStub.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)