/// <summary> /// 异步创建 /// </summary> /// <param name="assetName">Asset name.</param> /// <param name="onCreate">On create.</param> public void CreateAsync(string assetName, OnCreate onCreate) { //当前创建器为空 if (curAssetCreator == null) { curAssetCreator = objAssembly.CreateInstance(curAssetCreatorClass) as AssetCreatorBase; curAssetCreator.assetName = assetName; curAssetCreator.onCreate = onCreate; curAssetCreator.LoadAsset(); //StartCoroutine(curAssetCreator.LoadAssetAsync(assetBundle, curAssetCreator.assetName)); } //同名资源追加 回调方法, 当前创建器 assetName else if (curAssetCreator.assetName.Equals(assetName)) { curAssetCreator.onCreate += onCreate; } //检查当前创建器等待队列 else if (assetLoadingTable.ContainsKey(assetName)) { (assetLoadingTable[assetName] as AssetCreatorBase).onCreate += onCreate; } else { //新创建加载器 //AssetCreatorBase assetCreator = new AssetCreator(); AssetCreatorBase assetCreator = objAssembly.CreateInstance(curAssetCreatorClass) as AssetCreatorBase; assetCreator.assetName = assetName; assetCreator.assetBundle = null; assetCreator.onCreate = onCreate; //加入等待队列 assetLoading.Enqueue(assetCreator); //加入等待队列映射 assetLoadingTable.Add(assetCreator.assetName, assetCreator); } }
void Update() { //卸载中创建器不工作 if (ReleaseStack()) { return; } if (IsFree()) { //检测队列 if (assetLoading.Count > 0) { //弹出排队目标 curAssetCreator = assetLoading.Dequeue(); //加入等待队列映射 assetLoadingTable.Remove(curAssetCreator.assetName); //启动加载 //StartCoroutine(curAssetCreator.LoadAssetAsync(curAssetCreator.assetBundle, curAssetCreator.assetName)); curAssetCreator.LoadAsset(); } } else { if (curAssetCreator.isCmp()) { //AssetManager.ResetShader(curAssetCreator.getData()); //缓存模板实例 CacheGameObject(curAssetCreator.assetName, curAssetCreator.getData()); //返回 assts 资源回调 curAssetCreator.CallBack(); //释放当前加载状态 Free(); } else { //正在加载中! //Debug.Log("curAssetBundleLoader = " + curAssetCreator.progress()); } } }//end Update()
/// <summary> /// Creates the async. /// 此方法不支持 AssetBundle 引用计数 /// </summary> /// <param name="assetBundle">Asset bundle.</param> /// <param name="assetName">Asset name.</param> /// <param name="onCreate">On create.</param> public void CreateAsync(AssetBundle assetBundle, string assetName, OnCreate onCreate) { Debug.LogFormat("加载 assetName = {0}", assetName); if (curAssetCreator == null) { curAssetCreator = new AssetCreator(); curAssetCreator.assetName = assetName; curAssetCreator.onCreate = onCreate; StartCoroutine(curAssetCreator.LoadAssetAsync(assetBundle, curAssetCreator.assetName)); } else { AssetCreator assetCreator = new AssetCreator(); assetCreator.assetName = assetName; assetCreator.assetBundle = assetBundle; assetCreator.onCreate = onCreate; //加入等待队列 assetLoading.Enqueue(assetCreator); //加入等待队列映射 assetLoadingTable.Add(assetCreator.assetName, assetCreator); } }
void Free() { curAssetCreator.Clear(); curAssetCreator = null; }