खोज…


टिप्पणियों

जबकि विचार के स्कूल हैं जो सम्मोहक तर्क देते हैं कि सिंग्लेटों का अप्रतिबंधित उपयोग एक बुरा विचार क्यों है, जैसे कि gameprogrammingpatterns.com पर सिंगलटन , ऐसे मौके हैं जब आप कई दृश्यों में एकता में गेमऑबजेक्ट को बनाए रखना चाहते हैं (जैसे कि सहज पृष्ठभूमि संगीत के लिए) यह सुनिश्चित करते हुए कि एक से अधिक उदाहरण मौजूद नहीं हो सकते; एक सिंगलटन के लिए एक सही उपयोग मामला।

इस स्क्रिप्ट को एक गेम ऑबजेक्ट में जोड़कर, एक बार इसे तत्काल (जैसे किसी दृश्य में कहीं भी शामिल करके) यह दृश्य भर में सक्रिय रहेगा, और केवल एक उदाहरण कभी भी मौजूद रहेगा।


स्क्रिप्ट योग्य ( UnityDoc ) उदाहरणों कुछ उपयोग के मामलों के लिए Singletons के लिए एक वैध विकल्प प्रदान करते हैं। हालांकि वे एकल उदाहरण नियम को लागू नहीं करते हैं, वे दृश्यों के बीच अपनी स्थिति को बनाए रखते हैं और एकता क्रमांकन प्रक्रिया के साथ अच्छी तरह से खेलते हैं। वे नियंत्रण के व्युत्क्रम को भी बढ़ावा देते हैं क्योंकि संपादक के माध्यम से निर्भरता को इंजेक्ट किया जाता है

// MyAudioManager.cs
using UnityEngine;

[CreateAssetMenu] // Remember to create the instance in editor
public class MyAudioManager : ScriptableObject {
    public void PlaySound() {}
}
// MyGameObject.cs
using UnityEngine;

public class MyGameObject : MonoBehaviour
{
    [SerializeField]
    MyAudioManager audioManager; //Insert through Inspector

    void OnEnable()
    {
        audioManager.PlaySound();
    }
}

आगे की पढाई

RuntimeInitializeOnLoadMethodAttribute का उपयोग करके कार्यान्वयन

यूनिटी ५.२.५ के बाद से मुनोबेहैर के निष्पादन के आदेश को दरकिनार करते हुए आरंभीकरण तर्क को निष्पादित करने के लिए RuntimeInitializeOnLoadMethodAttribute का उपयोग करना संभव है। यह अधिक स्वच्छ और मजबूत कार्यान्वयन बनाने का एक तरीका प्रदान करता है:

using UnityEngine;

sealed class GameDirector : MonoBehaviour
{
    // Because of using RuntimeInitializeOnLoadMethod attribute to find/create and
    // initialize the instance, this property is accessible and
    // usable even in Awake() methods.
    public static GameDirector Instance
    {
        get; private set;
    }

    // Thanks to the attribute, this method is executed before any other MonoBehaviour
    // logic in the game.
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    static void OnRuntimeMethodLoad()
    {
        var instance = FindObjectOfType<GameDirector>();

        if (instance == null)
            instance = new GameObject("Game Director").AddComponent<GameDirector>();

        DontDestroyOnLoad(instance);

        Instance = instance;
    }

    // This Awake() will be called immediately after AddComponent() execution
    // in the OnRuntimeMethodLoad(). In other words, before any other MonoBehaviour's
    // in the scene will begin to initialize.
    private void Awake()
    {
        // Initialize non-MonoBehaviour logic, etc.
        Debug.Log("GameDirector.Awake()", this);
    }
}

निष्पादन का परिणाम:

  1. GameDirector.OnRuntimeMethodLoad() प्रारंभ ...
  2. GameDirector.Awake()
  3. GameDirector.OnRuntimeMethodLoad() पूरा हुआ।
  4. OtherMonoBehaviour1.Awake()
  5. OtherMonoBehaviour2.Awake() , आदि।

एकता सी # में एक साधारण सिंगलटन मोनोबेहौर

इस उदाहरण में, कक्षा का एक निजी स्थिर उदाहरण इसकी शुरुआत में घोषित किया जाता है।

