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# indexof 
Csharp :: c# make request to rest api 
Csharp :: c# list subfolders 
Csharp :: c# get command line arguments 
Csharp :: forech unity 
Csharp :: how to allow user import image c# 
Csharp :: unity debug c# code with console 
Csharp :: write line to file c# 
Csharp :: add item to list c# 
Csharp :: c# byte array to file 
Csharp :: how to save datagridview data to database in c# windows application 
Csharp :: unity quaternion 
Csharp :: get description from enum c# 
Csharp :: c# winforms textbox select text 
Csharp :: convert string to list int c# 
Csharp :: last two characters of string c# 
Csharp :: ienumerable count 
Csharp :: set parent of gameobject unity 
Csharp :: get out of foreach statement c# 
Csharp :: c# multiline comment 
Csharp :: how to create a random vector2 in unity 
Csharp :: find character from string c# count 
Csharp :: c# linq select only unique values from list 
Csharp :: raylib c# 
Csharp :: unity rewarded ads 
Csharp :: datatable to array c# 
Csharp :: if file exist rename c# 
Csharp :: unity no serializefield 
Csharp :: c# get certain character from string 
Csharp :: on collision enter by layer 2d unity 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =