define singleton in programming code example
Example 1: singelton code
using UnityEngine;
public class SingeltonObject : MonoBehaviour
{
void Awake()
{
SetUpSingelton();
}
private void SetUpSingelton()
{
if (FindObjectsOfType(GetType()).Length > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
}
Example 2: singleton pattern
class Singleton(object):
__instance = None
def __new__(cls, *args):
if cls.__instance is None:
cls.__instance = object.__new__(cls, *args)
return cls.__instance