एक स्थिर क्षेत्र का मूल्य उदाहरणों के बीच साझा किया जाता है, इसलिए यदि इस वर्ग का एक नया उदाहरण बन जाता है if पहली सिंग्लटन ऑब्जेक्ट के लिए एक संदर्भ मिलेगा, जो नए उदाहरण (या इसके गेम ऑब्जेक्ट) को नष्ट कर देगा।

using UnityEngine;
        
public class SingletonExample : MonoBehaviour {

    private static SingletonExample _instance;
    
    void Awake(){

        if (_instance == null){

            _instance = this;
            DontDestroyOnLoad(this.gameObject);
    
            //Rest of your Awake code
    
        } else {
            Destroy(this);
        }
    }

    //Rest of your class code

}

उन्नत एकता सिंगलटन

यह उदाहरण एक में इंटरनेट पर पाए जाने वाले मोनोबेहियर सिंगलेट्स के कई वेरिएंट को जोड़ता है और आपको वैश्विक स्थैतिक क्षेत्रों के आधार पर इसके व्यवहार को बदलने देता है।

इस उदाहरण को यूनिटी 5 का उपयोग करके परीक्षण किया गया था। इस सिंगलटन का उपयोग करने के लिए, आपको बस इसे निम्न प्रकार से विस्तारित करना होगा: public class MySingleton : Singleton<MySingleton> {} । आपको सामान्य Awake बजाय इसका उपयोग करने के लिए AwakeSingleton को ओवरराइड करने की आवश्यकता हो सकती है। आगे ट्विकिंग के लिए, नीचे बताए अनुसार स्थिर फ़ील्ड के डिफ़ॉल्ट मान बदलें।


  1. यह कार्यान्वयन गेम ऑबजेक्ट के अनुसार एक उदाहरण रखने के लिए DisallowMultipleComponent विशेषता का उपयोग करता है।
  2. यह वर्ग अमूर्त है और इसे केवल बढ़ाया जा सकता है। इसमें एक वर्चुअल विधि AwakeSingleton भी शामिल है जिसे सामान्य Awake को लागू करने के बजाय ओवरराइड करने की आवश्यकता है।
  3. यह कार्यान्वयन थ्रेड सुरक्षित है।
  4. यह सिंगलटन अनुकूलित है। उदाहरण अशक्त जाँच के बजाय instantiated ध्वज का उपयोग करके हम उस ओवरहेड से बचते हैं जो यूनिटी के == ऑपरेटर के कार्यान्वयन के साथ आता है। (और पढ़ें )
  5. यह कार्यान्वयन एकल कॉल के लिए किसी भी कॉल की अनुमति नहीं देता है जब यह एकता द्वारा नष्ट होने वाला है।
  6. यह सिंगलटन निम्नलिखित विकल्पों के साथ आता है:
  • FindInactive : निष्क्रिय GameObject से जुड़े एक ही प्रकार के घटकों के अन्य उदाहरणों को देखना है या नहीं।
  • Persist : दृश्यों के बीच घटक को जीवित रखना है या नहीं।
  • DestroyOthers : चाहे एक ही प्रकार के किसी भी अन्य घटक को नष्ट करना हो और केवल एक को रखना हो।
  • Lazy : चाहे "फ्लाई पर" ( Awake ) या "ऑन डिमांड" (जब गेटर कहा जाता है) पर एकल उदाहरण स्थापित करना है।
using UnityEngine;

