下面共享一个我自己的资源管理器,很简单的,看起来也明白,主要是我是新手,不想写的太复杂,自己项目使用的好用,便是最好的。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class GameObjectMgr
{
public static Dictionary<string, GameObject> loadedPerfabList = new Dictionary<string,GameObject>(); //存放已经加载的预设资源
public static Dictionary<string, AssetBundle> loadedAssetList = new Dictionary<string,AssetBundle>(); //存放已经加载的AB包
public static Dictionary<string, GameObject> createdObjList = new Dictionary<string,GameObject>(); //存放已经创建的对象
public static Dictionary<string, Dictionary<string, System.Object>> littlePool = new Dictionary<string, Dictionary<string, object>>();
public static string[] destoryPerfabList = null; //需要销毁的已经加载的预设
public static string[] destoryAssetList = null; //需要销毁的AB包,unload(false);
public static string[] destoryObjNameList = null; //需要销毁的对象名字列表;
public static void AddObjectToDictionary(string objName, System.Type _type, System.Object _obj)
{
//Debug.Log("添加=="+objName);
if (littlePool.ContainsKey(_type.ToString()))
{
littlePool[_type.ToString()][objName] = _obj;
}
else {
Dictionary<string, System.Object> adder = new Dictionary<string, System.Object>();
adder[objName] = _obj;
littlePool[_type.ToString()] = adder;
}
}
public static System.Object GetObjFromDictionary(string _obgName,System.Type _type) {
if (littlePool.ContainsKey(_type.ToString()))
{
if (littlePool[_type.ToString()].ContainsKey(_obgName))
{
if (littlePool[_type.ToString()][_obgName] != null)
{
return littlePool[_type.ToString()][_obgName];
}
else {
littlePool[_type.ToString()].Remove(_obgName);
return null;
}
}
return null;
}
else {
return null;
}
}
//设置是否显示
public static void SetObjActivePority(string _name, bool _active)
{
if (createdObjList == null)
{
//Debug.Log("出错拉,那窗口没有: "+_name);
}
else
{
foreach (string key in createdObjList.Keys)
{
if (key == _name)
{
if (createdObjList[key] != null)
{
createdObjList[key].SetActive(_active);
}
else
{
// Debug.Log("出错拉,窗口丢失: "+_name);
createdObjList.Remove(_name);
}
}
}
}
}
//获得一个已经创建的对象
public static GameObject GetGameObjectByName(string _name)
{
if (createdObjList == null)
{
//Debug.Log("出错拉,那窗口没有: "+_name);
return null;
}
else
{
foreach (string key in createdObjList.Keys)
{
if (key == _name)
{
if (createdObjList[key] != null)
{
return createdObjList[key];
}
else
{
//Debug.Log("出错拉,窗口丢失: "+_name);
createdObjList.Remove(_name);
return null;
}
}
}
}
return null;
}
//获得一个已经加载的预设
public static GameObject GetPrefabByName(string _name)
{
return (GameObject)GetObjFromDictionary(_name, typeof(GameObject));
}
//查看预设是否加载
public static bool IsPerfabLoaded(string _name)
{
if (loadedPerfabList == null)
{
return false;
}
else
{
foreach (string key in loadedPerfabList.Keys)
{
if (key == _name)
{
if (loadedPerfabList[key] != null)
return true;
else
{
loadedPerfabList.Remove(_name);
return false;
}
}
}
}
return false;
}
//对象是否已经被创建
public static bool IsObjCreated(string _name)
{
if (createdObjList == null)
{
return false;
}
else
{
foreach (string key in createdObjList.Keys)
{
if (key == _name)
{
if (createdObjList[key] != null)
{
return true;
}
else
{
createdObjList.Remove(_name);
return false;
}
}
}
}
return false;
}
//添加已经创建的GameObject键值对
public static void AddCreatedObjList(string _name, GameObject _obj)
{
//Debug.Log ("AddCreatedObjList>>>>>>>>>>>>>>>>>>>>"+_name);
if (createdObjList.ContainsKey(_name))
{
//Debug.Log(string.Format("名字是 {0} 的对象已经添加,不好",_name));
return;
}
createdObjList.Add(_name, _obj);
}
//添加已经加载的assetbundle键值对
public static void AddloadedAssetList(string _name, AssetBundle _obj)
{
//Debug.Log ("AddloadedAssetList>>>>>>>>>>>>>>>>>>>>"+_name);
if (loadedAssetList.ContainsKey(_name))
{
// Debug.Log(string.Format("名字是 {0} 的assetbundle已经添加,不好",_name));
return;
}
loadedAssetList.Add(_name, _obj);
}
//添加已经加载的预设键值对
public static void AddloadedPerfabList(string _name, GameObject _obj)
{
//Debug.Log ("AddloadedPerfabList>>>>>>>>>>>>>>>>>>>>"+_name);
if (loadedPerfabList.ContainsKey(_name))
{
//Debug.Log(string.Format("名字是 {0} 的预设已经添加,不用再次添加",_name));
return;
}
loadedPerfabList.Add(_name, _obj);
}
//添加需要清理的预设名字
public static void AdddestoryPerfabList(string _name)
{
//Debug.Log ("AdddestoryPerfabList>>>>>>>>>>>>>>>>>>>>"+_name);
string[] list = null;
int i = 0;
if (destoryPerfabList != null)
{
list = new string[1 + destoryPerfabList.Length];
foreach (string str in destoryPerfabList)
{
list[i] = destoryPerfabList[i];
i++;
}
}
else
{
i = 0;
list = new string[1];
}
list[i] = _name;
destoryPerfabList = null;
destoryPerfabList = new string[i + 1];
i = 0;
foreach (string str in destoryPerfabList)
{
destoryPerfabList[i] = list[i];
i++;
}
list = null;
return;
}
//添加待销毁对象名字
public static void AdddestoryObjNameList(string _name)
{
//Debug.Log ("AdddestoryObjNameList>>>>>>>>>>>>>>>>>>>>"+_name);
string[] list = null;
int i = 0;
if (destoryObjNameList != null)
{
list = new string[1 + destoryObjNameList.Length];
foreach (string str in destoryObjNameList)
{
list[i] = destoryObjNameList[i];
i++;
}
}
else
{
i = 0;
list = new string[1];
}
list[i] = _name;
destoryObjNameList = null;
destoryObjNameList = new string[i + 1];
i = 0;
foreach (string str in destoryObjNameList)
{
destoryObjNameList[i] = list[i];
i++;
}
list = null;
return;
}
//添加待清理assetbundle名字
public static void AdddestoryABList(string _name)
{
//Debug.Log ("AdddestoryABList>>>>>>>>>>>>>>>>>>>>"+_name);
string[] list = null;
int i = 0;
if (destoryAssetList != null)
{
list = new string[1 + destoryAssetList.Length];
foreach (string str in destoryAssetList)
{
list[i] = destoryAssetList[i];
i++;
}
}
else
{
i = 0;
list = new string[1];
}
list[i] = _name;
destoryAssetList = null;
destoryAssetList = new string[i + 1];
i = 0;
foreach (string str in destoryAssetList)
{
destoryAssetList[i] = list[i];
i++;
}
list = null;
return;
}
//清理内存
public static void FreeMemory()
{
//Debug.Log ("FreeMemory>>>>>>>>>>>>>>>>>>>>");
if (destoryObjNameList == null)
{
//Debug.Log("不需要释放资源");
return;
}
int i = 0;
if (destoryObjNameList != null)
{
i = destoryObjNameList.Length;
for (int j = i - 1; j >= 0; j--)
{
//Debug.Log("j==="+j+"i=="+i);
if (destoryObjNameList[j] != null)
if (createdObjList.ContainsKey(destoryObjNameList[j]))
{
GameObject obj = createdObjList[destoryObjNameList[j]];
createdObjList.Remove(destoryObjNameList[j]);
//Debug.Log("销毁对象 "+obj.name);
if (obj != null)
GameObject.DestroyImmediate(obj, true);
}
destoryObjNameList[j] = null;
}
}
destoryObjNameList = null;
if (destoryPerfabList != null)
{
//Debug.Log("需要销毁预设啊");
RemoveFromDictionary(destoryPerfabList,typeof(GameObject));
}
destoryPerfabList = null;
if (destoryAssetList == null || destoryAssetList.Length == 0)
{
//Debug.Log ("不需要释放AB包,可能会出现这种情况");
}
else
{
//销毁assetbundle内存镜像
i = destoryAssetList.Length;
for (int j = i - 1; j >= 0; j--)
{
if (loadedAssetList != null)
if (destoryAssetList[j] != null)
if (loadedAssetList.ContainsKey(destoryAssetList[j]))
{
//Debug.LogError("销毁assetbundle : " + destoryAssetList[j]);
AssetBundle asset = loadedAssetList[destoryAssetList[j]];
loadedAssetList.Remove(destoryAssetList[j]);
if (asset != null)
asset.Unload(true);
}
destoryAssetList[j] = null;
}
}
destoryAssetList = null;
GC.Collect(0);
GC.Collect(1);
Resources.UnloadUnusedAssets();
}
////通知lua创建对象
//public static GameObject CallLuaCreatePanel(string _message){
// //Debug.Log("CallLuaCreatePanel : "+_message);
// Global.CallGlobalLuaFunction("LuaInitPrefabs.OnInitPanelOperateMessage",_message);
// return createdObjList[_message]as GameObject;
//}
//通知lua对创建的Panel进行销毁或者隐藏
public static void CallLuaDestoryPanel(string _message, bool _isdestory)
{
//Debug.Log("CallLuaDestoryPanel : "+_message);
Global.CallGlobalLuaFunction("LuaInitPrefabs.OnDesPanelOperateMessage", _message, _isdestory);
}
static void RemoveFromDictionary(string[] nameList,System.Type _ty) {
if (littlePool.ContainsKey(_ty.ToString()))
{
foreach(string str in nameList){
if (littlePool[_ty.ToString()].ContainsKey(str))
{
littlePool[_ty.ToString()].Remove(str);
}
//else {
//Debug.Log("压根没有="+str);
//}
}
}
else {
Debugger.Log("神奇唉~");
}
}
}
其实主要的思想就是,在游戏里面动态加载的资源都通过这里面的add方法,将加在进来的资源引用上,然后在游戏里面就可以直接根据资源名称从这里面进行寻找,这里面还有资源包的销毁,亲测可用,代码也比较简单,很好读,我们做的是棋牌大厅一类的,但是都没有上线,所以这里面还是可以再进行优化的,比如将游戏类型放进去,也作为一个目录的一级,在单个游戏退出后,根据需求将这个游戏资源销毁掉,可以根据配置什么的,参考一下喽。