Guest User

Untitled

a guest
Dec 11th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3.  
  4. /*
  5. Use this class if you need to create a singleton.
  6.  
  7. Usage:
  8.  
  9. [UnitySingleton(mustBeOnlyComponentOnObject: true, mustBeWithinRootObject: true)]
  10. class MySingletonClass : UnitySingleton<MySingletonClass> {
  11. protected overide void OnAwake() {
  12. // mimics MonoBehaviour Awake
  13. }
  14. }
  15.  
  16. If "mustBeOnlyComponentOnObject" is true, then an error will be raised
  17. if there are any other components on the GameObject other than a Transform and MySingletonClass
  18.  
  19. If "mustBeWithinRootObject" is true, then an error will be raised
  20. if the GameObject MySingletonClass has a parent(which is equal to not being in the root object)
  21.  
  22. An error will also be raised if there are two or more of MySingletonClass in the scene.
  23. Also, every instance of MySingletonClass but one is Destroyed upon Awake to ensure that only one MySingletonClass can remain active.
  24.  
  25. Why not use [DisallowMultipleComponent]?
  26. Its up to the user to add this to its own Singleton.
  27. It will only prevent the editor from adding multiple.
  28. However, if a component of the same time is added to the GameObject during runtime, mustBeOnlyInstanceInScene is needed.
  29. */
  30. public abstract class UnitySingleton<T> : MonoBehaviour where T : MonoBehaviour {
  31. protected static T instance;
  32.  
  33. protected void Awake() {
  34. HandleAttribute();
  35.  
  36. if (instance == null) {
  37. instance = this as T;
  38. OnAwake();
  39. }
  40. }
  41.  
  42. protected virtual void OnAwake() { }
  43.  
  44. private void HandleAttribute() {
  45. UnitySingletonAttribute attribute = Attribute.GetCustomAttribute(typeof(T), typeof(UnitySingletonAttribute)) as UnitySingletonAttribute;
  46. if (attribute == null) {
  47. Debug.LogError("Failed to find " + typeof(UnitySingletonAttribute).Name + " on " + typeof(T));
  48. }
  49. else {
  50. if (attribute.mustBeOnlyComponentOnObject) {
  51. Component[] allComponents = GetComponents<Component>();
  52. if (allComponents.Length > 2) {
  53. Debug.LogError(typeof(T) + " must be the only component on this object");
  54. }
  55. }
  56.  
  57. if (attribute.mustBeWithinRootObject) {
  58. if (transform.parent != null) {
  59. Debug.LogError(typeof(T) + " is required to be within a root object");
  60. }
  61. }
  62.  
  63. if (attribute.mustBeOnlyInstanceInScene) {
  64. var instances = FindObjectsOfType<T>();
  65. if (instances.Length > 1) {
  66. Debug.LogError(instances.Length + " instances of " + typeof(T) + " found in the scene. There can only be one " + typeof(T) + " in the scene.");
  67. }
  68. }
  69. }
  70. }
  71. }
  72.  
  73. [AttributeUsage(AttributeTargets.All, Inherited = true)]
  74. public class UnitySingletonAttribute : Attribute {
  75. public readonly bool mustBeOnlyComponentOnObject;
  76. public readonly bool mustBeWithinRootObject;
  77. public readonly bool mustBeOnlyInstanceInScene;
  78.  
  79. public UnitySingletonAttribute(bool mustBeOnlyComponentOnObject = false, bool mustBeWithinRootObject = false, bool mustBeOnlyInstanceInScene = true) {
  80. this.mustBeOnlyComponentOnObject = mustBeOnlyComponentOnObject;
  81. this.mustBeWithinRootObject = mustBeWithinRootObject;
  82. this.mustBeOnlyInstanceInScene = mustBeOnlyInstanceInScene;
  83. }
  84. }
Add Comment
Please, Sign In to add comment