/// <summary> /// 异步加载资源 /// handlerFinish:结束回调 /// </summary> public void loadAssetAsync(string strAssetPath, task.TaskBase.FinishedHandler handlerFinish = null) { if (!isAssetbundleLoaded(strAssetPath)) { if (AssetManager.getInstance().IsStreamingAssets(strAssetPath)) { //异步创建StreamingAssets下资源 strAssetPath = UtilTools.PathCheck(strAssetPath); AssetBundleLoadTask task = new AssetBundleLoadTask(strAssetPath, null); if (handlerFinish != null) { task.EventFinished += handlerFinish; } } else { AssetLoadTask task = new AssetLoadTask(strAssetPath, null); if (handlerFinish != null) { task.EventFinished += handlerFinish; } } } else { if (AssetManager.getInstance().IsStreamingAssets(strAssetPath)) { strAssetPath = UtilTools.PathCheck(strAssetPath); } AssetManager.getInstance().addAssetbundleRefCount(strAssetPath); StartCoroutine("loadAssetAsyncCallback", handlerFinish); } }
/// <summary> /// 初始化场景管理类 /// </summary> public void initialize() { #if UNITY_IOS localNotification = new LocalNotificationIOS(); #elif UNITY_EDITOR #endif if (localNotification == null) { return; } string xmlPath = "Config/localNotification.json"; AssetLoadTask task = new AssetLoadTask(xmlPath, null); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { Object assetObj = ((AssetLoadTask)currentTask).getTargetAsset(); if (assetObj != null) { localNotification.CleanNotification(); ParseXML(assetObj.ToString()); //LogSys.Log("load config success :" + xmlPath); } else { LogSys.LogError("load config failed:" + xmlPath); } }); }
private IEnumerator BeginInitialize() { if (mStatus == LoadStatus.Done) { yield break; } else if (mStatus == LoadStatus.Loading) { yield return(new WaitUntil(() => mStatus == LoadStatus.Done)); } else { mStatus = LoadStatus.Loading; //初始化资源列表 yield return(AssetPath.Initialize()); if (AssetPath.mode == AssetMode.AssetBundle) { BundleAsset bundle = GetOrCreateBundle <BundleAsset>(AssetPath.list.manifest); AssetLoadTask <AssetBundleManifest> task = new AssetLoadTask <AssetBundleManifest>(AssetPath.list.manifest, FinishInitialize); task.assetName = "AssetBundleManifest"; yield return(bundle.LoadAssetAsync(task)); } else { FinishInitialize(null); } } }
/// <summary> /// key可以是文件名(带后缀)或者文件路径(Assets/...)。 /// 注意,当非实例化资源(非GameObject,如Material,Texture,TextAsset等)使用完毕后,需要主动调用Destroy去释放资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="callback"></param> /// <returns></returns> public static IAssetLoadTask <T> LoadAsset <T>(string key, Action <Asset <T> > callback) where T : UnityEngine.Object { AssetLoadTask <T> task = new AssetLoadTask <T>(key.ToLower(), callback); LoadAsset(task); return(task); }
public void loadLocalAssetAsync(string assetBundleName, string assetName, Action <AssetBundleRequest> loadCB) { string assetBundlePath = StreamingAssetPath + "AssetBundles/" + assetBundleName; //string assetBundlePath = "http://www.vmetu.com/external/res/buttons"; Debug.Log("Asset:" + assetBundlePath); AssetBundleLoadTask bundleTask = null; if (_assetBundleDic.ContainsKey(assetBundlePath)) { bundleTask = _assetBundleDic[assetBundlePath]; } else { bundleTask = new AssetBundleLoadTask { path = assetBundlePath, state = AssetLoadState.E_NONE }; _assetBundleDic.Add(assetBundlePath, bundleTask); } AssetLoadTask assetLoadTask = bundleTask.getOrCreateAssetLoadTask(assetName); switch (bundleTask.state) { case AssetLoadState.E_NONE: //还没开始加载 bundleTask.state = AssetLoadState.E_LOADING; assetLoadTask.loadedCb += loadCB; StartCoroutine(doLoadAssetBunlde(bundleTask)); break; case AssetLoadState.E_LOADING: //下载中,把回调加入到响应的回调中 { assetLoadTask.loadedCb += loadCB; } break; case AssetLoadState.E_LOADED: //assetBundle已下载完成 { if (assetLoadTask.state == AssetLoadState.E_LOADED) { loadCB(assetLoadTask.assetBundleRequest); } else if (assetLoadTask.state == AssetLoadState.E_LOADED) { assetLoadTask.loadedCb += loadCB; } else { StartCoroutine(doLoadAsset(assetName, bundleTask)); } } break; } }
//show win public bool CreateWin(int iDepth, bool byAction) { IsClose = false; if (_isLoading || _isShow) { return(false); } windDepth = iDepth; _isLoading = true; _isShow = true; isCreateByAction = byAction; ClearAllCacheEvent(); _loadResTaskList.Clear(); _loadCompleteList.Clear(); string[] appendPrefabs = GetAppendAtlasPrefabs(); if (appendPrefabs.Length > 0) { for (int i = 0; i < appendPrefabs.Length; i++) { if (AssetManager.getInstance().IsStreamingAssets(appendPrefabs[i])) { //转为assetbundle路径 appendPrefabs[i] = UtilTools.PathCheck(appendPrefabs[i]); //加载StreamingAssets下资源 AssetBundleLoadTask task = new AssetBundleLoadTask(appendPrefabs[i]); _loadResTaskList.Add(task); task.EventFinished += (manual, currentTask) => { LoadAppendResourceComplete(currentTask._taskName); }; } else { //加载Resources下资源 AssetLoadTask task = new AssetLoadTask(appendPrefabs[i]); _loadResTaskList.Add(task); task.EventFinished += (manual, currentTask) => { LoadAppendResourceComplete(currentTask._taskName); }; } } } else { LoadAppendResourceComplete(""); } UILoadCallback(); return(true); }
public AssetLoadTask getOrCreateAssetLoadTask(string assetName) { if (assetReqDic.ContainsKey(assetName)) { return(assetReqDic[assetName]); } else { AssetLoadTask assetLoadTask = new AssetLoadTask { assetName = assetName, state = AssetLoadState.E_NONE, }; assetReqDic.Add(assetName, assetLoadTask); return(assetLoadTask); } }
public LoadState(Game game, Func <State> nextState) : base(game) { m_stage = LoadStage.Startup; m_nextStateFunc = nextState; m_loadTask = null; m_backdrop = new Image(Texture.Black, Game.Screen.Width, Game.Screen.Height); m_backdrop.Anchor = Anchor.CentreMiddle; m_backdrop.LocalPosition = new Vector2(-0.5f * m_backdrop.Width, -0.5f * m_backdrop.Height); m_widget = new Image(Texture.Get("gui/loading.png", true), GetWidgetQuad(0.0f), 64.0f, 64.0f); m_widget.Anchor = Anchor.CentreMiddle; m_widget.LocalPosition = new Vector2(-0.5f * m_widget.Width, -0.5f * m_widget.Height); m_timeInStage = 0.0f; }
private IEnumerator doLoadAsset(string name, AssetBundleLoadTask assetBundleLoadTask) { AssetLoadTask assetLoadTask = assetBundleLoadTask.getOrCreateAssetLoadTask(name); // 异步加载 assetLoadTask.assetBundleRequest = assetBundleLoadTask.bundle.LoadAssetAsync(name); // 等待加载完成 yield return(assetLoadTask.assetBundleRequest); assetLoadTask.state = AssetLoadState.E_LOADED; if (assetLoadTask.loadedCb != null) { assetLoadTask.loadedCb(assetLoadTask.assetBundleRequest); } assetLoadTask.loadedCb = null; }
/* * void Update() * { * if (_javaObj != null) * messsage = _javaObj.CallStatic<string>("GetLOG"); * } * * void OnGUI() * { * if (GUI.Button(new Rect(20, 20, 100, 100), "Save")) * { * //SaveScreenshot(); * * playVideo("threecountrycg.mov", 1136, 640, 1); * } * * if (GUI.Button(new Rect(20, 250, 100, 100), "Copy")) * { * //_javaObj.CallStatic("Copy", "sssssssa"); * * playVideo("newbieguide1.mp4", 1136, 640, 1); * } * * if (GUI.Button(new Rect(20, 400, 100, 100), "Paste")) * { * //_javaObj.CallStatic<string>("Paste"); * * playVideo("newbieguide3.mp4", 1136, 640, 1); * } * * //GUI.TextField(new Rect(0, Screen.height - 100, Screen.width, 100), messsage); * * } * */ /// <summary> /// 开启推送服务器 /// </summary> public void startLocalNotification() { string xmlPath = "Config/localNotification.json"; AssetLoadTask task = new AssetLoadTask(xmlPath, null); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { UnityEngine.Object assetObj = ((AssetLoadTask)currentTask).getTargetAsset(); if (assetObj != null) { _javaSysUtilsOptObj.Call("startPushNotificationService", assetObj.ToString()); } else { LogSys.LogError("load config failed:" + xmlPath); } }); }
public void LoadAppendResourceComplete(string path) { if (!string.IsNullOrEmpty(path)) { _loadCompleteList.Add(path); } if (_loadCompleteList.Count >= _loadResTaskList.Count)//依赖资源加载完后加载界面 { if (AssetManager.getInstance().IsStreamingAssets(prefabsPath[0])) { //转为assetbundle路径 prefabsPath[0] = UtilTools.PathCheck(prefabsPath[0]); //加载StreamingAssets下资源 AssetBundleLoadTask task = new AssetBundleLoadTask(prefabsPath[0]); _loadResTaskList.Add(task); task.EventFinished += (manual, currentTask) => { Object assetObj = ((AssetBundleLoadTask)currentTask).getTargetAsset(); if (assetObj != null) //assetObj为窗口的prefab { LoadResourceComplete(assetObj); //AssetManager.getInstance().removeAssetbundle(prefabsPath[0]); } }; } else { //加载Resources下资源 AssetLoadTask task = new AssetLoadTask(prefabsPath[0]); _loadResTaskList.Add(task); task.EventFinished += (manual, currentTask) => { Object assetObj = ((AssetLoadTask)currentTask).getTargetAsset(); if (assetObj != null) //assetObj为窗口的prefab { LoadResourceComplete(assetObj); assetObj = null; //AssetManager.getInstance().removeAssetbundle(prefabsPath[0]); } }; } } }
/// <summary> /// 实例化2 /// appendedAssets:指定额外的特殊依赖项 /// </summary> public void Instantiate(string strAssetPath, Vector3 position, Quaternion rotation, InitiateHandler handlerFinish, string[] appendedAssets = null) { //string strLatesVersionpath = _objPathData.getLatestVersionPath(strPath); Utils.LogSys.Log("AssetManager Get Path Data 5:" + strAssetPath); EAssetPathType eType = _objPathData.getAssetPathType(strAssetPath); if (eType == EAssetPathType.eResources)//异步创建Resource下资源 { AssetLoadTask task = new AssetLoadTask(strAssetPath, appendedAssets); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { Object assetObj = ((AssetLoadTask)currentTask).getTargetAsset(); if (assetObj != null) { GameObject obj = GameObject.Instantiate(assetObj, position, rotation) as GameObject; ((AssetLoadTask)currentTask).unloadUnusedAssetbundle(false); handlerFinish(obj); } }); } else { //异步创建StreamingAssets下资源 AssetBundleLoadTask task = new AssetBundleLoadTask(strAssetPath, appendedAssets); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { Object assetObj = ((AssetBundleLoadTask)currentTask).getTargetAsset(); if (assetObj != null) { GameObject obj = GameObject.Instantiate(assetObj, position, rotation) as GameObject; ((AssetBundleLoadTask)currentTask).unloadUnusedAssetbundle(false); handlerFinish(obj); } }); } }
// Use this for initialization void Start() { _task = new AssetLoadTask(); StartCoroutine(_task.Run()); }
protected override void OnUpdate(float dt) { // Advance state m_timeInStage += dt; switch (m_stage) { case LoadStage.LoadingNewAssets: { // Start asset load if (m_loadTask == null) { m_loadTask = StartLoad(); } // Load some assets m_loadTask.LoadSome(new TimeSpan(250 * TimeSpan.TicksPerMillisecond)); var loadProgress = (m_loadTask.Total > 0) ? ((float)m_loadTask.Loaded / (float)m_loadTask.Total) : 1.0f; SetProgress(loadProgress); // Continue if (m_loadTask.Remaining == 0) { if (m_loadTask.Total > 0) { App.Log("Loaded {0} assets", m_loadTask.Total); } NextStage(); } break; } case LoadStage.UnloadingOldAssets: { // Unload some assets var loaded = Assets.Count; Assets.UnloadUnsourced(); if (loaded > Assets.Count) { App.Log("Unloaded {0} assets", loaded - Assets.Count); } NextStage(); break; } case LoadStage.LoadingAtlases: { TextureAtlas.Reload("models/tiles"); NextStage(); break; } case LoadStage.LoadingAnims: { LuaAnimation.ReloadAll(); NextStage(); break; } case LoadStage.Finalising: { Game.SelectLanguage(); GC.Collect(); NextStage(); break; } case LoadStage.Animating: { // Loading is done. Make the robot blink m_widget.Texture = Texture.Get("gui/load_complete.png", true); if (m_timeInStage < 0.4f || m_timeInStage >= 0.55f) { m_widget.Area = new Quad(0.0f, 0.0f, 0.5f, 1.0f); } else { m_widget.Area = new Quad(0.5f, 0.0f, 0.5f, 1.0f); } if (m_timeInStage >= 1.0f) { NextStage(); } break; } default: { NextStage(); break; } } }
/// <summary> /// 载入xml配置 /// </summary> public override void LoadXML() { string xmlPath = _configPath + _fileName; // Object assets = AssetManager.getInstance().loadXML(xmlPath);//同步加载XML // if (assets != null) { // ParseXML(assets.ToString()); // LogSys.Log("load config success :" + xmlPath); // } //if (AssetManager.getInstance().IsFirstUseStreamingAssets) if (AssetManager.getInstance().IsStreamingAssets(xmlPath)) { xmlPath = UtilTools.PathCheck(xmlPath); AssetBundleLoadTask task = new AssetBundleLoadTask(xmlPath, null); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { TextAsset assetObj = ((AssetBundleLoadTask)currentTask).getTargetAsset() as TextAsset; if (assetObj != null) { byte[] text_byte = assetObj.bytes; if (UtilTools.ArrayHeadIsWoDong(assetObj.bytes)) { CMyEncryptFile _encrypte = new CMyEncryptFile(); text_byte = _encrypte.Decrypt(assetObj.bytes, assetObj.bytes.Length); } ParseXML(text_byte); //LogSys.Log("load config success :" + xmlPath); } else { LogSys.LogError("load config failed:" + xmlPath); } _bLoadedXML = true; }); } else { AssetLoadTask task = new AssetLoadTask(xmlPath, null); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { TextAsset assetObj = ((AssetLoadTask)currentTask).getTargetAsset() as TextAsset; if (assetObj != null) { byte[] text_byte = assetObj.bytes; if (UtilTools.ArrayHeadIsWoDong(assetObj.bytes)) { CMyEncryptFile _encrypte = new CMyEncryptFile(); text_byte = _encrypte.Decrypt(assetObj.bytes, assetObj.bytes.Length); } float start_time = Time.realtimeSinceStartup; ParseXML(text_byte); //Utils.LogSys.Log(string.Format("{0:0.00}", Time.realtimeSinceStartup - start_time) + " parse :" + currentTask._taskName); //LogSys.Log("load config success :" + xmlPath); } else { LogSys.LogError("load config failed:" + xmlPath); } _bLoadedXML = true; }); } }