| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401 |
- using UnityEngine;
- using System.IO;
- using System.Collections.Generic;
- using UnityEngine.Playables;
- using UnityEngine.Video;
- using LuaInterface;
- public delegate void ResourceLoadCallback<T>(T o, string assetPath, params string[] assetNames);
- public delegate void ResourceLoadCallbackWithSeqId<T>(T o, long seqId,string assetPath,object[] userdatas, params string[] assetNames);
- public class ResourceInfo
- {
- private string path;
- private string name;
- private Object asset;
- private float lastUsedTime;
- public string Path
- {
- get { return path; }
- }
- public string Name
- {
- get { return name; }
- }
- public Object Asset
- {
- get { return asset; }
- }
- public float LastUsedTime
- {
- get { return lastUsedTime; }
- set
- {
- lastUsedTime = value;
- }
- }
- public ResourceInfo(string path_, string name_, Object asset_)
- {
- path = path_;
- name = name_;
- asset = asset_;
- lastUsedTime = Time.time;
- }
- public Object Reuse()
- {
- lastUsedTime = Time.time;
- return asset;
- }
- public void Dispose()
- {
- asset = null;
- }
- }
- public class LoadingResourceInfo
- {
- System.Type delegateType;
- public System.Type DelegateType
- {
- get { return delegateType; }
- }
- System.Delegate callback;
- public System.Delegate Callback
- {
- get { return callback; }
- }
- string assetPath;
- public string AssetPath
- {
- get { return assetPath; }
- }
- string assetName;
- public string AssetName
- {
- get { return assetName; }
- }
- string[] assetNames;
- public string[] AssetNames
- {
- get { return assetNames; }
- }
- long mSeqId = 0;
- public long SeqId
- {
- get { return mSeqId; }
- }
- bool mReturnSeqId = false;
- public bool ReturnSeqId
- {
- get { return mReturnSeqId; }
- }
- int mLoadNum = 0;
- public int LoadCBNum
- {
- get { return mLoadNum; }
- set { mLoadNum = value; }
- }
- public object[] userDatas;
- public LoadingResourceInfo(long seqId, string path_, System.Delegate callback_, System.Type delegateType_, bool returnSeq,params string[] assetNames_)
- {
- mSeqId = seqId;
- assetPath = path_;
- callback = callback_;
- assetNames = assetNames_;
- delegateType = delegateType_;
- mReturnSeqId = returnSeq;
- }
- public void Dispose()
- {
- callback = null;
- assetNames = null;
- }
- }
- public class ResourceMgr : Singleton<ResourceMgr>
- {
- long loadingSeqId = 0;
- //对象池
- Dictionary<string, Dictionary<string, GameObjectPool<GameObject>> > assetsPool = new Dictionary<string, Dictionary<string, GameObjectPool<GameObject>>>();
- //已加载的资源列表
- List<ResourceInfo> loadedResources = new List<ResourceInfo>();
- //正在加载的资源
- List<LoadingResourceInfo> loadingResources = new List<LoadingResourceInfo>();
- const float check_internal = 60;
- const float cache_Time = 300;
- float lastCheckTime = 0;
- [NoToLua]
- public override void Init()
- {
- lastCheckTime = Time.time;
- }
- [NoToLua]
- public override void UnInit()
- {
- ClearAllResource();
- }
- private GameObject mPoolGo = null;
- public Transform PoolNode
- {
- get
- {
- if (mPoolGo == null)
- {
- mPoolGo = new GameObject("PoolRoot");
- if (GameMgr.Instance != null) {
- mPoolGo.transform.SetParent(GameMgr.Instance.transform);
- }
- mPoolGo.SetActive(false);
- }
- return mPoolGo.transform;
- }
- }
- #region outer_methods
- //已经在的资源个数
- [NoToLua]
- public int LoadedResourceCnt
- {
- get { return loadedResources.Count; }
- }
- //正在加载的资源个数
- [NoToLua]
- public int LoadingResourceCnt
- {
- get { return loadingResources.Count; }
- }
- [NoToLua]
- public GameObject GetGOFromResources(string assetName)
- {
- GameObject prefab = LoadObjFromResources<GameObject>(assetName);
- if (prefab == null) return null;
- //DebugHelper.LogError("AssetName " + assetName);
- return GameObject.Instantiate(prefab);
- }
- [NoToLua]
- public T LoadObjFromResources<T>(string assetName) where T : UnityEngine.Object
- {
- return AssetsMgr.Instance.GetAssetFromResources<T>(assetName);
- }
- [NoToLua]
- public void LoadLuaAsset(ResourceLoadCallback<List<TextAsset>> cb, string pathName)
- {
- loadingSeqId++;
- LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(List<TextAsset>), false, null);
- loadingResources.Add(lri);
- lri.LoadCBNum = AssetsMgr.Instance.GetAsset<List<TextAsset>>(OnLoadDirAssetallback, ELoadType.OTHER, loadingSeqId, pathName);
- }
- [NoToLua]
- public void LoadDirAsset<T>(ResourceLoadCallback<T> cb, string pathName, ELoadType type = ELoadType.OTHER)
- {
- if (string.IsNullOrEmpty(pathName)) return;
- if (ExistAssetPath(pathName))
- {
- FinishResourceLoad(cb, typeof(T), pathName, null,false,0,null);
- return;
- }
- loadingSeqId++;
- LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(T), false,null);
- loadingResources.Add(lri);
- lri.LoadCBNum = AssetsMgr.Instance.GetAsset<T>(OnLoadDirAssetallback, type, loadingSeqId, pathName);
- }
- #region methods_for_lua
- public long LoadAssetSprite(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<Sprite> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, null,assetNames);
- }
- public long LoadAssetSprites(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<List<Sprite>> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, null, assetNames);
- }
- public long LoadAssetSpriteWithUserData(string pathName, ELoadType type,object[] userDatas, ResourceLoadCallbackWithSeqId<Sprite> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, userDatas,assetNames);
- }
- public long LoadAssetGameObject(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<GameObject> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, null,assetNames);
- }
- public long LoadAssetGameObjectWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<GameObject> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type,userDatas ,assetNames);
- }
- public long LoadAssetGameObjects(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<List<GameObject>> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type,null, assetNames);
- }
- public long LoadAssetGameObjectsWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<List<GameObject>> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, userDatas, assetNames);
- }
- public long LoadAssetRuntimeAnimatorControllers(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<List<RuntimeAnimatorController>> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type,null, assetNames);
- }
- public long LoadAssetRuntimeAnimatorControllersWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<List<RuntimeAnimatorController>> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, userDatas,assetNames);
- }
- public long LoadAssetMaterial(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<Material> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, null,assetNames);
- }
- public long LoadAssetMaterialWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<Material> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, userDatas, assetNames);
- }
- public long LoadAssetTexture(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<Texture2D> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, null, assetNames);
- }
- public long LoadAssetTextureWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<Texture2D> cb, params string[] assetNames)
- {
- return LoadAsset(cb, pathName, type, userDatas, assetNames);
- }
- public long LoadAsset<T>(ResourceLoadCallbackWithSeqId<T> cb, string pathName, ELoadType type, object[] userDatas, params string[] assetNames)
- {
- return Inner_LoadAsset(cb, pathName, assetNames, type, userDatas);
- }
- public Sprite FindSpriteByPathAndName(string pathName, string assetName)
- {
- return FindAssetByPathAndName<Sprite>(pathName, assetName);
- }
- #endregion
- public long LoadAsset<T>(ResourceLoadCallback<T> cb, string pathName, ELoadType type, params string[] assetNames)
- {
- return Inner_LoadAsset(cb, pathName, assetNames, type);
- }
- public long LoadAsset<T>(ResourceLoadCallback<T> cb, string pathName, params string[] assetNames)
- {
- return Inner_LoadAsset(cb, pathName, assetNames, ELoadType.OTHER);
- }
- public Object[] LoadABSync(string pathName, params string[] assetNames)
- {
- return AssetsMgr.Instance.LoadABSync(pathName, assetNames);
- }
- public T LoadAssetSync<T>(string assetPath, string assetName) where T : UnityEngine.Object
- {
- T ret = null;
- Object obj = FindAssetByPathAndName(assetPath, assetName);
- if (obj == null)
- {
- ret = AssetsMgr.Instance.LoadAssetSync<T>(assetPath, assetName);
- ResourceInfo ri = new ResourceInfo(assetPath, assetName, ret);
- loadedResources.Add(ri);
- }
- else
- {
- ret = obj as T;
- }
- return ret;
- }
- /// <summary>
- /// 从对象池中获取对象
- /// </summary>
- /// <param name="assetsPath"></param>
- /// <param name="assetsName"></param>
- /// <param name="action"></param>
- public long GetGOFromPool(string assetsPath, string assetsName, System.Action<GameObject> action = null)
- {
- if (action == null || string.IsNullOrEmpty(assetsName)) return 0;
- if(!string.IsNullOrEmpty(assetsPath))
- {
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
- if (assetsPool.TryGetValue(assetsPath, out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- mDirGoPool.TryGetValue(assetsName, out curPool);
- if (curPool == null)
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetsName, curPool);
- }
- GameObject go = curPool.Spawn();
- if (go != null)
- {
- action(go);
- return 0;
- }
- }
- }
- ResourceLoadCallback<GameObject> callBack = (prefab, path_, assetName_) =>
- {
- if (null == prefab)
- {
- if (action != null)
- action(null);
- return;
- }
- if (action != null)
- {
- GameObject goInst = GameObject.Instantiate(prefab);
- goInst.transform.localPosition = Vector3.zero;
- goInst.transform.localScale = Vector3.one;
- goInst.name = assetsName;
- action(goInst);
- }
- };
- return LoadAsset<GameObject>(callBack, assetsPath, assetsName);
- }
-
- public long GetGoesFromPool(string assetsPath,string[] assetsNames, ResourceLoadCallback<List<GameObject>> cb)
- {
- if (cb == null || string.IsNullOrEmpty(assetsPath) || assetsNames == null || assetsNames.Length == 0) return 0;
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
- if (assetsPool.TryGetValue(assetsPath, out mDirGoPool))
- {
- List<GameObject> goList = new List<GameObject>();
- int cnt = 0;
- GameObjectPool<GameObject> curPool = null;
- for(int idx =0; idx < assetsNames.Length;idx++)
- {
- mDirGoPool.TryGetValue(assetsNames[idx], out curPool);
- if (curPool == null)
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetsNames[idx], curPool);
- }
- GameObject go = curPool.Spawn();
- if (go != null)
- {
- goList.Add(go);
- }
- else
- {
- cnt = idx;
- break;
- }
- }
- if(goList.Count == assetsNames.Length)
- {
- cb(goList, assetsPath, assetsNames);
- return 0;
- }
- else
- {
- for(int idx =0; idx <cnt;idx++)
- {
- RecycleGO(assetsPath, assetsNames[idx],goList[idx]);
- }
- goList.Clear();
- goList = null;
- }
- }
- ResourceLoadCallback<List<GameObject>> callBack = (prefabs, path_, assetName_) =>
- {
- if (null == prefabs)
- {
- if (cb != null)
- cb(null, path_, assetName_);
- return;
- }
- if (cb != null)
- {
- List<GameObject> goInstList = new List<GameObject>();
- for(int idx =0;idx < prefabs.Count;idx++)
- {
- GameObject goInst = GameObject.Instantiate(prefabs[idx]);
- goInst.transform.localPosition = Vector3.zero;
- goInst.transform.localScale = Vector3.one;
- goInst.name = assetName_[idx];
- goInstList.Add(goInst);
- }
- cb(goInstList, path_, assetName_);
- }
- };
- return LoadAsset<List<GameObject>>(callBack, assetsPath, assetsNames);
- }
- public GameObject GetGoFromPool(string assetPath, string assetName)
- {
- if (string.IsNullOrEmpty(assetName))
- return null;
- GameObject go = null;
- if (!string.IsNullOrEmpty(assetPath))
- {
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
- if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- mDirGoPool.TryGetValue(assetName, out curPool);
- if (curPool == null)
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- }
- go = curPool.Spawn();
- if (go != null)
- {
- return go;
- }
- }
- }
-
-
- GameObject prefab = (GameObject)FindAssetByPathAndName(assetPath, assetName);
- if(prefab == null)
- {
- prefab = ResourceMgr.Instance.LoadAssetSync<GameObject>(assetPath, assetName);
- }
- if(prefab != null)
- {
- go = GameObject.Instantiate(prefab);
- }
- return go;
- }
- public GameObject GetUIGoFromPool(string assetPath, string assetName)
- {
- if (string.IsNullOrEmpty(assetName))
- return null;
- if (!string.IsNullOrEmpty(assetPath))
- {
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
- if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- mDirGoPool.TryGetValue(assetName, out curPool);
- if (curPool == null)
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- }
- GameObject go = curPool.Spawn();
- if (go != null)
- {
- return go;
- }
- }
- }
- return null;
- }
- public void SetGoPoolMaxCacheNum(string assetPath, string assetName,int num)
- {
- if (string.IsNullOrEmpty(assetPath) || string.IsNullOrEmpty(assetName)) return;
-
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
- if(assetsPool.TryGetValue(assetPath,out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- mDirGoPool.TryGetValue(assetName, out curPool);
- if (curPool == null)
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- }
- curPool.SetMaxCacheNum(num);
- }
- else
- {
- mDirGoPool = new Dictionary<string, GameObjectPool<GameObject>>();
- GameObjectPool<GameObject> curPool = new GameObjectPool<GameObject>();
- curPool.SetMaxCacheNum(num);
- mDirGoPool.Add(assetName, curPool);
- assetsPool.Add(assetPath, mDirGoPool);
- }
- }
- public Object GetAsset(string assetPath,string assetName)
- {
- if(string.IsNullOrEmpty(assetName))
- {
- return null;
- }
- return FindAssetByPathAndName(assetPath, assetName);
- }
- public void RecycleUIGO(string assetPath, string assetName, GameObject go)
- {
- if (go == null) return;
- if (string.IsNullOrEmpty(assetName) || string.IsNullOrEmpty(assetPath))
- {
- GameObject.Destroy(go);
- go = null;
- return;
- }
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool = null;
- if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- if (mDirGoPool.TryGetValue(assetName, out curPool))
- {
- go.transform.SetParent(UIMgr.Instance.UIRootTrans);
- //CommonUtil.SetGameObjectLayer(go, "Hide");
- go.SetActive(false);
- curPool.Recycle(go);
- return;
- }
- else
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- go.transform.SetParent(UIMgr.Instance.UIRootTrans);
- //CommonUtil.SetGameObjectLayer(go, "Hide");
- go.SetActive(false);
- curPool.Recycle(go);
- }
- }
- else
- {
- mDirGoPool = new Dictionary<string, GameObjectPool<GameObject>>();
- GameObjectPool<GameObject> curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- assetsPool.Add(assetPath, mDirGoPool);
- go.transform.SetParent(UIMgr.Instance.UIRootTrans);
- //CommonUtil.SetGameObjectLayer(go, "Hide");
- go.SetActive(false);
- curPool.Recycle(go);
- }
- }
- public void RecycleGO(string assetPath,string assetName, GameObject go)
- {
- if (go == null) return;
- if (string.IsNullOrEmpty(assetName) || string.IsNullOrEmpty(assetPath))
- {
- GameObject.Destroy(go);
- go = null;
- return;
- }
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool = null;
- if(assetsPool.TryGetValue(assetPath, out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- if (mDirGoPool.TryGetValue(assetName, out curPool))
- {
- go.transform.SetParent(PoolNode);
- curPool.Recycle(go);
- return;
- }
- else
- {
- curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- go.transform.SetParent(PoolNode);
- curPool.Recycle(go);
- }
- }
- else
- {
- mDirGoPool = new Dictionary<string, GameObjectPool<GameObject>>();
- GameObjectPool<GameObject> curPool = new GameObjectPool<GameObject>();
- mDirGoPool.Add(assetName, curPool);
- assetsPool.Add(assetPath, mDirGoPool);
- go.transform.SetParent(PoolNode);
- curPool.Recycle(go);
- }
- }
- public void UnloadAssetBySeqId(long seqId)
- {
- RemoveLoadingCb(seqId);
- }
- [NoToLua]
- public void CleanGOPool()
- {
- foreach (var p in assetsPool)
- {
- Dictionary<string, GameObjectPool<GameObject>> poolDic = p.Value;
- foreach(var list in poolDic)
- {
- GameObjectPool<GameObject> goPool = list.Value;
- goPool.Dispose();
- }
- }
- assetsPool.Clear();
- }
- public Shader FindShader(string shaderName,string shaderPath)
- {
- return AssetsMgr.Instance.FindShader(shaderName, shaderPath);
- }
- #endregion
- [NoToLua]
- public void Update()
- {
- //if (LoadedResourceCnt == 0) return;
- //float curTime = Time.time;
- //if (curTime - lastCheckTime >= check_internal)
- //{
- // ClearLongTimeUnusedResource(curTime);
- // lastCheckTime = curTime;
- //}
- }
- #region inner_methods
- void RemoveLoadingCb(long seqId)
- {
- LoadingResourceInfo lri = FindLoadingResourceInfoBySeqId(seqId);
- if (lri != null)
- {
- AssetsMgr.Instance.CancelLoadAsset(seqId);
- lri.Dispose();
- loadingResources.Remove(lri);
- }
- }
- LoadingResourceInfo FindLoadingResourceInfoBySeqId(long seqId)
- {
- for (int idx = 0; idx < loadingResources.Count; idx++)
- {
- LoadingResourceInfo lri = loadingResources[idx];
- if (lri.SeqId == seqId) return lri;
- }
- return null;
- }
- public bool HasAsset(string path,string assetName)
- {
- if (string.IsNullOrEmpty(assetName)) return false;
- Object obj = FindAssetByPathAndName(path, assetName);
- return obj != null;
- }
- bool ExistAssets(string path, string[] assetNames)
- {
- if (assetNames == null || assetNames.Length == 0) return false;
- bool find = false;
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- Object obj = FindAssetByPathAndName(path, assetNames[idx]);
- if (obj == null) return false;
- find = true;
- }
- return find;
- }
- bool ExistAssetPath(string path)
- {
- for (int idx = 0; idx < loadedResources.Count; idx++)
- {
- ResourceInfo ri = loadedResources[idx];
- if (ri.Path == path)
- {
- return true;
- }
- }
- return false;
- }
- Object FindAssetByPathAndName(string pathName, string assetName)
- {
- for (int idx = 0; idx < loadedResources.Count; idx++)
- {
- ResourceInfo ri = loadedResources[idx];
- if (ri.Path == pathName && ri.Name == assetName)
- {
- return ri.Asset;
- }
- }
- return null;
- }
- T FindAssetByPathAndName<T>(string pathName, string assetName) where T : Object
- {
- for (int idx = 0; idx < loadedResources.Count; idx++)
- {
- ResourceInfo ri = loadedResources[idx];
- if (ri.Path == pathName && ri.Name == assetName)
- {
- T ret = ri.Reuse() as T;
- return ret;
- }
- }
- return null;
- }
- long Inner_LoadAsset<T>(ResourceLoadCallbackWithSeqId<T> cb, string pathName, string[] assetNames, ELoadType type, object[] userDatas)
- {
- if (string.IsNullOrEmpty(pathName) && (assetNames == null || assetNames.Length == 0)) return 0;
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- if (assetNames[idx] == "Null" || string.IsNullOrEmpty(assetNames[idx]))
- {
- DebugHelper.LogError("加载的资源存在Null,请检查配置");
- return 0;
- }
- }
- if (assetNames == null || assetNames.Length == 0)
- {
- if (cb != null)
- cb.DynamicInvoke(null, 0, pathName, userDatas, assetNames);
- return 0;
- }
- if (ExistAssets(pathName, assetNames))
- {
- FinishResourceLoad(cb, typeof(T), pathName, assetNames,true,0, userDatas);
- return 0;
- }
- loadingSeqId++;
- LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(T), true,assetNames);
- lri.userDatas = userDatas;
- loadingResources.Add(lri);
- lri.LoadCBNum = AssetsMgr.Instance.GetAsset<T>(OnLoadAssetallback, type, loadingSeqId, pathName, assetNames);
- if (lri.LoadCBNum == 0)
- {
- if (cb != null)
- cb.DynamicInvoke(null, loadingSeqId,pathName, userDatas, assetNames);
- RemoveLoadingCb(loadingSeqId);
- }
- return loadingSeqId;
- }
- private long Inner_LoadAsset<T>(ResourceLoadCallback<T> cb, string pathName, string[] assetNames, ELoadType type)
- {
- if (string.IsNullOrEmpty(pathName) && (assetNames == null || assetNames.Length == 0)) return 0;
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- if (assetNames[idx] == "Null" || string.IsNullOrEmpty(assetNames[idx]))
- {
- DebugHelper.LogError("加载的资源存在Null,请检查配置");
- return 0;
- }
- }
- if (assetNames == null || assetNames.Length == 0)
- {
- if (cb != null)
- cb.DynamicInvoke(null, pathName, assetNames);
- return 0;
- }
-
- if (ExistAssets(pathName, assetNames))
- {
- FinishResourceLoad(cb, typeof(T), pathName, assetNames,false,0,null);
- return 0;
- }
- loadingSeqId++;
- LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(T), false,assetNames);
- loadingResources.Add(lri);
- lri.LoadCBNum = AssetsMgr.Instance.GetAsset<T>(OnLoadAssetallback, type, loadingSeqId, pathName, assetNames);
- if (lri.LoadCBNum == 0)
- {
- if (cb != null)
- cb.DynamicInvoke(null, pathName, assetNames);
- RemoveLoadingCb(loadingSeqId);
- }
- return loadingSeqId;
- }
- void OnLoadDirAssetallback<T>(T result, long seqId, string assetPath, params string[] assetNames)
- {
- LoadingResourceInfo lri = FindLoadingResourceInfoBySeqId(seqId);
- if (lri != null)
- {
- System.Delegate cb = lri.Callback;
- if(cb!=null)
- cb.DynamicInvoke(result, assetPath, assetNames);
- RemoveLoadingCb(seqId);
- }
- else
- {
- DebugHelper.Log("Load " + seqId + " Failed LoadingCnt:" + LoadingResourceCnt);
- }
- }
- void OnLoadAssetallback<T>(T result, long seqId, string assetPath, params string[] assetNames)
- {
- var t = typeof(T);
- if (t.Equals(typeof(List<GameObject>)))
- {
- List<GameObject> objList = result as List<GameObject>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- string objName = objList[idx] != null ? objList[idx].name : null;
- ResourceInfo ri = new ResourceInfo(assetPath, objName, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<Texture2D>)))
- {
- List<Texture2D> objList = result as List<Texture2D>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<Sprite>)))
- {
- List<Sprite> objList = result as List<Sprite>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<TextAsset>)))
- {
- List<TextAsset> objList = result as List<TextAsset>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<PlayableAsset>)))
- {
- List<PlayableAsset> objList = result as List<PlayableAsset>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<AudioClip>)))
- {
- List<AudioClip> objList = result as List<AudioClip>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<VideoClip>)))
- {
- List<VideoClip> objList = result as List<VideoClip>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<AnimationClip>)))
- {
- List<AnimationClip> objList = result as List<AnimationClip>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(List<RuntimeAnimatorController>)))
- {
- List<RuntimeAnimatorController> objList = result as List<RuntimeAnimatorController>;
- if (assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
- loadedResources.Add(ri);
- }
- }
- else
- {
- for (int idx = 0; idx < objList.Count; idx++)
- {
- ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
- loadedResources.Add(ri);
- }
- }
- }
- else if (t.Equals(typeof(ScriptableObject)) ||
- t.Equals(typeof(GameObject)) ||
- t.Equals(typeof(AudioClip)) ||
- t.Equals(typeof(VideoClip)) ||
- t.Equals(typeof(Texture2D)) ||
- t.Equals(typeof(Sprite)) ||
- t.Equals(typeof(Material)) ||
- t.Equals(typeof(AnimationClip)) ||
- t.Equals(typeof(RuntimeAnimatorController)))
- {
- Object obj = result as Object;
- ResourceInfo ri = new ResourceInfo(assetPath, assetNames[0], obj);
- loadedResources.Add(ri);
- }
- LoadingResourceInfo lri = FindLoadingResourceInfoBySeqId(seqId);
- if (lri != null)
- {
- lri.LoadCBNum = lri.LoadCBNum - 1;
- if(lri.LoadCBNum <= 0)
- {
- System.Delegate cb = lri.Callback;
- FinishResourceLoad(cb, lri.DelegateType, lri.AssetPath, lri.AssetNames, lri.ReturnSeqId, lri.SeqId,lri.userDatas);
- RemoveLoadingCb(seqId);
- }
- }
- else
- {
- DebugHelper.Log("Load " + seqId + " Failed LoadingCnt:" + LoadingResourceCnt);
- }
- }
- void FinishResourceLoad(System.Delegate cb, System.Type delegateType, string pathName, string[] assetNames,bool returnSeq,long seqId,object[] userdatas)
- {
- if (cb == null) return;
- if (delegateType.Equals(typeof(List<GameObject>)))
- {
- FinishLoad<GameObject>(cb, pathName, assetNames, false, returnSeq,seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<Texture2D>)))
- {
- FinishLoad<Texture2D>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<Sprite>)))
- {
- FinishLoad<Sprite>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<TextAsset>)))
- {
- FinishLoad<TextAsset>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<PlayableAsset>)))
- {
- FinishLoad<PlayableAsset>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<AudioClip>)))
- {
- FinishLoad<AudioClip>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<VideoClip>)))
- {
- FinishLoad<VideoClip>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<AnimationClip>)))
- {
- FinishLoad<AnimationClip>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(List<RuntimeAnimatorController>)))
- {
- FinishLoad<RuntimeAnimatorController>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(GameObject)))
- {
- FinishLoad<GameObject>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(Texture2D)))
- {
- FinishLoad<Texture2D>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(Sprite)))
- {
- FinishLoad<Sprite>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(TextAsset)))
- {
- FinishLoad<TextAsset>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(PlayableAsset)))
- {
- FinishLoad<PlayableAsset>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(AudioClip)))
- {
- FinishLoad<AudioClip>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(VideoClip)))
- {
- FinishLoad<VideoClip>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(AnimationClip)))
- {
- FinishLoad<AnimationClip>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(Sprite)))
- {
- FinishLoad<Sprite>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(Material)))
- {
- FinishLoad<Material>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- else if (delegateType.Equals(typeof(RuntimeAnimatorController)))
- {
- FinishLoad<RuntimeAnimatorController>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
- }
- }
- void FinishLoad<T>(System.Delegate cb, string pathName, string[] assetNames, bool single, bool returnSeq, long seqId, object[] userdatas) where T : Object
- {
- if (single)
- {
- T go = FindAssetByPathAndName<T>(pathName, assetNames[0]);
- try
- {
- if (returnSeq)
- {
- cb.DynamicInvoke(go, seqId, pathName, userdatas,assetNames);
- }
- else
- {
-
- cb.DynamicInvoke(go, pathName, assetNames);
- }
- }
- catch(System.Exception e)
- {
- DebugHelper.LogError(e.StackTrace);
- }
-
- }
- else
- {
- List<T> assetList = new List<T>();
- if (assetNames != null && assetNames.Length > 0)
- {
- for (int idx = 0; idx < assetNames.Length; idx++)
- {
- assetList.Add(FindAssetByPathAndName<T>(pathName, assetNames[idx]));
- }
- }
- else
- {
- for (int idx = 0; idx < loadedResources.Count; idx++)
- {
- ResourceInfo ri = loadedResources[idx];
- if (ri.Path == pathName)
- {
- T asset = ri.Reuse() as T;
- assetList.Add(asset);
- }
- }
- }
- if(returnSeq)
- {
- cb.DynamicInvoke(assetList,seqId, pathName, userdatas, assetNames);
- }
- else
- {
- cb.DynamicInvoke(assetList, pathName, assetNames);
- }
- }
- ClearNullResource();
- }
- void ClearNullResource()
- {
- for (int idx = loadedResources.Count - 1; idx >= 0; idx--)
- {
- ResourceInfo ri = loadedResources[idx];
- if (ri.Asset == null)
- {
- loadedResources.RemoveAt(idx);
- }
- }
- }
- public void RemoveLoadedResource(string path,string assetName)
- {
- for (int idx = loadedResources.Count-1; idx >= 0; idx--)
- {
- var ri = loadedResources[idx];
- if(ri.Path == path && ri.Name == assetName)
- {
- ri.Dispose();
- loadedResources.RemoveAt(idx);
- }
- }
- }
- List<string> mResidentDirList = new List<string>();
- List<string> mResidentResList = new List<string>();
- public void ReadResidentResCfg()
- {
- mResidentDirList.Clear();
- mResidentResList.Clear();
- Dictionary<string, Dictionary<string, string>> tab = ConfigMgr.Instance.getTable(Config.ResidentResCfg);
- if (tab == null) return;
- foreach(var p in tab)
- {
- var key = p.Key;
- var dic = p.Value;
- int kVal = 0;
- if (int.TryParse(key, out kVal))
- {
- string resName = dic["name"];
- int type = 0;
- if (dic.ContainsKey("type"))
- {
- int.TryParse(dic["type"], out type);
- }
- if (!string.IsNullOrEmpty(resName) && type > 0)
- {
- if (type == 1)
- {
- if (!mResidentDirList.Contains(resName))
- mResidentDirList.Add(resName);
- }
- else if (type == 2)
- {
- if (!mResidentResList.Contains(resName))
- {
- mResidentResList.Add(resName);
- }
- }
- }
- }
- }
- }
- public bool IsResidentRes(string assetPath,string assetName)
- {
- string path = assetPath + "/" + assetName;
- return IsResidentRes(path);
- }
- public bool IsResidentRes(string assetFullPath)
- {
- for (int idx = 0; idx < mResidentResList.Count; idx++)
- {
- if (mResidentResList[idx] == assetFullPath) return true;
- }
- string pathDir;
- FileUtils.ExtractParent(assetFullPath, out pathDir);
- if (!string.IsNullOrEmpty(pathDir))
- {
- for (int idx = 0; idx < mResidentDirList.Count; idx++)
- {
- if (mResidentDirList[idx] == pathDir) return true;
- }
- }
- return false;
- }
- public void ClearAllResource()
- {
- for(int idx = loadedResources.Count -1; idx>=0; idx--)
- {
- ResourceInfo ri = loadedResources[idx];
- if (IsResidentRes(ri.Path, ri.Name))
- {
- continue;
- }
- ri.Dispose();
- loadedResources.RemoveAt(idx);
- }
- CleanGOPool();
- }
- void ClearLongTimeUnusedResource(float curTime)
- {
- for (int idx = loadedResources.Count - 1; idx >= 0; idx--)
- {
- ResourceInfo ri = loadedResources[idx];
- float passedTime = curTime - ri.LastUsedTime;
- if (passedTime >= cache_Time && !IsResidentRes(ri.Path,ri.Name) && HasGoInPool(ri.Path,ri.Name))
- {
- ri.Dispose();
- loadedResources.RemoveAt(idx);
- }
- }
- }
- bool HasGoInPool(string assetPath,string assetName)
- {
- if (string.IsNullOrEmpty(assetPath)) return false;
-
- if (assetsPool == null) return false;
- Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
- if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
- {
- GameObjectPool<GameObject> curPool = null;
- mDirGoPool.TryGetValue(assetName, out curPool);
- if (curPool != null && curPool.Count > 0)
- {
- return true;
- }
- }
- return false;
- }
- #endregion
- }
|