using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//标准单例类
public class NormalSingleton<T> where T : class, new()
{
private static T _single;
public static T Singel
{
get
{
if (_single == null)
{
T t = new T();
if (t is MonoBehaviour)
{
Debug.LogError("Mono类请使用MonoSingleTon!");
return null;
}
_single = t;
}
return _single;
}
}
}
MonoSingleton.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Mono单例类
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _singleton;
public static T SingleTon
{
get
{
if (_singleton == null)
{
_singleton = FindObjectOfType<T>();
if (_singleton == null)
{
Debug.LogError("场景中未找到类的对象,类名为:"+typeof(T).Name);
}
}
return _singleton;
}
}
private void Awake()
{
if (_singleton == null)
{
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}
通过物体路径 自动挂载脚本(游戏预制物体名字 和 脚本名字得一样)(方式一)
ILoader接口 给ResourceLoder 和 ABLoader实现
ILoader.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface ILoader
{
/// <summary>
/// Resource方法加载物体
/// </summary>
/// <param name="path">加载物体的路径</param>
/// <param name="parent">指定父物体</param>
/// <returns></returns>