提交 13bba21d 编写于 作者: yqian1000's avatar yqian1000

新增创建挂载预制体方法

上级 a8cbe91d
using Unity.VisualScripting;
using UnityEngine;
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
GameObject singleton = new GameObject(typeof(T).Name);
_instance = singleton.AddComponent<T>();
DontDestroyOnLoad(singleton);
}
}
return _instance;
}
}
protected virtual void OnEnable()
{
if (_instance == null)
{
_instance = this as T;
DontDestroyOnLoad(gameObject);
}
}
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
GameObject singleton = new GameObject(typeof(T).Name);
_instance = singleton.AddComponent<T>();
DontDestroyOnLoad(singleton);
}
}
return _instance;
}
}
protected virtual void OnEnable()
{
if (_instance == null)
{
_instance = this as T;
DontDestroyOnLoad(gameObject);
}
}
static public T CreateSingletonMono(string prefabName, Transform parent = null)
{
if (string.IsNullOrEmpty(prefabName))
{
GameObject prefab = ResourceManager.LoadGamePrefab(prefabName);
GameObject go = GameObject.Instantiate(prefab, parent);
_instance = go.AddComponent<T>();
DontDestroyOnLoad(go);
return _instance;
}
else
{
return Instance;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册