[DisallowMultipleComponent]
public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
    private static volatile T instance;
    // thread safety
    private static object _lock = new object();
    public static bool FindInactive = true;
    // Whether or not this object should persist when loading new scenes. Should be set in Init().
    public static bool Persist;
    // Whether or not destory other singleton instances if any. Should be set in Init().
    public static bool DestroyOthers = true;
    // instead of heavy comparision (instance != null)
    // http://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
    private static bool instantiated;

    private static bool applicationIsQuitting;

    public static bool Lazy;

    public static T Instance
    {
        get
        {
            if (applicationIsQuitting)
            {
                Debug.LogWarningFormat("[Singleton] Instance '{0}' already destroyed on application quit. Won't create again - returning null.", typeof(T));
                return null;
            }
            lock (_lock)
            {
                if (!instantiated)
                {
                    Object[] objects;
                    if (FindInactive) { objects = Resources.FindObjectsOfTypeAll(typeof(T)); }
                    else { objects = FindObjectsOfType(typeof(T)); }
                    if (objects == null || objects.Length < 1)
                    {
                        GameObject singleton = new GameObject();
                        singleton.name = string.Format("{0} [Singleton]", typeof(T));
                        Instance = singleton.AddComponent<T>();
                        Debug.LogWarningFormat("[Singleton] An Instance of '{0}' is needed in the scene, so '{1}' was created{2}", typeof(T), singleton.name, Persist ? " with DontDestoryOnLoad." : ".");
                    }
                    else if (objects.Length >= 1)
                    {
                        Instance = objects[0] as T;
                        if (objects.Length > 1)
                        {
                            Debug.LogWarningFormat("[Singleton] {0} instances of '{1}'!", objects.Length, typeof(T));
                            if (DestroyOthers)
                            {
                                for (int i = 1; i < objects.Length; i++)
                                {
                                    Debug.LogWarningFormat("[Singleton] Deleting extra '{0}' instance attached to '{1}'", typeof(T), objects[i].name);
                                    Destroy(objects[i]);
                                }
                            }
                        }
                        return instance;
                    }
                }
                return instance;
            }
        }
        protected set
        {
            instance = value;
            instantiated = true;
            instance.AwakeSingleton();
            if (Persist) { DontDestroyOnLoad(instance.gameObject); }
        }
    }

    // if Lazy = false and gameObject is active this will set instance
    // unless instance was called by another Awake method
    private void Awake()
    {
        if (Lazy) { return; }
        lock (_lock)
        {
            if (!instantiated)
            {
                Instance = this as T;
            }
            else if (DestroyOthers && Instance.GetInstanceID() != GetInstanceID())
            {
                Debug.LogWarningFormat("[Singleton] Deleting extra '{0}' instance attached to '{1}'", typeof(T), name);
                Destroy(this);
            }
        }
    }
    
    // this might be called for inactive singletons before Awake if FindInactive = true
    protected virtual void AwakeSingleton() {}

    protected virtual void OnDestroy()
    {
        applicationIsQuitting = true;
        instantiated = false;
    }
}

बेस क्लास के माध्यम से सिंगलटन कार्यान्वयन

ऐसी परियोजनाओं में जो कई सिंगलटन क्लासेस की सुविधा प्रदान करती हैं (जैसा कि अक्सर होता है), यह एकल वर्ग के लिए सिंगलटन व्यवहार को सार करने के लिए साफ और सुविधाजनक हो सकती है:

using UnityEngine;
using System.Collections.Generic;
using System;

public abstract class MonoBehaviourSingleton<T> : MonoBehaviour {
    
    private static Dictionary<Type, object> _singletons
        = new Dictionary<Type, object>();

    public static T Instance {
        get {
            return (T)_singletons[typeof(T)];
        }
    }

    void OnEnable() {
        if (_singletons.ContainsKey(GetType())) {
            Destroy(this);
        } else {
            _singletons.Add(GetType(), this);
            DontDestroyOnLoad(this);
        }
    }
}

एक MonoBehaviour तब MonoBehaourourSingleton का विस्तार करके सिंगलटन पैटर्न को लागू कर सकता है। इस दृष्टिकोण से सिंगलटन पर न्यूनतम पदचिह्न के साथ पैटर्न का उपयोग किया जा सकता है:

using UnityEngine;
using System.Collections;

public class SingletonImplementation : MonoBehaviourSingleton<SingletonImplementation> {

    public string Text= "String Instance";

    // Use this for initialisation
    IEnumerator Start () {
        var demonstration = "SingletonImplementation.Start()\n" +
                            "Note that the this text logs only once and\n"
                            "only one class instance is allowed to exist.";
        Debug.Log(demonstration);
        yield return new WaitForSeconds(2f);
        var secondInstance = new GameObject();
        secondInstance.AddComponent<SingletonImplementation>();
    }
   
}

ध्यान दें कि सिंगलटन पैटर्न के लाभों में से एक यह है कि उदाहरण के संदर्भ को सांख्यिकीय रूप से एक्सेस किया जा सकता है:

// Logs: String Instance
Debug.Log(SingletonImplementation.Instance.Text);

