public IEnumerator load() { ResourceLoadTaskGroup group = new ResourceLoadTaskGroup(); foreach (TileCfg tileCfg in TileCfg.dataList) { ResourceLoadTask task = new ResourceLoadTask(); task.path = tileCfg.resourcePath; task.name = tileCfg.resourceName; group.addTask(task); } yield return(ResourceLoader.LoadGroupAsync(group)); for (int i = 0; i < TileCfg.dataList.Count; i++) { ResourceLoadTask t = group.getTaskList()[i]; textures.Add(t.asset as Texture2D); Tileset tileset = new Tileset(); tileset.init(t.asset as Texture2D, TileLength); tilesets.Add(TileCfg.dataList[i].id, tileset); } ResourceLoadTask taskMapTilePrefab = new ResourceLoadTask(); taskMapTilePrefab.path = PrefabCfg.get(2).resourcePath; taskMapTilePrefab.name = PrefabCfg.get(2).resourceName; yield return(ResourceLoader.LoadAssetAsync(taskMapTilePrefab)); mapTilePrefab = taskMapTilePrefab.asset as GameObject; }
//获取依赖文件任务; private void GetDependsTask(string varAssetFileName) { string[] tempDepends = ResourceManager.GetManager().GetAssetBundleDepends(varAssetFileName); if (tempDepends != null && tempDepends.Length > 0) { if (mDependList == null) { mDependList = new List <ResourceLoadTask>(); } for (int i = 0; i < tempDepends.Length; i++) { string tempDependRes = tempDepends[i]; tempDependRes = tempDependRes.ToLower(); //GetDependsTask(tempDependRes); ResourceLoadTask tempTask = ResourceManager.GetManager().GetResourceLoadTask(tempDependRes); if (tempTask == null) { tempTask = new AssetBundleLoadTask(tempDependRes, null, null, null, null); mDependList.Add(tempTask); } } } }
/// <summary> /// Adds the task. /// only Download. /// </summary> /// <param name="url">URL.</param> /// <param name="version">Version.</param> public void AddTask(string url, int version) { if (HasTask(url)) { return; } tasks.Add(url, ResourceLoadTask.Get(new DownLoaderTask(url, version), true)); }
public void AddTask(DownLoaderTask task, string name) { if (HasTask(task.URL)) { return; } tasks.Add(task.URL, ResourceLoadTask.Get(task, name)); }
protected virtual AssetRequestBase LoadAsset(ResourceLoadTask task) { if (task.IsLocal) { return(new AssetLocalRequestTask(Resources.LoadAsync(task.Name))); } else { return(new AssetWWWRequestTask(task.WWWTask.Asset.LoadAsync(task.Name, task.WWWTask.AssetType))); } }
/// <summary> /// 获取一个已完成的任务; /// </summary> /// <param name="AssetFileName"></param> /// <returns></returns> public ResourceLoadTask GetResourceLoadTask(string AssetFileName) { if (string.IsNullOrEmpty(AssetFileName)) { return(null); } ResourceLoadTask tempTask = null; mLoadedBundles.TryGetValue(AssetFileName, out tempTask); return(tempTask); }
public void NotifyWhenLoaded(ResourceLoadTask task) { buffers.Add(task.Name, task); if (loadedCallback.ContainsKey(task.Name)) { List <CustomerAction <Object> > temp = loadedCallback[task.Name]; for (int i = 0, max = temp.Count; i < max; i++) { temp[i](task.Asset); } } }
public static IEnumerator loadFromResourcesDirAsync(ResourceLoadTask task) { //加载resources文件夹里的文件,不能有后缀。 ResourceRequest resourceRequest = Resources.LoadAsync(task.path + "/" + getNameWithOutExt(task.name)); while (!resourceRequest.isDone) { yield return(true); } task.asset = resourceRequest.asset; task.state = STATE_DONE; }
/// <summary> /// 添加一个已完成的任务; /// </summary> /// <param name="task"></param> /// <returns></returns> public bool AddLoadedTask(ResourceLoadTask task) { if (task == null) { return(false); } if (!string.IsNullOrEmpty(task.pAssetFileName)) { if (string.IsNullOrEmpty(task.pError) && mLoadedBundles.ContainsKey(task.pAssetFileName) == false) { mLoadedBundles.Add(task.pAssetFileName, task); return(true); } } return(false); }
/// <summary> /// 加载资源的方法; /// </summary> /// <param name="AssetFileName"></param> /// <param name="AssetObjectName"></param> /// <param name="AssetObjectType"></param> /// <param name="callback"></param> /// <param name="varParam"></param> /// <returns></returns> public bool LoadAssetBundle(string AssetFileName, string AssetObjectName, Type AssetObjectType, ResourceLoadCallback callback = null, object varParam = null) { ResourceLoadTask tempTask = null; if (mResourcesModel == ResourcesModel.RM_Resources) { tempTask = new EditorResourceLoadTask(AssetFileName, AssetObjectName, AssetObjectType, callback, varParam); } else { AssetFileName = AssetFileName.ToLower(); tempTask = new AssetBundleLoadTask(AssetFileName, AssetObjectName, AssetObjectType, callback, varParam); } mLoadtasks.Enqueue(tempTask); return(true); }
/// <summary> /// Update this instance. /// /// </summary> public virtual void Update() { //create new www. if (currentDownloadTask == null) { if (tasks.Count > 0) { CreateWWW(); } } else { //chech if done or not. if (currentDownloadTask.WWWTask.IsDone) { currentDownloadTask.WWWing = false; if (currentDownloadTask.WWWTask.ErrorMsg != null) { GlobalLog.LogWarning(currentDownloadTask.Name + " load failed, " + currentDownloadTask.WWWTask.ErrorMsg); } else { if (!currentDownloadTask.OnlyDownload) { AdddLoadingBuffer(currentDownloadTask); } } currentDownloadTask = null; } } //check loaded or not. for (indexFor = 0, maxFor = assetBundleLoadingBuffer.Count; indexFor < maxFor; indexFor++) { if (assetBundleLoadingBuffer[indexFor] == null) { continue; } if (assetBundleLoadingBuffer[indexFor].RequestTask.Request.isDone) { NotifyWhenLoaded(assetBundleLoadingBuffer[indexFor]); assetBundleLoadingBuffer[indexFor] = null; } } }
public static IEnumerator LoadAssetAsync(ResourceLoadTask task) { switch (Application.platform) { case RuntimePlatform.Android: { yield return(loadFromAssetaBundleAsync(task)); } break; case RuntimePlatform.WindowsEditor: { //yield return loadFromAssetaBundleAsync(task); yield return(loadFromResourcesDirAsync(task)); } break; } yield return(loadFromResourcesDirAsync(task)); }
public static IEnumerator Load() { ResourceLoadTask task = new ResourceLoadTask(); task.path = "cfg"; task.name = "tile.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); TileCfg.load((task.asset as TextAsset).bytes); task.name = "resources.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); ResourcesCfg.load((task.asset as TextAsset).bytes); task.name = "prefab.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); PrefabCfg.load((task.asset as TextAsset).bytes); task.name = "prefabType.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); PrefabTypeCfg.load((task.asset as TextAsset).bytes); task.name = "audio.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); AudioCfg.load((task.asset as TextAsset).bytes); task.name = "frameAnimation.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); FrameAnimationCfg.load((task.asset as TextAsset).bytes); task.name = "characterSprite.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); CharacterSpriteCfg.load((task.asset as TextAsset).bytes); task.name = "unitType.csv"; yield return(ResourceLoader.LoadAssetAsync(task)); UnitTypeCfg.load((task.asset as TextAsset).bytes); }
private void AdddLoadingBuffer(ResourceLoadTask val) { tempAssetRequestTask = LoadAsset(val); if (tempAssetRequestTask != null) { val.CreateRequest(tempAssetRequestTask); for (indexFor = 0, maxFor = assetBundleLoadingBuffer.Count; indexFor < maxFor; indexFor++) { if (assetBundleLoadingBuffer[indexFor] == null) { assetBundleLoadingBuffer[indexFor] = val; break; } } if (indexFor == maxFor) { assetBundleLoadingBuffer.Add(val); } } }
private void CreateWWW() { Dictionary <string, ResourceLoadTask> .Enumerator enumertor = tasks.GetEnumerator(); if (enumertor.MoveNext()) { currentDownloadTask = enumertor.Current.Value; if (!currentDownloadTask.IsLocal) { currentDownloadTask.WWWing = currentDownloadTask.WWWTask.StartDownload(); } else { //add load buff.TODO. } if (!currentDownloadTask.WWWing) { currentDownloadTask = null; } tasks.Remove(enumertor.Current.Key); } }
public IEnumerator init() { InitUnitsMemory(); unitDrawer = new GameObjectPool(new UnitDrawerFactory()); PrefabCfg prefabcfg = PrefabCfg.get(1); ResourceLoadTask task = new ResourceLoadTask(); task.path = prefabcfg.resourcePath; task.name = prefabcfg.resourceName; yield return(ResourceLoader.LoadAssetAsync(task)); if (task.asset == null) { Debug.LogError("load charactor drawer prefab erro"); } characrerDrawerPrefab = task.asset as GameObject; foreach (UnitTypeCfg unitTypeCfg in UnitTypeCfg.dataList) { } yield return(0); }
/// <summary> /// 每帧更新,所有的load完成后的回调都在这里完成 /// </summary> IEnumerator InitLoad() { ResourceLoadTask temTask = null; while (true) { if (temTask != null) { if (string.IsNullOrEmpty(temTask.pAssetFileName)) { temTask.Callback(); } else { yield return(StartCoroutine(temTask.Load())); if (string.IsNullOrEmpty(temTask.pError) == false) { Debug.Log(temTask.pError); } AddLoadedTask(temTask);//缓存完成的任务; ///无论加载成功或失败都要回调 temTask.Callback(); } temTask = null; } if (mLoadtasks.Count > 0) { temTask = mLoadtasks.Dequeue(); } yield return(null); } }
public void addTask(ResourceLoadTask task) { taskList.Add(task); }
public static void LoadFromResourcesDir(ResourceLoadTask task) { task.asset = Resources.Load(task.path + "/" + task.name.Split('.')[0]); task.state = STATE_DONE; }
private static IEnumerator loadFromAssetaBundleAsync(ResourceLoadTask task) { task.state = STATE_LOADING; task.name = task.name.Trim().ToLower(); task.path = task.path.Trim().ToLower(); string key = task.path + "|" + task.name; LoadedAsset loadedAsset = null; //如果资源已经在其他地方加载了,等待加载完成,然后直接获取 if (loadedAssets.ContainsKey(key)) { loadedAsset = loadedAssets[key]; while (loadedAsset.state != STATE_DONE) { yield return(0); } task.state = STATE_DONE; task.asset = loadedAsset.asset; yield break; } else { loadedAsset = new LoadedAsset(); loadedAsset.state = STATE_LOADING; loadedAssets[key] = loadedAsset; } //先加载依赖资源 if (!resources.ContainsKey(key)) { Debug.LogError(key); foreach (var VARIABLE in resources.Keys) { Debug.LogError(VARIABLE); } task.asset = null; task.state = STATE_DONE; yield break; } foreach (AssetPathAndName assetPathAndName in resources[key]) { ResourceLoadTask t = new ResourceLoadTask(); t.name = assetPathAndName.name; t.path = assetPathAndName.path; yield return(loadFromAssetaBundleAsync(t)); // Debug.Log(assetPathAndName.path + " " + assetPathAndName.name); } LoadedAssetBundle bundle = null; if (loadedAssetBundles.ContainsKey(task.path)) { bundle = loadedAssetBundles[task.path]; if (bundle.state != STATE_DONE) { yield return(0); } } else { bundle = new LoadedAssetBundle(); loadedAssetBundles[task.path] = bundle; bundle.state = STATE_LOADING; WWW asset = new WWW(streamingAssetsPathURL + task.path + "." + ResourceVariant); yield return(asset); bundle.assetBundle = asset.assetBundle; bundle.state = STATE_DONE; } loadedAsset.asset = bundle.assetBundle.LoadAsset(task.name); loadedAsset.state = STATE_DONE; task.state = STATE_DONE; task.asset = loadedAsset.asset; }
/// <summary> /// AssetBundle资源任务的异步加载实现; /// </summary> /// <returns></returns> override public IEnumerator Load() { pLoadState = ResourceLoadState.Loading; ResourceLoadTask tempTask = ResourceManager.GetManager().GetResourceLoadTask(pAssetFileName); //如果任务已经加载过,则直接获取资源并完成; if (tempTask != null) { pAssetBundle = tempTask.pAssetBundle; pLoadObject = tempTask.pLoadObject; if (pLoadObject == null && pAssetBundle != null && !string.IsNullOrEmpty(pAssetObjectName)) { pLoadObject = pAssetBundle.LoadAsset(pAssetObjectName); } pLoadState = ResourceLoadState.Finished; yield break; } //否则,进行资源的加载; //获取资源的依赖文件; GetDependsTask(pAssetFileName); ResourceLoadTask temTask = null; if (mDependList != null) { //如果有依赖文件则先加载依赖文件; while (mDependList.Count > 0) { if (temTask != null) { if (!string.IsNullOrEmpty(temTask.pAssetFileName)) { yield return(temTask.Load()); if (string.IsNullOrEmpty(temTask.pError) == false) { Debug.Log(temTask.pError); } ResourceManager.GetManager().AddLoadedTask(temTask); } ///无论加载成功或失败都要回调 temTask.Callback(); temTask = null; mDependList.RemoveAt(0); } if (mDependList.Count > 0) { temTask = mDependList[0]; } } } //通过WWW加载资源; string path = Application.dataPath + "/AssetBundles/" + pAssetFileName; WWW www = new WWW(path); yield return(www); if (www.isDone) { pAssetBundle = www.assetBundle; if (pAssetBundle != null) { if (!string.IsNullOrEmpty(pAssetObjectName) && pAssetObjectType != null) { pLoadObject = pAssetBundle.LoadAsset(pAssetObjectName, pAssetObjectType); } } pLoadState = ResourceLoadState.Finished; } yield return(null); }
protected override AssetRequestBase LoadAsset(ResourceLoadTask task) { return(new AssetWWWRequestTask(task.WWWTask.Asset.LoadAsync("characterBase", typeof(GameObject)))); }
public IEnumerator loadUnitType(int unitTypeId, LoaderResult result) { if (id2UnitType.ContainsKey(unitTypeId)) { result.isDone = true; result.asset = id2UnitType[unitTypeId]; yield break; } UnitTypeCfg unitTypeCfg = UnitTypeCfg.get(unitTypeId); int spriteCfgId = unitTypeCfg.spritId; if (!id2CharacterSprite.ContainsKey(spriteCfgId)) { CharacterSpriteCfg spriteCfg = CharacterSpriteCfg.get(spriteCfgId); CharacterSprite sprite = new CharacterSprite(); sprite.animations[(int)UnitAnimation.Run] = new CharacterAnimation(); for (int i = 0; i < 8; i++) { int spriteAnimId = spriteCfg.runAnim[i]; if (spriteAnimId == 0) { sprite.animations[(int)UnitAnimation.Run].anim[i] = null; continue; } if (!id2FrameAnimation.ContainsKey(spriteAnimId)) { FrameAnimationCfg spriteAnimCfg = FrameAnimationCfg.get(spriteAnimId); List <Sprite> anim = new List <Sprite>(); ResourceLoadTaskGroup group = new ResourceLoadTaskGroup(); for (int j = spriteAnimCfg.nBegin; j <= spriteAnimCfg.nEnd; j++) { ResourceLoadTask task = new ResourceLoadTask(); task.path = spriteAnimCfg.resourcePath; string resourceName = j + ".0.png"; task.name = resourceName; group.addTask(task); } yield return(ResourceLoader.LoadGroupAsync(group)); for (int j = 0; j <= spriteAnimCfg.nEnd - spriteAnimCfg.nBegin; j++) { Texture2D texture2d = group.getTaskList()[j].asset as Texture2D; Sprite s = Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0), spriteAnimCfg.fixelsPerUnit ); anim.Add(s); } FrameAnimation frameanimation = new FrameAnimation(); frameanimation.frames = anim; frameanimation.frameRate = spriteAnimCfg.frameRate; id2FrameAnimation[spriteAnimId] = frameanimation; } sprite.animations[(int)UnitAnimation.Run].anim[i] = id2FrameAnimation[spriteAnimId].frames; } sprite.frameRate = spriteCfg.frameRate; id2CharacterSprite[spriteCfgId] = sprite; } UnitType unitType = new UnitType(); unitType.sprite = id2CharacterSprite[spriteCfgId]; result.isDone = true; result.asset = unitType; id2UnitType[unitTypeId] = unitType; yield break; }