公司的项目准备用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
新人第一次发帖,若有错误,还望各位大神指出继承