हालांकि ध्यान रखें, युग्मन को कम करने के लिए इस अभ्यास को कम से कम किया जाना चाहिए। यह दृष्टिकोण डिक्शनरी के उपयोग के कारण मामूली प्रदर्शन लागत पर भी आता है, लेकिन इस संग्रह में प्रत्येक एकल वर्ग का केवल एक उदाहरण हो सकता है, DRY सिद्धांत के अनुसार व्यापार बंद (खुद को दोहराएं नहीं), पठनीयता और सुविधा छोटी है।

सिंगलटन पैटर्न यूनिटी एंटिटी-कंपोनेंट सिस्टम का उपयोग करता है

मुख्य विचार एकल का प्रतिनिधित्व करने के लिए GameObjects का उपयोग करना है, जिसके कई फायदे हैं:

  • जटिलता को न्यूनतम रखता है लेकिन निर्भरता इंजेक्शन जैसी अवधारणाओं का समर्थन करता है
  • एकल-घटक प्रणाली के हिस्से के रूप में सिंगलेट्स के पास एक सामान्य एकता जीवनचक्र है
  • सिंगलेट्स को स्थानीय रूप से आलसी लोड और कैश्ड किया जा सकता है, जहां नियामक की जरूरत होती है (जैसे अपडेट लूप में)
  • किसी स्थिर क्षेत्र की आवश्यकता नहीं है
  • मौजूदा MonoBeviours / Components को सिंगलेट्स के रूप में उपयोग करने के लिए उन्हें संशोधित करने की आवश्यकता नहीं है
  • रीसेट करने में आसान (सिर्फ सिंग्लेटन्स गेमऑब्जेक्ट को नष्ट करें), अगले उपयोग पर फिर से आलसी लोड किया जाएगा
  • मॉक को इंजेक्ट करना आसान है (बस इसे इस्तेमाल करने से पहले मॉक के साथ इनिशियलाइज़ करें)
  • सामान्य एकता संपादक का उपयोग कर निरीक्षण और विन्यास पहले से ही संपादक समय पर हो सकता है ( एकता संपादक में एक सिंगलटन के स्क्रीनशॉट का स्क्रीनशॉट )

Test.cs (जो उदाहरण सिंगलटन का उपयोग करता है):

using UnityEngine;
using UnityEngine.Assertions;

public class Test : MonoBehaviour {
    void Start() {
        ExampleSingleton singleton = ExampleSingleton.instance;
        Assert.IsNotNull(singleton); // automatic initialization on first usage
        Assert.AreEqual("abc", singleton.myVar1);
        singleton.myVar1 = "123";
        // multiple calls to instance() return the same object:
        Assert.AreEqual(singleton, ExampleSingleton.instance); 
        Assert.AreEqual("123", ExampleSingleton.instance.myVar1);
    }
}

ExampleSingleton.cs (जिसमें एक उदाहरण और वास्तविक सिंगलटन वर्ग शामिल है):

using UnityEngine;
using UnityEngine.Assertions;

public class ExampleSingleton : MonoBehaviour {
    public static ExampleSingleton instance { get { return Singleton.get<ExampleSingleton>(); } }
    public string myVar1 = "abc";
    public void Start() { Assert.AreEqual(this, instance, "Singleton more than once in scene"); } 
}

/// <summary> Helper that turns any MonBehaviour or other Component into a Singleton </summary>
public static class Singleton {
    public static T get<T>() where T : Component {
        return GetOrAddGo("Singletons").GetOrAddChild("" + typeof(T)).GetOrAddComponent<T>();
    }
    private static GameObject GetOrAddGo(string goName) {
        var go = GameObject.Find(goName);
        if (go == null) { return new GameObject(goName); }
        return go;
    }
}

public static class GameObjectExtensionMethods { 
    public static GameObject GetOrAddChild(this GameObject parentGo, string childName) {
        var childGo = parentGo.transform.FindChild(childName);
        if (childGo != null) { return childGo.gameObject; } // child found, return it
        var newChild = new GameObject(childName);        // no child found, create it
        newChild.transform.SetParent(parentGo.transform, false); // add it to parent
        return newChild;
    }

    public static T GetOrAddComponent<T>(this GameObject parentGo) where T : Component {
        var comp = parentGo.GetComponent<T>();
        if (comp == null) { return parentGo.AddComponent<T>(); }
        return comp;
    }
}

GameObject के लिए दो विस्तार विधियाँ अन्य स्थितियों में भी सहायक होती हैं, यदि आपको उनकी आवश्यकता नहीं है तो उन्हें सिंगलटन वर्ग के अंदर ले जाएँ और उन्हें निजी बनाएं।

