/// <summary> /// 同步资源加载 , 外部直接调用,仅加载不需要实例化的资源,例如Texture,Audio /// </summary> /// <typeparam UIName="T"></typeparam> /// <param UIName="path"></param> /// <returns></returns> internal T LoadResource <T>(string path, uint crc = 0, ResLoadMode mode = ResLoadMode.Null) where T : UnityEngine.Object { if (string.IsNullOrEmpty(path)) { Debug.LogError("路径为null"); return(null); } if (mode == ResLoadMode.Null) { mode = LoadMode(ref path); } if (crc == 0) { crc = Crc32.GetCrc32(path); } ResItem item = GetCacheResouceItem(crc); if (item != null) { return(item.Obj as T); } //资源加载 item = LoadResource <T>(mode, path, crc); CacheResource(path, ref item, crc); //Debug.Log(item.Obj.name); return(item.Obj as T); }
/// <summary> /// 同步加载加载对象 /// </summary> /// <param name="path">对象路径</param> /// <param name="clearChangeScene">跳场景是否清除</param> /// <param name="setCreateNode">是否设置创建节点</param> /// <returns></returns> private GameObject InstantiateObject(string path, bool clearChangeScene, bool setCreateNode) { ResLoadMode mode = ResManager.Instance.LoadMode(ref path); uint crc = Crc32.GetCrc32(path); return(InstantiateObject(path, crc, mode, clearChangeScene, setCreateNode)); }
/// <summary> /// 预加载资源 /// </summary> public void PreloadResource <T>(string path, uint crc = 0, ResLoadMode mode = ResLoadMode.Null) where T : UnityEngine.Object { if (string.IsNullOrEmpty(path)) { return; } if (mode == ResLoadMode.Null) { mode = LoadMode(ref path); } if (crc == 0) { crc = Crc32.GetCrc32(path); } ResItem item = GetCacheResouceItem(crc, 0); if (item != null) { return; } //资源加载 item = LoadResource <T>(mode, path, crc); //缓存 CacheResource(path, ref item, crc); //跳场景不清空缓存 item.Clear = false; ReleaseResource(item.Obj, false); }
internal GameObject InstantiateObjectAsset(string path, int assetHelpHash, bool clearChangeScene, bool setCreateNode) { ResLoadMode mode = ResManager.Instance.LoadMode(ref path); uint crc = Crc32.GetCrc32(path); ResManager.Instance.AddObjAsset(crc, assetHelpHash); return(InstantiateObject(path, crc, mode, clearChangeScene, setCreateNode)); }
/// <summary> /// 异步对象加载 /// </summary> /// <param UIName="path"></param> /// <param UIName="fealFinish"></param> /// <param UIName="priority"></param> /// <param UIName="setSceneObject"></param> /// <param UIName="param1"></param> /// <param UIName="param2"></param> /// <param UIName="param3"></param> /// <param UIName="clearChangeScene"></param> private long InstantiateObjectAsync(string path, OnAsyncObjFinish onFinish, LoadResPriority priority, bool setCreateNode = false, params object[] paramValues) { if (string.IsNullOrEmpty(path)) { return(0); } ResLoadMode mode = ResManager.Instance.LoadMode(ref path); uint crc = Crc32.GetCrc32(path); return(InstantiateObjectAsync(path, crc, mode, onFinish, priority, setCreateNode, paramValues)); }
/// <summary> /// 预加载资源 /// </summary> public void PreloadResourceAsset <T>(string path, int assetHelpHash) where T : UnityEngine.Object { if (string.IsNullOrEmpty(path)) { Debug.LogError("预加载的资源路径为null"); return; } ResLoadMode mode = LoadMode(ref path); uint crc = Crc32.GetCrc32(path); AddResAsset(crc, assetHelpHash); PreloadResource <T>(path, crc, mode); }
/// <summary> /// 异步加载资源 (仅仅是不需要实例化的资源) /// </summary> internal void AsyncLoadResourceAsset <T>(string path, OnAsyncObjFinish onFinish, int assetHelperHash, LoadResPriority priority = LoadResPriority.Res_Slow, params object[] paramValues) where T : UnityEngine.Object { if (!string.IsNullOrEmpty(path)) { ResLoadMode mode = LoadMode(ref path); uint crc = Crc32.GetCrc32(path); AddResAsset(crc, assetHelperHash); AsyncLoadResource <T>(path, onFinish, crc, mode, priority, paramValues); } else { Debug.LogError("路径为null"); } }
internal T LoadResource <T>(string path, int assetHelperHash) where T : UnityEngine.Object { if (!string.IsNullOrEmpty(path)) { ResLoadMode mode = LoadMode(ref path); uint crc = Crc32.GetCrc32(path); AddResAsset(crc, assetHelperHash); var res = LoadResource <T>(path, crc, mode); return(res); } else { Debug.LogError("路径为null"); return(null); } }
private GameObject InstantiateObject(string path, uint crc, ResLoadMode mode, bool clearChangeScene, bool setCreateNode) { ResouceObj resouceObj = null; try { resouceObj = GetObjectFromPool(crc); } catch (Exception e) { Debug.LogError(e); Debug.LogError("对象路径 Path :" + path); } if (resouceObj == null) { resouceObj = new ResouceObj();// _ResourceObjClassPool.Spawn(true); resouceObj.Crc = crc; resouceObj.ClearByChangeScene = clearChangeScene; //ResouceManager提供加载方法 resouceObj = ResManager.Instance.LoadRessource(path, mode, resouceObj); if (resouceObj.ResItem.Obj != null) { resouceObj.CloneObj = GameObject.Instantiate(resouceObj.ResItem.Obj) as GameObject; } } if (setCreateNode) { resouceObj.CloneObj.transform.SetParent(_CreateNode, false); } resouceObj.Guid = resouceObj.CloneObj.GetInstanceID(); if (!_ResourceObjDic.ContainsKey(resouceObj.Guid)) { _ResourceObjDic.Add(resouceObj.Guid, resouceObj); } //引用计数 加一 resouceObj.ResItem.RefCount.Increase(); return(resouceObj.CloneObj); }
internal LoadResMode JudgeLoadMode(ResLoadMode mode) { LoadResMode load = LoadResMode.Resource; if (mode == ResLoadMode.Null) { load = loadResMode; } else if (mode == ResLoadMode.Res) { load = LoadResMode.Resource; } else if (mode == ResLoadMode.Ab) { load = LoadResMode.AssetBundle; } return(load); }
internal ResLoadMode LoadMode(ref string path) { ResLoadMode mode = ResLoadMode.Null; if (path.Length > ResKitConst.ResUrl.Length) { if (path.Contains(ResKitConst.ResUrl)) { path = path.Replace(ResKitConst.ResUrl, ""); mode = ResLoadMode.Res; } else if (path.Contains(ResKitConst.AbUrl)) { path = path.Replace(ResKitConst.AbUrl, ""); mode = ResLoadMode.Ab; } } return(mode); }
/// <summary> /// 针对ObjectManager的异步加载接口 /// </summary> /// <param UIName="path"></param> /// <param UIName="resObj"></param> /// <param UIName="dealFinish"></param> /// <param UIName="priority"></param> internal void AsyncLoadResource(string path, ResLoadMode mode, ResouceObj resObj, OnAsyncFinish dealFinish, LoadResPriority priority) { ResItem item = GetCacheResouceItem(resObj.Crc); if (item != null) { resObj.ResItem = item; if (dealFinish != null) { dealFinish(path, resObj); } return; } //判断是否在加载中 AsyncLoadResParam param = null; if (!_LoadingAssetDic.TryGetValue(resObj.Crc, out param) || param == null) { param = _AsyncLoadResParamPool.Spawn(true); param.Crc = resObj.Crc; if (mode == ResLoadMode.Ab) { param.LoadMode = LoadResMode.AssetBundle; } else { param.LoadMode = LoadResMode.Resource; } param.Path = path; param.Priority = priority; _LoadingAssetDic.Add(resObj.Crc, param); _LoadingAssetList[(int)priority].Add(param); } //回调列表里面加回调 AsyncCallBack callBack = _AsyncCallBackPool.Spawn(true); callBack.OnFinish = dealFinish; callBack.ResObj = resObj; param.CallBackList.Add(callBack); }
/// <summary> /// 异步对象加载 /// </summary> /// <param UIName="path"></param> /// <param UIName="fealFinish"></param> /// <param UIName="priority"></param> /// <param UIName="setSceneObject"></param> /// <param UIName="param1"></param> /// <param UIName="param2"></param> /// <param UIName="param3"></param> /// <param UIName="clearChangeScene"></param> private long InstantiateObjectAsync(string path, uint crc, ResLoadMode mode, OnAsyncObjFinish onFinish, LoadResPriority priority, bool setCreateNode = false, params object[] paramValues) { ResouceObj resObj = GetObjectFromPool(crc); if (resObj != null) { if (setCreateNode) { resObj.CloneObj.transform.SetParent(_CreateNode, false); } if (onFinish != null) { resObj.ResItem.RefCount.Increase(); onFinish(path, resObj.CloneObj, paramValues); } return(resObj.Guid); } long guid = ResManager.CreateGuid(); resObj = new ResouceObj();// _ResourceObjClassPool.Spawn(true); resObj.Crc = crc; resObj.SetParent = setCreateNode; resObj.OnFinish = onFinish; if (paramValues != null && paramValues.Length > 0) { foreach (object value in paramValues) { resObj.ParamList.Add(value); } } //调用ResourceManagerd的异步加载接口 ResManager.Instance.AsyncLoadResource(path, mode, resObj, OnLoadResourceObjFinish, priority); return(guid); }
/// <summary> /// 同步加载资源,针对ObjectManager的接口 /// </summary> /// <param UIName="path"></param> /// <param UIName="resObj"></param> /// <returns></returns> public ResouceObj LoadRessource(string path, ResLoadMode mode, ResouceObj resObj) { if (resObj == null) { return(null); } uint crc = resObj.Crc == 0 ? Crc32.GetCrc32(path) : resObj.Crc; ResItem item = GetCacheResouceItem(crc); if (item != null) { resObj.ResItem = item; return(resObj); } item = LoadResource <Object>(mode, path, crc); CacheResource(path, ref item, crc); resObj.ResItem = item; item.Clear = resObj.ClearByChangeScene; return(resObj); }
/// <summary> /// 异步加载资源 (仅仅是不需要实例化的资源) /// </summary> private void AsyncLoadResource <T>(string path, OnAsyncObjFinish onFinish, uint crc = 0, ResLoadMode mode = ResLoadMode.Null, LoadResPriority priority = LoadResPriority.Res_Slow, params object[] paramValues) where T : UnityEngine.Object { if (mode == ResLoadMode.Null) { mode = LoadMode(ref path); } if (crc == 0) { crc = Crc32.GetCrc32(path); } ResItem item = GetCacheResouceItem(crc); if (item != null) { if (onFinish != null) { onFinish(path, item.Obj, paramValues); } return; } //判断是否在加载中 AsyncLoadResParam param = null; if (!_LoadingAssetDic.TryGetValue(crc, out param) || param == null) { param = _AsyncLoadResParamPool.Spawn(true); param.Crc = crc; param.Path = path; param.Priority = priority; param.ResType = typeof(T); param.LoadMode = JudgeLoadMode(mode); _LoadingAssetDic.Add(crc, param); _LoadingAssetList[(int)priority].Add(param); } //回调列表里面加回调 AsyncCallBack callBack = _AsyncCallBackPool.Spawn(true); callBack.OnObjFinish = onFinish; if (paramValues != null && paramValues.Length > 0) { foreach (object value in paramValues) { callBack.Params.Add(value); } } param.CallBackList.Add(callBack); }
private ResItem LoadResource <T>(ResLoadMode mode, string path, uint crc) where T : UnityEngine.Object { T obj = null; ResItem item = null; #if UNITY_EDITOR if (IsEditorLoadMode) { if (JudgeLoadMode(mode) == LoadResMode.Resource) { obj = Resources.Load <T>(path); } else { obj = LoadAssetByEditor <T>(path); } } #endif if (obj == null) { if (JudgeLoadMode(mode) == LoadResMode.Resource) { obj = Resources.Load <T>(path); } else { item = AssetBundleMgr.Instance.LoadResourceAssetBundle(crc); if (item != null && item.AssetBundle != null) { if (item.Obj != null) { obj = item.Obj as T; } else { Debug.Log("AssetBundle模型下加载"); obj = item.AssetBundle.LoadAsset <T>(item.AssetName); } } else { Debug.LogErrorFormat("加载AssetBundle失败 : 不能从AssetBundleConfig中找到 Path :{0}", path); } } } if (obj == null) { Debug.LogError("加载资源失败,请检查资源[加载模式] mode ;" + loadResMode + " [路径] Path:" + path); } if (item == null) { item = new ResItem(); } item.Crc = crc; item.Obj = obj; if (obj) { item.Guid = obj.GetInstanceID(); } return(item); }