提交 426374aa 编写于 作者: Emo_Tiny's avatar Emo_Tiny

全局对象管理中心

上级 cc66e218
fileFormatVersion: 2
guid: f306c8709b26942118dae1897b73e0e7
folderAsset: yes
DefaultImporter:
userData:
fileFormatVersion: 2
guid: 37017dbb75686814ab009a8e02cc3726
guid: 350c7bd867107694fa403c4a21f12614
folderAsset: yes
DefaultImporter:
externalObjects: {}
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ZFramework.Core
{
public class GlobalObjectManager
{
private static Dictionary<string, GameObject> _gameObjectsDic;
private static List<IGlobalObject> _objectList;
public static void Retain(GameObject go)
{
if (_gameObjectsDic == null)
{
_gameObjectsDic = new Dictionary<string, GameObject>();
}
if (!_gameObjectsDic.ContainsKey(go.name))
{
_gameObjectsDic.Add(go.name,go);
if (Application.isPlaying)
{
UnityEngine.Object.DontDestroyOnLoad(go);
}
}
}
public static void Retain(IGlobalObject go )
{
if (_objectList == null)
{
_objectList = new List<IGlobalObject>();
}
_objectList.Add(go);
}
public static void Release(GameObject go)
{
if (_gameObjectsDic!= null && _gameObjectsDic.ContainsKey(go.name))
{
_gameObjectsDic.Remove(go.name);
GameObject.Destroy(go);
}
}
public static GameObject GetGameObject(string name)
{
GameObject go = null;
if (_gameObjectsDic!= null)
{
//return _gameObjectsDic[name];
_gameObjectsDic.TryGetValue(name,out go);
}
return go;
}
}
}
fileFormatVersion: 2
guid: 5f9395a1e634c5b499d0f49bab3d6dd6
folderAsset: yes
DefaultImporter:
guid: 60fae4a4f20e9294d84b5c7d18697918
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
namespace ZFramework.Core
{
public class ResourceManager
{
}
}
\ No newline at end of file
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ZFramework.Core
{
public class SingleMonoBehaviorObject<T> : MonoBehaviour where T : SingleMonoBehaviorObject<T>
{
private static T _instance;
private void Awake()
{
if (CheckInstance())
OnLoad();
}
private bool CheckInstance()
{
if (this == Instance)
{
return true;
}
Destroy(gameObject);
return false;
}
protected virtual void OnLoad()
{
}
protected void OnDestroy()
{
if (_instance == this)
{
_instance = null;
}
}
/// <summary>
/// 判断对象是否有效
/// </summary>
public static bool IsValid
{
get
{
return _instance != null;
}
}
public static T Active()
{
return Instance;
}
/// <summary>
/// 实例化
/// </summary>
public static T Instance
{
get
{
if (_instance == null)
{
System.Type type = typeof(T);
string name = type.Name;
GameObject go = GlobalObjectManager.GetGameObject(name);
if (go == null)
{
go = GameObject.Find($"/{name}");
if (go == null)
{
go = new GameObject(name);
go.transform.position = Vector3.one;
}
GlobalObjectManager.Retain(go);
}
if (go!= null)
{
_instance = go.GetComponent<T>();
if (_instance == null)
{
_instance = go.AddComponent<T>();
}
}
if (_instance == null)
{
Debug.LogError($"Can't create SingletonBehaviour<{typeof(T)}>");
}
}
return _instance;
}
}
}
}
fileFormatVersion: 2
guid: c20df96b8dfc022479e6cb96c48e4535
folderAsset: yes
DefaultImporter:
guid: f90b1510b8a85d54599012e3d0d436fa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZFramework.Core;
namespace ZFramework.Core
{
public interface IGlobalObject
{
void Active();
void Release();
}
public abstract class SingleObject <T> : IGlobalObject where T : SingleObject<T>,new()
{
protected static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = new T();
_instance.Init();
GlobalObjectManager.Retain(_instance);
}
return _instance;
}
}
public static bool IsValid
{
get
{
return _instance != null;
}
}
protected virtual void Init()
{
}
public virtual void Active()
{
}
public virtual void Release()
{
}
}
}
fileFormatVersion: 2
guid: d235aef8e5460c544ab26d87a1bfd5ee
folderAsset: yes
DefaultImporter:
guid: 1663409355ffc3647842bd78629025c6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
namespace Script.ResourceMgr.LoadRS
{
public class ResourceLoadMgr
{
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册