DCS/ruiyiweiUX/Assets/GeneralTools/Scripts/Always/SingletonGameObject.cs

25 lines
622 B
C#
Raw Normal View History

2026-06-09 13:59:11 +08:00
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;
}
}
}