简单资源管理器

来源:互联网 发布:html加载完后执行js 编辑:程序博客网 时间:2024/05/22 10:47

下面共享一个我自己的资源管理器,很简单的,看起来也明白,主要是我是新手,不想写的太复杂,自己项目使用的好用,便是最好的。

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("神奇唉~");        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401

其实主要的思想就是,在游戏里面动态加载的资源都通过这里面的add方法,将加在进来的资源引用上,然后在游戏里面就可以直接根据资源名称从这里面进行寻找,这里面还有资源包的销毁,亲测可用,代码也比较简单,很好读,我们做的是棋牌大厅一类的,但是都没有上线,所以这里面还是可以再进行优化的,比如将游戏类型放进去,也作为一个目录的一级,在单个游戏退出后,根据需求将这个游戏资源销毁掉,可以根据配置什么的,参考一下喽。