/// <summary> /// 同步加载需要实例化的资源 /// </summary> /// <param name="path"></param> /// <param name="bClear"></param> /// <returns></returns> public GameObject InstantiateObject(string path, bool setSceneObj = false, bool bClear = true) { uint crc = CRC32.GetCRC32(path); ResourceObj resource = GetObjectFromPool(crc); if (resource == null) { resource = m_ResourceObjClassPool.Spawn(true); resource.m_Crc = crc; resource.m_bClear = bClear; resource = ResourceManager.Instance.LoadResource(path, resource); if (resource.m_ResItem.m_Obj != null) { resource.m_CloneObj = GameObject.Instantiate(resource.m_ResItem.m_Obj) as GameObject; } } if (setSceneObj) { resource.m_CloneObj.transform.SetParent(SceneTrs); } int tempID = resource.m_CloneObj.GetInstanceID(); if (!m_ResourceObjDic.ContainsKey(tempID)) { m_ResourceObjDic.Add(tempID, resource); } return(resource.m_CloneObj); }
/// <summary> /// 取消异步加载资源 /// </summary> /// <returns></returns> public bool CancleLoad(ResourceObj res) { AsyncLoadResParam para = null; if (m_LoadingAssetDic.TryGetValue(res.m_Crc, out para) && m_LoadingAssetList[(int)para.m_Priority].Contains(para)) { for (int i = para.m_CallBackList.Count; i > 0; i--) { AsyncCallBack tempCallBack = para.m_CallBackList[i]; if (tempCallBack != null && res == tempCallBack.m_ResObj) { tempCallBack.Reset(); m_AsyncCallBackPool.Recycle(tempCallBack); para.m_CallBackList.Remove(tempCallBack); } if (para.m_CallBackList.Count <= 0) { para.Reset(); m_LoadingAssetList[(int)para.m_Priority].Remove(para); m_AsyncLoadResParamPool.Recycle(para); m_LoadingAssetDic.Remove(res.m_Crc); return(true); } } } return(false); }
/// <summary> /// 从对象池中取出对象 /// </summary> /// <param name="crc"></param> /// <returns></returns> protected ResourceObj GetObjectFromPool(uint crc) { List <ResourceObj> st = null; if (m_ObjectPoolDic.TryGetValue(crc, out st) && st != null && st.Count > 0) { ResourceManager.Instance.IncreaseResourceRef(crc); ResourceObj resObj = st[0]; st.RemoveAt(0); GameObject obj = resObj.m_CloneObj; if (!System.Object.ReferenceEquals(obj, null)) { resObj.m_Already = false; if (!System.Object.ReferenceEquals(resObj.m_OfflineData, null)) { resObj.m_OfflineData.ResetProp(); } #if UNITY_EDITOR if (obj.name.EndsWith("(Recycle)")) { obj.name = obj.name.Replace("(Recycle)", ""); } #endif } return(resObj); } return(null); }
/// <summary> /// 同步加载资源,针对给ObjectManager接口 /// </summary> /// <param name="path"></param> /// <param name="resObj"></param> /// <returns></returns> public ResourceObj LoadResource(string path, ResourceObj resObj) { if (resObj == null) { return(null); } uint crc = resObj.m_Crc == 0 ? CRC32.GetCRC32(path) : resObj.m_Crc; ResourceItem item = GetCacheResourrceItem(crc); if (item != null) { resObj.m_ResItem = item; return(resObj); } Object obj = null; #if UNITY_EDITOR if (!m_LoadFromAssetBundle) { item = AssetBundleManager.Instance.FindResourceItem(crc); if (item != null && item.m_Obj != null) { obj = item.m_Obj as Object; } else { if (item == null) { item = new ResourceItem { m_Crc = crc } } ; obj = LoadAssetByEditor <Object>(path); } } #endif if (obj == null) { item = AssetBundleManager.Instance.LoatResourceAssetBundle(crc); if (item.m_Obj != null) { obj = item.m_Obj as Object; } else { obj = item.m_AssetBundle.LoadAsset <Object>(item.m_ABName); } } CacheResource(path, ref item, crc, obj); resObj.m_ResItem = item; item.m_Clear = resObj.m_bClear; return(resObj); }
/// <summary> /// 清除某个资源在对象池中所有的对象 /// </summary> public void ClearPoolObject(uint crc) { List <ResourceObj> st = null; if (!m_ObjectPoolDic.TryGetValue(crc, out st) || st == null) { return; } for (int i = st.Count - 1; i >= 0; i--) { ResourceObj resourceObj = st[i]; if (resourceObj.m_bClear) { st.Remove(resourceObj); int tempID = resourceObj.m_CloneObj.GetInstanceID(); GameObject.Destroy(resourceObj.m_CloneObj); resourceObj.Reset(); m_ResourceObjDic.Remove(tempID); m_ResourceObjClassPool.Recycle(resourceObj); } } if (st.Count <= 0) { m_ObjectPoolDic.Remove(crc); } }
public void Reset() { m_DealObjectFinish = null; m_DealFinish = null; m_Param1 = null; m_Param2 = null; m_Param3 = null; m_ResObj = null; }
/// <summary> /// 取消异步加载 /// </summary> /// <param name="guid"></param> public void CancleLoad(long guid) { ResourceObj resOjbObj = null; if (m_AsyncResObj.TryGetValue(guid, out resOjbObj) && ResourceManager.Instance.CancleLoad(resOjbObj)) { m_AsyncResObj.Remove(guid); resOjbObj.Reset(); m_ResourceObjClassPool.Recycle(resOjbObj); } }
/// <summary> /// 根据实例化对象直接获取离线数据 /// </summary> /// <param name="obj"></param> /// <returns></returns> public OfflineData FindOfflineData(GameObject obj) { OfflineData data = null; ResourceObj resourceObj = null; m_ResourceObjDic.TryGetValue(obj.GetInstanceID(), out resourceObj); if (resourceObj != null) { data = resourceObj.m_OfflineData; } return(data); }
/// <summary> /// 异步对象加载 /// </summary> public long InstantiateObjectAsync(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool setSceneObject = false, object param1 = null, object param2 = null, object param3 = null, bool bClear = true) { if (string.IsNullOrEmpty(path)) { return(0); } uint crc = CRC32.GetCRC32(path); ResourceObj resObj = GetObjectFromPool(crc); if (resObj != null) { if (setSceneObject) { resObj.m_CloneObj.transform.SetParent(SceneTrs, false); } if (dealFinish != null) { dealFinish(path, resObj.m_CloneObj, param1, param2, param3); } return(resObj.m_Guid); } long guid = ResourceManager.Instance.CreateGUID(); resObj = m_ResourceObjClassPool.Spawn(true); resObj.m_Crc = crc; resObj.m_setSceneParent = setSceneObject; resObj.m_bClear = bClear; resObj.m_DealFnish = dealFinish; resObj.m_param1 = param1; resObj.m_param2 = param2; resObj.m_param3 = param3; //调用ResourceManager异步加载接口 ResourceManager.Instance.AsyncLoadResource(path, resObj, OnLoadResourceObjFinish, priority); return(guid); }
/// <summary> /// 资源加载完成回调 /// </summary> /// <param name="path">路径</param> /// <param name="resObj">中间类</param> void OnLoadResourceObjFinish(string path, ResourceObj resObj, object param1 = null, object param2 = null, object param3 = null) { if (resObj == null) { return; } if (resObj.m_ResItem.m_Obj == null) { #if UNITY_EDITOR Debug.LogError("异步加载资源为空:" + path); #endif } else { resObj.m_CloneObj = Instantiate(resObj.m_ResItem.m_Obj) as GameObject; resObj.m_OfflineData = resObj.m_CloneObj.GetComponent <OfflineData>(); } //加载完成就从正在加载的异步中移除 if (m_AsyncResObj.ContainsKey(resObj.m_Guid)) { m_AsyncResObj.Remove(resObj.m_Guid); } if (resObj.m_CloneObj != null && resObj.m_setSceneParent) { resObj.m_CloneObj.transform.SetParent(SceneTrs, false); } if (resObj.m_DealFnish != null) { int tempID = resObj.m_CloneObj.GetInstanceID(); if (!m_ResourceObjDic.ContainsKey(tempID)) { m_ResourceObjDic.Add(tempID, resObj); } resObj.m_DealFnish(path, resObj.m_CloneObj, resObj.m_param1, resObj.m_param2, resObj.m_param3); } }
/// <summary> /// 释放需要实例化的资源,根据 /// </summary> /// <param name="resobj"></param> /// <param name="destoryObj"></param> /// <returns></returns> public bool ReleaseResource(ResourceObj resobj, bool destoryObj = false) { if (resobj == null) { return(false); } ResourceItem item = null; if (!AssetDic.TryGetValue(resobj.m_Crc, out item) || null == item) { Debug.LogError($"AssetDic里不存在资源: {resobj.m_CloneObj.name},可能多次释放"); } GameObject.Destroy(resobj.m_CloneObj); item.RefCount--; DestoryResourceItem(item, destoryObj); return(true); }
/// <summary> /// 针对ObjectManager的异步加载接口 /// </summary> public void AsyncLoadResource(string path, ResourceObj resObj, OnAsyncFinish dealFinish, LoadResPriority priority) { ResourceItem item = GetCacheResourrceItem(resObj.m_Crc); if (item != null) { resObj.m_ResItem = item; if (dealFinish != null) { dealFinish(path, resObj); } } //判断是否在加载中 AsyncLoadResParam para = null; if (!m_LoadingAssetDic.TryGetValue(resObj.m_Crc, out para) || para == null) { para = m_AsyncLoadResParamPool.Spawn(true); para.m_Crc = resObj.m_Crc; para.m_Path = path; para.m_Priority = priority; m_LoadingAssetDic.Add(resObj.m_Crc, para); m_LoadingAssetList[(int)priority].Add(para); } //往回调列表里面添加回调 AsyncCallBack callBack = m_AsyncCallBackPool.Spawn(true); callBack.m_DealFinish = dealFinish; callBack.m_ResObj = resObj; para.m_CallBackList.Add(callBack); }
/// <summary> /// 清空对象池 /// </summary> public void ClearCache() { List <uint> tempList = new List <uint>(); foreach (var key in m_ObjectPoolDic.Keys) { List <ResourceObj> st = m_ObjectPoolDic[key]; for (int i = st.Count - 1; i >= 0; i--) { ResourceObj resourceObj = st[i]; if (!System.Object.ReferenceEquals(resourceObj.m_CloneObj, null) && resourceObj.m_bClear) { GameObject.Destroy(resourceObj.m_CloneObj); m_ResourceObjDic.Remove(resourceObj.m_CloneObj.GetInstanceID()); resourceObj.Reset(); m_ResourceObjClassPool.Recycle(resourceObj); } } if (st.Count <= 0) { tempList.Add(key); } } for (int i = 0; i < tempList.Count; i++) { uint temp = tempList[i]; if (m_ObjectPoolDic.ContainsKey(temp)) { m_ObjectPoolDic.Remove(temp); } } tempList.Clear(); }
/// <summary> /// 根据ResourceObj减少引用计数 /// </summary> /// <param name="resObj"></param> /// <param name="count"></param> /// <returns></returns> public int DecreaseResourceRef(ResourceObj resObj, int count = 1) { return(resObj != null?DecreaseResourceRef(resObj.m_Crc, count) : 0); }
/// <summary> /// 异步加载 /// </summary> /// <returns></returns> IEnumerator AsyncLoadCor() { List <AsyncCallBack> callBackList = null; //上一次yield的时间 long lastYieldTime = System.DateTime.Now.Ticks; while (true) { bool haveYield = false; for (int i = 0; i < (int)LoadResPriority.RES_NUM; i++) { List <AsyncLoadResParam> loadingList = m_LoadingAssetList[i]; if (loadingList.Count <= 0) { continue; } AsyncLoadResParam loadingItem = loadingList[0]; loadingList.RemoveAt(0); callBackList = loadingItem.m_CallBackList; Object obj = null; ResourceItem item = null; #if UNITY_EDITOR if (!m_LoadFromAssetBundle) { obj = LoadAssetByEditor <Object>(loadingItem.m_Path); //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = AssetBundleManager.Instance.FindResourceItem(loadingItem.m_Crc); } #endif if (obj == null) { item = AssetBundleManager.Instance.LoatResourceAssetBundle(loadingItem.m_Crc); if (item != null && item.m_AssetBundle != null) { AssetBundleRequest abRequest = null; if (loadingItem.m_Sprite) { abRequest = item.m_AssetBundle.LoadAssetAsync <Sprite>(item.m_AssetName); } else { abRequest = item.m_AssetBundle.LoadAssetAsync(item.m_AssetName); } yield return(abRequest); if (abRequest.isDone) { obj = abRequest.asset; } lastYieldTime = System.DateTime.Now.Ticks; } } //缓存资源 CacheResource(loadingItem.m_Path, ref item, loadingItem.m_Crc, obj, callBackList.Count); for (int j = 0; j < callBackList.Count; j++) { AsyncCallBack callBack = callBackList[j]; if (callBack != null && callBack.m_DealFinish != null && callBack.m_ResObj != null) { ResourceObj tempResourceObj = callBack.m_ResObj; tempResourceObj.m_ResItem = item; callBack.m_DealFinish(loadingItem.m_Path, tempResourceObj, tempResourceObj.m_param1, tempResourceObj.m_param2, tempResourceObj.m_param3); callBack.m_DealFinish = null; tempResourceObj = null; } if (callBack != null && callBack.m_DealObjectFinish != null) { callBack.m_DealObjectFinish(loadingItem.m_Path, obj, callBack.m_Param1, callBack.m_Param2, callBack.m_Param3); callBack.m_DealObjectFinish = null; } callBack.Reset(); m_AsyncCallBackPool.Recycle(callBack); } obj = null; callBackList.Clear(); m_LoadingAssetDic.Remove(loadingItem.m_Crc); loadingItem.Reset(); m_AsyncLoadResParamPool.Recycle(loadingItem); if (System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME) { haveYield = true; lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } if (!haveYield || System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME) { lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } }
public bool IsObjectManagerCreate(GameObject tempObj) { ResourceObj resourceObj = m_ResourceObjDic[tempObj.GetInstanceID()]; return(resourceObj != null); }
/// <summary> /// 释放需要实例化的资源 /// </summary> /// <param name="obj"></param> /// <param name="maxCacheCount"></param> /// <param name="destoryCache"></param> /// <param name="recycleParent"></param> public void ReleaseObject(GameObject obj, int maxCacheCount = -1, bool destoryCache = false, bool recycleParent = true) { if (obj == null) { return; } ResourceObj resObj = null; int tempID = obj.GetInstanceID(); if (!m_ResourceObjDic.TryGetValue(tempID, out resObj)) { Debug.Log($"{obj.name}对象非ObjectManager创建"); } if (resObj == null) { Debug.LogError("缓存的ResourceObj为空!"); return; } if (resObj.m_CloneObj == null) { Debug.LogError("该对象已经放回对象池,检测自己是否清空引用!"); return; } #if UNITY_EDITOR obj.name += "(Recycle)"; #endif List <ResourceObj> st = null; if (maxCacheCount == 0) { m_ResourceObjDic.Remove(tempID); ResourceManager.Instance.ReleaseResource(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } else//回收到对象池 { if (!m_ObjectPoolDic.TryGetValue(resObj.m_Crc, out st) || st == null) { st = new List <ResourceObj>(); m_ObjectPoolDic.Add(resObj.m_Crc, st); } if (resObj.m_CloneObj) { if (recycleParent) { resObj.m_CloneObj.transform.SetParent(ResyclePoolTrs); } else { resObj.m_CloneObj.SetActive(false); } } if (maxCacheCount < 0 || st.Count < maxCacheCount) { st.Add(resObj); resObj.m_Already = true; ResourceManager.Instance.DecreaseResourceRef(resObj); } else { m_ResourceObjDic.Remove(tempID); ResourceManager.Instance.ReleaseResource(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } } }