25 lines
622 B
C#
25 lines
622 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class SingletonGameObject<T> : MonoBehaviour where T : Component
|
|
{
|
|
private static T instance;
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = FindObjectOfType<T>();
|
|
if (instance == null)
|
|
{
|
|
instance = new GameObject("SingletonGameObject").AddComponent<T>();
|
|
}
|
|
DontDestroyOnLoad(instance.gameObject);
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
}
|