Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

whats singleton unity

// allows all non-static instances to access the same static instance
public class NonStaticClass : MonoBehaviour {
    private static NonStaticClass staticInstance;
}
Comment

how to make a singleton in unity

 void Awake()
 {
   if (instance == null)
     instance = this;
   else if (instance != this)
     Destroy(gameObject);
 }
Comment

how to create a singleton in unity

public class SomeClass : MonoBehaviour {
    private static SomeClass _instance;

    public static SomeClass Instance { get { return _instance; } }


    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        } else {
            _instance = this;
          	DontDestroyOnLoad(gameObject);
        }
    }
}
Comment

unity singleton

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

public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
    #region  Variables
    protected static bool Quitting { get; private set; }

    private static readonly object Lock = new object();
    private static Dictionary<System.Type, Singleton<T>> _instances;

    public static T Instance
    {
        get
        {
            if (Quitting)
            {
                return null;
            }
            lock (Lock)
            {
                if (_instances == null)
                    _instances = new Dictionary<System.Type, Singleton<T>>();

                if (_instances.ContainsKey(typeof(T)))
                    return (T)_instances[typeof(T)];
                else
                    return null;
            }
        }
    }

    #endregion

    #region  Methods
    private void OnEnable()
    {
        if (!Quitting)
        {
            bool iAmSingleton = false;

            lock (Lock)
            {
                if (_instances == null)
                    _instances = new Dictionary<System.Type, Singleton<T>>();

                if (_instances.ContainsKey(this.GetType()))
                    Destroy(this.gameObject);
                else
                {
                    iAmSingleton = true;

                    _instances.Add(this.GetType(), this);

                    DontDestroyOnLoad(gameObject);
                }
            }

            if(iAmSingleton)
                OnEnableCallback();
        }
    }

    private void OnApplicationQuit()
    {
        Quitting = true;

        OnApplicationQuitCallback();
    }

    protected abstract void OnApplicationQuitCallback();

    protected abstract void OnEnableCallback();
    #endregion
}
Comment

unity singleton

public class example : MonoBehaviour 
{
    public static example my_object { get; private set; } ;

    float my_player_health=100;

     private void Awake()
    {
        if (my_object != null && my_object != this)
        {
            Destroy(this);
        } else {
            my_object = this;
        }
       DontDestroyOnLoad(this.gameObject);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# round to closest multiple 
Csharp :: how to convert timestamp to datetime c# 
Csharp :: two linked list intersection 
Csharp :: how to create url parameters for URi C# 
Csharp :: c# if isset 
Csharp :: change size of button c# 
Csharp :: c# get a value from value tuple list 
Csharp :: c# list foreach lambda multiple actions 
Csharp :: csharp csvhelper 
Csharp :: convert path to uri c# 
Csharp :: math with c sharp 
Csharp :: unity check if gameobject is inside collider 
Csharp :: drop down list razor example 
Csharp :: check if two timespans intersect c# 
Csharp :: c# standard microphone decibels 
Csharp :: IsInstanceOf nunit 
Csharp :: Match one of 1, 2, x or X, or nothing 
Csharp :: clickable table row asp.net core 
Csharp :: c# get index of item in list 
Csharp :: c# substring find word 
Csharp :: interop C# save as and replace 
Csharp :: unity gun clipping through walls 
Csharp :: get first number in string C# 
Csharp :: minimum of three numbers 
Csharp :: c# convert excel column index to letter 
Csharp :: last index for array c# 
Csharp :: c# listview add items horizontally 
Csharp :: select many vs select 
Csharp :: how to insert data into multiple tables using asp.net c# 
Csharp :: c# list object 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =