公司的項目準備用ab包的形式開發,而加載ab包的功能放在一個沒有繼承MonoBehaviour的類中,全部在加載ab包的時候,無法用協程來判斷ab包是否加載完成,因此我本身用了一種方式來判斷;異步
我用的事異步加載ab包,code
AssetBundleCreateRequest asset = AssetBundle.LoadFromFileAsync(path);
固然還有其餘的加載方式,網上有不少大神都對此有很詳細的描述,這裏我就不在贅述了,咱們能夠經過委託 asset.completed,當單個資源包加載完成後,要執行的事;協程
void LoadAssetsBundle() { m_Path = Application.dataPath; //獲取運行文件的同級目錄 Directory.SetCurrentDirectory(Directory.GetParent(m_Path).FullName); m_Path = Directory.GetCurrentDirectory(); string assetBundlenPath = ConfigManager.Instance().GetAssetBundlePath(); m_Path += "/"+ assetBundlenPath; m_AssetsNameList = ConfigManager.ReadAssetBundleName(); string path = m_Path + "/" + m_AssetsNameList[0]; AssetBundleCreateRequest asset = AssetBundle.LoadFromFileAsync(path); asset.completed += AssetBundleLoadcompleted; } private void AssetBundleLoadcompleted(AsyncOperation asset) { AssetBundleCreateRequest assetBundle = (AssetBundleCreateRequest)asset; if (asset == null) { Debug.Log(m_AssetsNameList[m_Index] + "加載失敗"); } else { Debug.Log(m_AssetsNameList[m_Index] + "加載成功"); m_AssetBundleList.Add(assetBundle.assetBundle); } if (m_Index < m_AssetsNameList.Count - 1) { m_Index++; //LoadAsset(m_Index); string path = m_Path + "/" + m_AssetsNameList[m_Index]; if (!File.Exists(path)) { Debug.Log(m_AssetsNameList[m_Index] + " AssetBundle包文件不存在", LogType.Error); return; } AssetBundleCreateRequest NextAsset = AssetBundle.LoadFromFileAsync(path); NextAsset.completed += AssetBundleLoadcompleted; } else { Debug.Log(m_AssetsNameList.Count + "個資源包所有加載完成"); for (int i = 0; i < m_AssetBundleList.Count; i++) { Debug.Log(m_AssetBundleList[i].name); } } }
上述代碼中有些方法是公司項目中自定義的,須要稍加修改。blog
新人第一次發帖,若有錯誤,還望各位大神指出繼承