MonoBehaviour & ScriptableObject आधारित सिंगलटन क्लास

अधिकांश सिंगलटन उदाहरण बेस क्लास के रूप में मोनोबेहौर का उपयोग करते हैं। मुख्य नुकसान यह है कि यह सिंगलटन वर्ग केवल रन समय के दौरान रहता है। इसकी कुछ कमियां हैं:

  • कोड बदलने के अलावा अन्य एकल क्षेत्रों को सीधे संपादित करने का कोई तरीका नहीं है।
  • सिंग्लटन पर अन्य परिसंपत्तियों के संदर्भ को संग्रहीत करने का कोई तरीका नहीं है।
  • एकता यूआई घटना के गंतव्य के रूप में सिंगलटन को स्थापित करने का कोई तरीका नहीं है। मैं अंत में "Proxy Components" का उपयोग करते हुए कहता हूं कि इसका एकमात्र प्रस्ताव 1 लाइन विधियां हैं जो "GameManager.Instance.SomeGlobalMethod ()" कहते हैं।

जैसा कि टिप्पणी पर उल्लेख किया गया है कि कार्यान्वयन हैं जो इसे आधार वर्ग के रूप में ScriptableObjects का उपयोग करके हल करने की कोशिश करते हैं लेकिन मोनोबीहैर के रन टाइम लाभ खो देते हैं। यह कार्यान्वयन रन टाइम के दौरान एक आधार वर्ग और एक संबद्ध मोनोबीवर के रूप में ScriptableObject का उपयोग करके इस समस्या को हल करता है:

  • यह एक संपत्ति है इसलिए इसके गुणों को किसी अन्य एकता संपत्ति की तरह संपादक पर अपडेट किया जा सकता है।
  • यह एकता क्रमांकन प्रक्रिया के साथ अच्छी तरह से खेलता है।
  • संपादक से अन्य परिसंपत्तियों के लिए सिंगलटन पर संदर्भ निर्दिष्ट करना संभव है (निर्भरता संपादक के माध्यम से इंजेक्ट की जाती है)।
  • एकता की घटनाएँ सीधे सिंग्लटन पर विधियों को बुला सकती हैं।
  • "SingletonClassName.Instance" का उपयोग करके इसे कोडबेस में कहीं से भी कॉल कर सकते हैं
  • रन टाइम तक पहुँचने के लिए मोनोबीहैर इवेंट और तरीके जैसे: अपडेट, अवेक, स्टार्ट, फिक्स्डअपडेट, स्टार्टकॉरटाइन, आदि।
/************************************************************
 * Better Singleton by David Darias
 * Use as you like - credit where due would be appreciated :D
 * Licence: WTFPL V2, Dec 2014
 * Tested on Unity v5.6.0 (should work on earlier versions)
 * 03/02/2017 - v1.1 
 * **********************************************************/

using System;
using UnityEngine;
using SingletonScriptableObjectNamespace;

public class SingletonScriptableObject<T> : SingletonScriptableObjectNamespace.BehaviourScriptableObject where T : SingletonScriptableObjectNamespace.BehaviourScriptableObject
{
    //Private reference to the scriptable object
    private static T _instance;
    private static bool _instantiated;
    public static T Instance
    {
        get
        {
            if (_instantiated) return _instance;
            var singletonName = typeof(T).Name;
            //Look for the singleton on the resources folder
            var assets = Resources.LoadAll<T>("");
            if (assets.Length > 1) Debug.LogError("Found multiple " + singletonName + "s on the resources folder. It is a Singleton ScriptableObject, there should only be one.");
            if (assets.Length == 0)
            {
                _instance = CreateInstance<T>();
                Debug.LogError("Could not find a " + singletonName + " on the resources folder. It was created at runtime, therefore it will not be visible on the assets folder and it will not persist.");
            }
            else _instance = assets[0];
            _instantiated = true;
            //Create a new game object to use as proxy for all the MonoBehaviour methods
            var baseObject = new GameObject(singletonName);
            //Deactivate it before adding the proxy component. This avoids the execution of the Awake method when the the proxy component is added.
            baseObject.SetActive(false);
            //Add the proxy, set the instance as the parent and move to DontDestroyOnLoad scene
            SingletonScriptableObjectNamespace.BehaviourProxy proxy = baseObject.AddComponent<SingletonScriptableObjectNamespace.BehaviourProxy>();
            proxy.Parent = _instance;
            Behaviour = proxy;
            DontDestroyOnLoad(Behaviour.gameObject);
            //Activate the proxy. This will trigger the MonoBehaviourAwake. 
            proxy.gameObject.SetActive(true);
            return _instance;
        }
    }
    //Use this reference to call MonoBehaviour specific methods (for example StartCoroutine)
    protected static MonoBehaviour Behaviour;
    public static void BuildSingletonInstance() { SingletonScriptableObjectNamespace.BehaviourScriptableObject i = Instance; }
    private void OnDestroy(){ _instantiated = false; }
}

