/// <summary> /// 从本地缓存中加载 /// </summary> /// <param name="assetBundle"></param> /// <param name="scriptObj"></param> /// <param name="srcPath">资源路径</param> /// <param name="srcName"></param> /// <param name="type"></param> /// <param name="order"></param> /// <param name="isInstance"></param> /// <param name="progress">进度回调</param> /// <param name="complete"></param> /// <param name="args"></param> public void LoadFromAssetBundle(AssetBundle assetBundle, System.Object scriptObj, string srcPath, string srcName, ResourceType type, Order order, bool isInstance, LoadingProgressCallback progress, LoadCompleteCallback complete, params string[] args) { m_assetBundle = assetBundle; if (scriptObj != null) { m_scriptObj = scriptObj; m_hasScript = true; } else { m_hasScript = false; } m_scriptObj = scriptObj; m_srcPath = srcPath; m_srcName = srcName; m_type = type; m_order = order; m_isInstance = isInstance; m_progressCallback = progress; m_completeCallback = complete; m_customData = args; m_isDestroyed = false; m_isLoading = true; m_loadType = LoadType.fromAssetBundle; }
/// <summary> /// 加载完成回调 /// </summary> /// <param name="path"></param> /// <param name="name"></param> /// <param name="callback"></param> /// <param name="obj">当为场景时可以为null</param> /// <param name="isInstantiate"></param> /// <param name="args"></param> private Object ResCallBackReady(string path, string name, LoadCompleteCallback callback, Object obj, bool isInstantiate, params string[] args) { if (isInstantiate) { if (obj == null) { return(null); } Object t_obj = GameObject.Instantiate(obj); m_dictLocalResource[path].AddGameObjectRef(t_obj as GameObject); m_dictGameObjectRes[t_obj.GetInstanceID()] = m_dictLocalResource[path]; if (callback != null) { callback(path, name, t_obj, args); } return(t_obj); } else { if (callback != null) { callback(path, name, obj, args); } return(obj); } }
/// <summary> /// 开始加载 /// </summary> /// <param name="scriptObj">请求的脚本</param> /// <param name="srcPath">资源路径</param> /// <param name="srcName">资源名</param> /// <param name="type">ResourceType</param> /// <param name="order">Order</param> /// <param name="isInstance">是否实例化</param> /// <param name="isDownload">是否需要下载</param> /// <param name="progress">进度回调</param> /// <param name="complete">结束回调</param> /// <param name="args">自定义参数</param> public void LoadFromWWW(System.Object scriptObj, string srcPath, string srcName, ResourceType type, Order order, bool isInstance, bool isDownload, LoadingProgressCallback progress, LoadCompleteCallback complete, params string[] args) { if (scriptObj != null) { m_scriptObj = scriptObj; m_hasScript = true; } else { m_hasScript = false; } m_scriptObj = scriptObj; m_srcPath = srcPath; m_srcName = srcName; m_type = type; m_order = order; m_isInstance = isInstance; m_isNeedDownload = isDownload; m_progressCallback = progress; m_completeCallback = complete; m_customData = args; m_isDestroyed = false; if (m_Www == null) { if (m_isNeedDownload) { m_Www = new WWW(GetPath(m_srcPath)); m_isLoading = true; } } m_loadType = LoadType.fromWWW; }
public void LoadTexture(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { NTexture texture; if (mPool.TryGetValue(url, out texture)) { texture.refCount++; onSuccess(texture); return; } GameEntry.Resource.LoadAsset(url, typeof(Texture2D), new LoadAssetCallbacks( (assetName, asset, duration, userData) => { texture = new NTexture(asset as Texture2D); texture.refCount++; onSuccess(texture); }, (assetName, status, errorMessage, userData) => { onFail(errorMessage); })); }
private void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { AssetsLoader loader = new AssetsLoader("icon/" + url, url, new ArrayList { onSuccess, onFail }); loader.Load(this.OnLoadComplete, null, this.OnLoadError); }
public void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { LoadItem item = new LoadItem(); item.url = url; item.onSuccess = onSuccess; item.onFail = onFail; _items.Add(item); }
private void OnLoadComplete(object sender, AssetsProxy assetsproxy, object data) { AssetsLoader loader = ( AssetsLoader )sender; ArrayList al = ( ArrayList )loader.data; LoadCompleteCallback onSuccess = ( LoadCompleteCallback )al[0]; Texture2D texture = assetsproxy.LoadAsset <Texture2D>(loader.assetName); onSuccess?.Invoke(texture); }
/// <summary> /// 增加回调函数 /// </summary> /// <param name="loader">加载器</param> /// <param name="completeCallback">回调</param> public void PushCallback(Loader loader, LoadCompleteCallback completeCallback) { if (null == completeCallback) { return; } if (m_DicLoaderDatas.TryGetValue(loader.path, out LoaderData loaderData)) { loaderData.completeCallbacks.Add(completeCallback); } }
public void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { LoadItem item = new LoadItem(); item.url = url; item.onSuccess = onSuccess; item.onFail = onFail; _items.Add(item); if(!_started) StartCoroutine(Run()); }
public void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { LoadItem item = new LoadItem(); item.url = url; item.onSuccess = onSuccess; item.onFail = onFail; _items.Add(item); if (!_started) // _started = false, 进入方法 { StartCoroutine(Run()); } }
public void LoadIcon(string _url, LoadCompleteCallback _onSuccess, LoadErrorCallback _onFail) { LoadItem tempItem = new LoadItem(); tempItem.url = _url; tempItem.onSuccess = _onSuccess; tempItem.onFail = _onFail; items.Add(tempItem); if (!started) { StartCoroutine(Run()); } }
public void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { LoadItem item = new LoadItem(); item.url = url; item.onSuccess = onSuccess; item.onFail = onFail; _items.Add(item); Debug.Log("url:" + url); if (!_started) { StartCoroutine(Run()); } }
static int LoadIcon(IntPtr L) { try { ToLua.CheckArgsCount(L, 4); IconManager obj = (IconManager)ToLua.CheckObject <IconManager>(L, 1); string arg0 = ToLua.CheckString(L, 2); LoadCompleteCallback arg1 = (LoadCompleteCallback)ToLua.CheckDelegate <LoadCompleteCallback>(L, 3); LoadErrorCallback arg2 = (LoadErrorCallback)ToLua.CheckDelegate <LoadErrorCallback>(L, 4); obj.LoadIcon(arg0, arg1, arg2); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { if (texCached.ContainsKey(url)) { NTexture texture = texCached[url]; texture.refCount++; if (onSuccess != null) { onSuccess(texture); } return; } AppFacade.Instance.GetManager <ResManager>(ManagerName.Resource).Load(url, abInfo => { NTexture texture = null; if (texCached.ContainsKey(url)) { texCached[url].refCount++; texture = texCached[url]; } else { if (abInfo == null) { if (onFail != null) { onFail(""); } return; } texture = new NTexture((Texture2D)abInfo.mainObject); texture.refCount++; texCached[url] = texture; } if (onSuccess != null) { onSuccess(texture); } }); }
public void LoadResourceAsync <T>(string path, LoadCompleteCallback callback = null) where T : UnityEngine.Object { uint crc = Crc32.GetCrc32(path); if (loadedResourceItemDic.ContainsKey(crc)) { UnityEngine.Object asset = loadedResourceItemDic[crc].asset; if (callback != null) { callback(asset); } } else { ResourceItem item = ResourceManager.Instance.LoadResourceAB(crc); AssetBundleRequest request = null; if (!loadingResourceItemDic.ContainsKey(crc)) { request = item.assetBundle.LoadAssetAsync <T>(path); loadingResourceItemDic.Add(crc, request); } else { request = loadingResourceItemDic[crc]; } request.completed += (AsyncOperation obj) => { this.CacheResource(ref item, request.asset); loadingResourceItemDic.Remove(crc); if (callback != null) { callback(request.asset); } }; } }
public IEnumerator LoadAsset(string url, LoadCompleteCallback callback) { if (mLoadedAssets.ContainsKey(url)) { callback(mLoadedAssets[url]); yield break; } // Load dependencies if exists AssetBundleCreateRequest asyncLoad = null; AssetHeader header = mLocalAssets[url]; if (header.dependencies != null && header.dependencies.Length > 0) { foreach (var depUrl in header.dependencies) { if (!mLoadedAssets.ContainsKey(depUrl)) { asyncLoad = AssetBundle.LoadFromFileAsync(GetFullPath(depUrl)); yield return(asyncLoad); mLoadedAssets.Add(depUrl, new Asset(mLocalAssets[depUrl], asyncLoad.assetBundle)); } } } // Load main asset bundle asyncLoad = AssetBundle.LoadFromFileAsync(GetFullPath(url)); Asset asset = new Asset(mLocalAssets[url], asyncLoad.assetBundle); mLoadedAssets.Add(url, asset); yield return(asyncLoad); if (callback != null) { callback(asset); } }
public LoadCompleteCallback LoadCompleteCallback(LuaFunction func, LuaTable self, bool flag) { if (func == null) { LoadCompleteCallback fn = delegate(FairyGUI.NTexture param0) { }; return(fn); } if (!flag) { LoadCompleteCallback_Event target = new LoadCompleteCallback_Event(func); LoadCompleteCallback d = target.Call; target.method = d.Method; return(d); } else { LoadCompleteCallback_Event target = new LoadCompleteCallback_Event(func, self); LoadCompleteCallback d = target.CallWithSelf; target.method = d.Method; return(d); } }
/// <summary> /// 回收后可以再利用 /// </summary> /// <param name="destory"></param> public void Release(bool destory = false) { m_loaded = 0; m_isLoading = false; m_isDestroyed = true; m_assetBundle = null; if (m_Www != null) { if (destory && string.IsNullOrEmpty(m_Www.error) && m_Www.assetBundle != null) { m_Www.assetBundle.Unload(true); } m_Www.Dispose(); m_Www = null; } m_progressCallback = null; m_completeCallback = null; m_customData = null; }
public void LoadIcon(string url, LoadCompleteCallback onSuccess, LoadErrorCallback onFail) { var ret = LoadSync(url); if (ret != null && onSuccess != null) { onSuccess(ret); } else if (onFail != null) { onFail(url); } //LoadItem item = new LoadItem(); //item.url = url; //item.onSuccess = onSuccess; //item.onFail = onFail; //_items.Add(item); //if (!_started) // StartCoroutine(Run()); }
/// <summary> /// Start coroutine to load specified asset /// </summary> /// <param name="url">URL of the asset</param> /// <param name="version">Asset version</param> /// <param name="callback">Load complete callback method</param> public void LoadAssetAsync(string url, float version, LoadCompleteCallback callback) { if (mLocalAssets.ContainsKey(url)) { if (mLocalAssets[url].version == version) { if (mLoadedAssets.ContainsKey(url)) { callback(mLoadedAssets[url]); } else { StartCoroutine(LoadAsset(url, callback)); } return; } } else { callback(null); } StartCoroutine(DownloadAsset(url, callback)); }
/// <summary> /// Load an asset asynchronously into memory. /// </summary> public void Load <TObject>(string key, LoadCompleteCallback <TObject> onComplete = null) where TObject : UnityEngine.Object { if (IsLoaded(typeof(TObject), key)) { onComplete?.Invoke(true, Get <TObject>(key)); return; } _numLoadCalls++; // when multiple identical load calls all happen at the same time, we don't want to execute them all, // we just need to load once and distribute the result to all the callbacks if (IsLoading(typeof(TObject), key)) { if (!_typeToKeyToDuplicateLoadCallbacks.TryGetValue(typeof(TObject), out Dictionary <string, List <Action> > keyToDuplicateLoadCallbacks)) { keyToDuplicateLoadCallbacks = new Dictionary <string, List <Action> >(); _typeToKeyToDuplicateLoadCallbacks.Add(typeof(TObject), keyToDuplicateLoadCallbacks); } if (!keyToDuplicateLoadCallbacks.TryGetValue(key, out List <Action> duplicateLoadCallbacks)) { duplicateLoadCallbacks = new List <Action>(); keyToDuplicateLoadCallbacks.Add(key, duplicateLoadCallbacks); } duplicateLoadCallbacks.Add(() => { // by the time this executes, the asset should be loaded TObject payload = Get <TObject>(key); _numCompletions++; onComplete?.Invoke(payload != null, payload); }); // return, we'll execute the callback when the first load call completes return; } // record that we are already loading this asset if (!_loadingAssets.TryGetValue(typeof(TObject), out HashSet <string> loadingKeys)) { loadingKeys = new HashSet <string>(); _loadingAssets.Add(typeof(TObject), loadingKeys); } loadingKeys.Add(key); Addressables.LoadAssetAsync <TObject>(key).Completed += (AsyncOperationHandle <TObject> obj) => { _numCompletions++; if (obj.Result == null) { Debug.LogErrorFormat("the asset loaded with the key {0} is null", key); } else { if (!_loadedAssets.ContainsKey(typeof(TObject))) { _loadedAssets.Add(typeof(TObject), new Dictionary <string, UnityEngine.Object>()); } _loadedAssets[typeof(TObject)].Add(key, obj.Result as TObject); loadingKeys.Remove(key); } onComplete?.Invoke(obj.Result != null, obj.Result as TObject); // now invoke any of the duplicate load complete callbacks if (_typeToKeyToDuplicateLoadCallbacks.ContainsKey(typeof(TObject))) { if (_typeToKeyToDuplicateLoadCallbacks[typeof(TObject)].ContainsKey(key)) { foreach (Action callback in _typeToKeyToDuplicateLoadCallbacks[typeof(TObject)][key]) { callback?.Invoke(); } _typeToKeyToDuplicateLoadCallbacks[typeof(TObject)][key].Clear(); } } }; }
private Queue <AssetLoader> m_loaderPool = new Queue <AssetLoader>(); //加载器缓存池 /// <summary> /// 获取资源 /// 场景不会返回对象资源,文本是同步加载,并且它们都不缓存 /// </summary> /// <param name="scriptObject">引用资源的脚本</param> /// <param name="path">资源包全路径(带名字)</param> /// <param name="name">要请求的资源名</param> /// <param name="progress"></param> /// <param name="complete"></param> /// <param name="type">ResourceType</param> /// <param name="isInstance">是否实例化</param> /// <param name="isCache">是否缓存资源,可实例化的资源在引用计数为0时也不销毁,不可实例化资源跟引用计数无关</param> /// <param name="args"></param> /// <returns>原始对象,如果能直接得到则为从缓存中取出</returns> public Object GetResource(System.Object scriptObject, string path, string name, LoadingProgressCallback progress, LoadCompleteCallback complete, AssetType type, bool isInstance, bool isCache, params string[] args) { Asset t_asset; AssetLoader t_loader; LoaderInfo t_loaderInfo; if (m_dictResource.TryGetValue(path, out t_asset)) { if (t_asset.OriginalObj != null) { if (progress != null) { progress(path, name, 1f, args); } if (complete != null) { if (IsNeedIncreaseRefCount(type, isInstance)) { t_asset.AddRefCount(scriptObject); } return(ResCallBackReady(path, name, t_asset.OriginalObj, isInstance, complete, args)); } return(null); } else//第一个加载此路径的资源包还未加载完成,第2次已经请求了 { if (IsNeedIncreaseRefCount(type, isInstance)) { t_asset.AddRefCount(scriptObject); } t_loaderInfo = AssetLoader.CreateLoaderInfo(scriptObject, progress, complete, args); m_dictLoader[path].Add(t_loaderInfo); } } else//加载资源包 { t_asset = new Asset(name, path, isCache); m_dictResource.Add(path, t_asset); if (IsNeedIncreaseRefCount(type, isInstance)) { t_asset.AddRefCount(scriptObject); } t_loader = PoolFrom(); t_loaderInfo = AssetLoader.CreateLoaderInfo(scriptObject, progress, complete, args); t_loader.Add(t_loaderInfo); t_loader.Load(path, name, type, isInstance, isCache); m_dictLoader.Add(path, t_loader); } return(null); }
private int m_loaderCount = 0; //统计所有加载器,!!!需要与实际加载器数同步? /// <summary> /// 获取实例化后的资源,默认类型为gameobject,默认优先级 /// 场景不会返回对象资源,文本是同步加载,并且它们都不缓存 /// </summary> /// <param name="scriptObject">引用资源的脚本</param> /// <param name="path">资源包全路径(带名字)</param> /// <param name="name">要请求的资源名</param> /// <param name="progress"></param> /// <param name="complete"></param> /// <param name="args"></param> public void GetLocalResource(System.Object scriptObject, string path, string name, LoadingProgressCallback progress, LoadCompleteCallback complete, params string[] args) { GetLocalResource(scriptObject, path, name, progress, complete, ResourceType.gameobject, Order.o_default, args); }
/// <summary> /// 初始化资源,会实例化 /// 场景不会返回对象资源,文本是同步加载,并且它们都不缓存 /// </summary> /// <param name="scriptObject">引用资源的脚本</param> /// <param name="path">资源包全路径(带名字)</param> /// <param name="name">要请求的资源名</param> /// <param name="progress"></param> /// <param name="complete"></param> /// <param name="type">ResourceType</param> /// <param name="order">优先级</param> /// <param name="args"></param> /// <returns></returns> public void GetLocalResource(System.Object scriptObject, string path, string name, LoadingProgressCallback progress, LoadCompleteCallback complete, ResourceType type, Order order, params string[] args) { GetLocalResource(scriptObject, path, name, progress, complete, type, order, true, args); }
/// <summary> /// 获取资源 /// 场景不会返回对象资源,文本是同步加载,并且它们都不缓存 /// </summary> /// <param name="scriptObject">引用资源的脚本</param> /// <param name="path">资源包全路径(带名字)</param> /// <param name="name">要请求的资源名</param> /// <param name="progress"></param> /// <param name="complete"></param> /// <param name="type">ResourceType</param> /// <param name="order">优先级</param> /// <param name="isInstance">是否实例化</param> /// <param name="args"></param> /// <returns>原始对象,如果能直接得到则为从缓存中取出</returns> public Object GetLocalResource(System.Object scriptObject, string path, string name, LoadingProgressCallback progress, LoadCompleteCallback complete, ResourceType type, Order order, bool isInstance, params string[] args) { ResInfo t_info; ResLoader t_loader; bool t_isNeedDownload = true; if (m_dictLocalResource.TryGetValue(path, out t_info)) { if (t_info.assetBundle != null)//之前已经加载过相同路径的资源 { Object t_assetObj = t_info.GetAsset(name); if (t_assetObj != null) { #if Debug Debug.Log("from cache"); #endif if (progress != null) { progress(path, name, 1f, args); } if (complete != null) { if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } return(ResCallBackReady(path, name, complete, t_assetObj, isInstance, args)); } return(null); } else { if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } t_loader = new ResLoader(); t_loader.LoadFromAssetBundle(t_info.assetBundle, scriptObject, path, name, type, order, isInstance, progress, complete, args); m_dictDownload[(int)order].Add(t_loader); ++m_loaderCount; return(null); } } else//第一个加载此路径的资源包还未加载完成,第2次已经请求了 { if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } t_isNeedDownload = false; } } else//加载资源包 { t_info = new ResInfo(path); m_dictLocalResource.Add(path, t_info); if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } } t_loader = new ResLoader(); t_loader.LoadFromWWW(scriptObject, path, name, type, order, isInstance, t_isNeedDownload, progress, complete, args); m_dictDownload[(int)order].Add(t_loader); ++m_loaderCount; return(null); }
public void CreateResources(Microsoft.Graphics.Canvas.UI.Xaml.CanvasAnimatedControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args, LoadCompleteCallback callback) { bitmapHolder.CreateResources(sender, args); callback(); }
/// <summary> /// 创建加载信息 /// </summary> /// <param name="scriptObj">请求的脚本</param> /// <param name="srcPath">资源路径</param> /// <param name="srcName">资源名</param> /// <param name="type">ResourceType</param> /// <param name="isInstance">是否实例化</param> /// <param name="isCache">是否缓存资源,可实例化的资源在引用计数为0时也不销毁,不可实例化资源跟引用计数无关</param> /// <param name="progress">进度回调</param> /// <param name="complete">结束回调</param> /// <param name="args">自定义参数</param> public static LoaderInfo CreateLoaderInfo(System.Object scriptObj, LoadingProgressCallback progress, LoadCompleteCallback complete, params string[] args) { LoaderInfo t_loaderInfo = new LoaderInfo(); t_loaderInfo.scriptObj = scriptObj; t_loaderInfo.progressCallback = progress; t_loaderInfo.completeCallback = complete; t_loaderInfo.customData = args; return(t_loaderInfo); }
void Push_LoadCompleteCallback(IntPtr L, LoadCompleteCallback o) { ToLua.Push(L, o); }
public IEnumerator DownloadAsset(string url, LoadCompleteCallback callback) { if (mLoadedAssets.ContainsKey(url)) { callback(mLoadedAssets[url]); yield break; } // Download header Asset asset = new Asset(); yield return(StartCoroutine(DownloadAssetHeader(url, asset))); if (asset.header == null) { callback(null); yield break; } // Download all dependency Asset dependentAsset = new Asset(); if (asset.header.dependencies != null && asset.header.dependencies.Length > 0) { foreach (var dependency in asset.header.dependencies) { yield return(StartCoroutine(DownloadAssetHeader(dependency, dependentAsset))); if (dependentAsset.header == null) { callback(null); yield break; } yield return(StartCoroutine(DownloadAssetBundle(dependency, dependentAsset))); if (dependentAsset.bundle == null) { callback(null); yield break; } } } // Download main asset yield return(StartCoroutine(DownloadAssetBundle(url, asset))); if (asset.bundle == null) { callback(null); yield break; } SaveAssetManifest(); yield return(null); if (callback != null) { callback(asset); } }