feat:1、添加项目
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public interface ISingleton
|
||||
{
|
||||
void OnSingletonInit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ee67e6ba4fa478bb8b260f227b6cc33
|
||||
timeCreated: 1686105364
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class Singleton<T> : ISingleton, IDisposable where T : Singleton<T>, new()
|
||||
{
|
||||
private static T mInstance;
|
||||
|
||||
private static readonly object LockObject = new();
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (LockObject)
|
||||
{
|
||||
mInstance ??= new T();
|
||||
mInstance.OnSingletonInit();
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
mInstance = null;
|
||||
}
|
||||
|
||||
public void OnSingletonInit()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a4250ff340d9034da1d59b2f9cffb46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,100 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public abstract class SingletonUnity<T> : MonoBehaviour, ISingleton where T : SingletonUnity<T>
|
||||
{
|
||||
private static T mInstance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (mInstance != null)
|
||||
{
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
if (ApplicationIsQuitting)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
GameObject instanceGO = new GameObject(typeof(T).Name);
|
||||
mInstance = instanceGO.AddComponent<T>();
|
||||
SetSelfParentRoot(instanceGO, mInstance.ParentRootName);
|
||||
mInstance.OnSingletonInit();
|
||||
return mInstance;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ApplicationIsQuitting;
|
||||
protected virtual string ParentRootName { get; private set; }
|
||||
|
||||
private static void SetSelfParentRoot(GameObject go, string parentRootName)
|
||||
{
|
||||
if (!AppObjConst.EngineSingletonGo) return;
|
||||
|
||||
if (parentRootName == null)
|
||||
{
|
||||
go.transform.SetParent(AppObjConst.EngineSingletonGo.transform, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Transform sinleonRoot = AppObjConst.EngineSingletonGo.transform;
|
||||
Transform rootTF = sinleonRoot.Find(parentRootName);
|
||||
if (rootTF == null)
|
||||
{
|
||||
GameObject rootGo = new GameObject(parentRootName);
|
||||
switch (parentRootName)
|
||||
{
|
||||
case AppObjConst.MonoManagerGoName:
|
||||
AppObjConst.MonoManagerGo = rootGo;
|
||||
break;
|
||||
case AppObjConst.DispatcherGoName:
|
||||
AppObjConst.DispatcherGo = rootGo;
|
||||
break;
|
||||
case AppObjConst.LoaderGoName:
|
||||
AppObjConst.LoaderGo = rootGo;
|
||||
break;
|
||||
}
|
||||
|
||||
rootTF = rootGo.transform;
|
||||
rootTF.transform.SetParent(sinleonRoot, false);
|
||||
}
|
||||
|
||||
go.transform.SetParent(rootTF.transform, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void New()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
mInstance = null;
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
mInstance = null;
|
||||
}
|
||||
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
ApplicationIsQuitting = true;
|
||||
if (mInstance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Destroy(mInstance.gameObject);
|
||||
}
|
||||
|
||||
public void OnSingletonInit()
|
||||
{
|
||||
mInstance.New();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04ea3647f7ad1b4448f7ba33061ab319
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user