// Helper classes for the SingletonScriptableObject
namespace SingletonScriptableObjectNamespace
{
    #if UNITY_EDITOR
    //Empty custom editor to have cleaner UI on the editor.
    using UnityEditor;
    [CustomEditor(typeof(BehaviourProxy))]
    public class BehaviourProxyEditor : Editor
    {
        public override void OnInspectorGUI(){}
    }
    
    #endif
    
    public class BehaviourProxy : MonoBehaviour
    {
        public IBehaviour Parent;

        public void Awake() { if (Parent != null) Parent.MonoBehaviourAwake(); }
        public void Start() { if (Parent != null) Parent.Start(); }
        public void Update() { if (Parent != null) Parent.Update(); }
        public void FixedUpdate() { if (Parent != null) Parent.FixedUpdate(); }
    }

    public interface IBehaviour
    {
        void MonoBehaviourAwake();
        void Start();
        void Update();
        void FixedUpdate();
    }

    public class BehaviourScriptableObject : ScriptableObject, IBehaviour
    {
        public void Awake() { ScriptableObjectAwake(); }
        public virtual void ScriptableObjectAwake() { }
        public virtual void MonoBehaviourAwake() { }
        public virtual void Start() { }
        public virtual void Update() { }
        public virtual void FixedUpdate() { }
    }
}

यहाँ एक उदाहरण है GameManager सिंगलटन क्लास का उपयोग करके सिंगलटनस्क्रिप्टेबलऑब्जेक्ट (बहुत सारी टिप्पणियों के साथ):

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//this attribute is optional but recommended. It will allow the creation of the singleton via the asset menu.
//the singleton asset should be on the Resources folder.
[CreateAssetMenu(fileName = "GameManager", menuName = "Game Manager", order = 0)]
public class GameManager : SingletonScriptableObject<GameManager> {

    //any properties as usual
    public int Lives;
    public int Points;

    //optional (but recommended)
    //this method will run before the first scene is loaded. Initializing the singleton here
    //will allow it to be ready before any other GameObjects on every scene and will
    //will prevent the "initialization on first usage". 
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    public static void BeforeSceneLoad() { BuildSingletonInstance(); }

    //optional,
    //will run when the Singleton Scriptable Object is first created on the assets. 
    //Usually this happens on edit mode, not runtime. (the override keyword is mandatory for this to work)
    public override void ScriptableObjectAwake(){
        Debug.Log(GetType().Name + " created." );
    }

    //optional,
    //will run when the associated MonoBehavioir awakes. (the override keyword is mandatory for this to work)
    public override void MonoBehaviourAwake(){
        Debug.Log(GetType().Name + " behaviour awake." );

        //A coroutine example:
        //Singleton Objects do not have coroutines.
        //if you need to use coroutines use the atached MonoBehaviour
        Behaviour.StartCoroutine(SimpleCoroutine());
    }

    //any methods as usual
    private IEnumerator SimpleCoroutine(){
        while(true){
            Debug.Log(GetType().Name + " coroutine step." );
            yield return new WaitForSeconds(3);
        }
    }

    //optional,
    //Classic runtime Update method (the override keyword is mandatory for this to work).
    public override void Update(){

    }

    //optional,
    //Classic runtime FixedUpdate method (the override keyword is mandatory for this to work).
    public override void FixedUpdate(){

    }
}

/*
*  Notes:
*  - Remember that you have to create the singleton asset on edit mode before using it. You have to put it on the Resources folder and of course it should be only one. 
*  - Like other Unity Singleton this one is accessible anywhere in your code using the "Instance" property i.e: GameManager.Instance
*/


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow