音频内容

This commit is contained in:
2024-09-19 01:12:10 +08:00
parent 1c390954ce
commit 35bd460011
80 changed files with 2205 additions and 59 deletions
@@ -0,0 +1,17 @@
/*
* 单例接口
*/
namespace AibisDream.Kit
{
/// <summary>
/// 单例接口
/// </summary>
public interface ISingleton
{
/// <summary>
/// 单例初始化(继承当前接口的类都需要实现该方法)
/// </summary>
void OnSingletonInit();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a230db405379d3a428a5cba018978da9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,36 @@
using UnityEngine;
namespace AibisDream.Kit
{
public abstract class Singleton<T> : MonoBehaviour, ISingleton where T : Singleton<T>
{
public static T Instance { get; private set; }
protected virtual void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
else
{
Instance = (T)this;
OnSingletonInit();
}
}
public virtual void OnSingletonInit()
{
}
public static bool IsInitialized => Instance != null;
protected virtual void OnDestroy()
{
if (Instance == this)
{
Instance = null;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 214dbdcae17401d4e94285d5e416ce66
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
using System;
using System.Reflection;
namespace AibisDream.Kit
{
public abstract class SingletonBase<T> : ISingleton where T : SingletonBase<T>
{
private static T _instance;
public static T Instance
{
get
{
if (_instance != null) return _instance;
// 先获取所有非public的构造方法
var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
// 从ctors中获取无参的构造方法
var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
if (ctor == null)
throw new Exception("Non-public ctor() not found!");
// 调用构造方法
_instance = ctor.Invoke(null) as T;
_instance?.OnSingletonInit();
return _instance;
}
}
public abstract void OnSingletonInit();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0e06ce6997ac4a14fa439ac9c3747fe9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,52 @@
using System;
using System.Reflection;
namespace AibisDream.Kit
{
public class SingletonProp<T> where T : class, ISingleton
{
/// <summary>
/// 静态实例
/// </summary>
private static T _instance;
/// <summary>
/// 标签锁
/// </summary>
private static readonly object _lock = new();
public static T Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
// 先获取所有非public的构造方法
var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
// 从ctors中获取无参的构造方法
var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
if (ctor == null)
throw new Exception("Non-public ctor() not found!");
// 调用构造方法
_instance = ctor.Invoke(null) as T;
_instance?.OnSingletonInit();
return _instance;
}
}
return _instance;
}
}
/// <summary>
/// 资源释放
/// </summary>
public static void Dispose()
{
_instance = null;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6c24ee94a9ee48aca0977131b6d772e0
timeCreated: 1726411869