├── .gitignore ├── LICENSE ├── PrefabAttribute.cs ├── Singleton.cs └── readme.md /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kleber-swf/Unity-Singleton-MonoBehaviour/d773495c80eecfcf60b1dc7be82ab813e3e2af09/.gitignore -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Kleber Lopes da Silva 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /PrefabAttribute.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Unity Singleton MonoBehaviour 3 | * This file is licensed under the terms of the MIT license. 4 | * 5 | * Copyright (c) 2014 Kleber Lopes da Silva (http://kleber-swf.com) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | using System; 26 | 27 | namespace com.kleberswf.lib.core { 28 | [AttributeUsage(AttributeTargets.Class)] 29 | public class PrefabAttribute : Attribute { 30 | public readonly string Name; 31 | public PrefabAttribute(string name) { Name = name; } 32 | public PrefabAttribute() { Name = null; } 33 | } 34 | } -------------------------------------------------------------------------------- /Singleton.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Unity Singleton MonoBehaviour 3 | * This file is licensed under the terms of the MIT license. 4 | * 5 | * Copyright (c) 2014 Kleber Lopes da Silva (http://kleber-swf.com) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | using System; 26 | using UnityEngine; 27 | 28 | namespace com.kleberswf.lib.core { 29 | public abstract class Singleton : MonoBehaviour where T : MonoBehaviour { 30 | private static T _instance; 31 | 32 | public static T Instance { 33 | get { 34 | if (!Instantiated) CreateInstance(); 35 | return _instance; 36 | } 37 | } 38 | 39 | private static void CreateInstance() { 40 | if (Destroyed) return; 41 | var type = typeof (T); 42 | var objects = FindObjectsOfType(); 43 | if (objects.Length > 0) { 44 | if (objects.Length > 1) { 45 | Debug.LogWarning("There is more than one instance of Singleton of type \"" + type + 46 | "\". Keeping the first one. Destroying the others."); 47 | for (var i = 1; i < objects.Length; i++) Destroy(objects[i].gameObject); 48 | } 49 | _instance = objects[0]; 50 | _instance.gameObject.SetActive(true); 51 | Instantiated = true; 52 | Destroyed = false; 53 | return; 54 | } 55 | 56 | string prefabName; 57 | GameObject gameObject; 58 | var attribute = Attribute.GetCustomAttribute(type, typeof (PrefabAttribute)) as PrefabAttribute; 59 | 60 | if (attribute == null || string.IsNullOrEmpty(attribute.Name)) { 61 | prefabName = type.ToString(); 62 | gameObject = new GameObject(); 63 | } else { 64 | prefabName = attribute.Name; 65 | gameObject = Instantiate(Resources.Load(prefabName)); 66 | if (gameObject == null) 67 | throw new Exception("Could not find Prefab \"" + prefabName + "\" on Resources for Singleton of type \"" + type + 68 | "\"."); 69 | } 70 | 71 | gameObject.name = prefabName; 72 | if (_instance == null) 73 | _instance = gameObject.GetComponent() ?? gameObject.AddComponent(); 74 | Instantiated = true; 75 | Destroyed = false; 76 | } 77 | 78 | public bool Persistent; 79 | public static bool Instantiated { get; private set; } 80 | public static bool Destroyed { get; private set; } 81 | 82 | protected virtual void Awake() { 83 | if (_instance == null) { 84 | if (Persistent) { 85 | CreateInstance(); 86 | DontDestroyOnLoad(gameObject); 87 | } 88 | return; 89 | } 90 | if (Persistent) DontDestroyOnLoad(gameObject); 91 | if (GetInstanceID() != _instance.GetInstanceID()) Destroy(gameObject); 92 | } 93 | 94 | private void OnDestroy() { 95 | Destroyed = true; 96 | Instantiated = false; 97 | } 98 | 99 | public void Touch() { } 100 | } 101 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Singleton MonoBehaviour for Unity Projects 2 | Implementation of the Singleton Design Pattern to Unity MonoBehaviours. 3 | This is useful when you need a single instance of a class and wants to tweak its properties in the editor. 4 | 5 | #### Features: 6 | * creates the Singleton instance automatically 7 | * Singleton is ready no matter where or when you need it in your code 8 | * grants the Singleton uniqueness (just one active instance at time) 9 | * if there is already an instance in the Scene, uses it 10 | * optinally makes the instance persistent through scenes 11 | 12 | This implementation is a work in progress and it's widely open to suggestions. 13 | 14 | More info at: http://kleber-swf.com/singleton-monobehaviour-unity-projects 15 | --------------------------------------------------------------------------------