public AssetLoaderRequest LoadScene(string rABName, string rAssetName, LoadSceneMode rLoadSceneMode, bool bIsSimulate) { var rRequest = new AssetLoaderRequest(rABName, rAssetName, rLoadSceneMode, false, bIsSimulate); this.LoadAssetSync(rRequest); return(rRequest); }
public void LoadAssetAsync <T>(AssetLoaderRequest request) where T : Object { string assetBundlePath = RemoveSuffix(request.AssetPath); string assetBundleName = GetAssetBundleNameByAssetMaps(assetBundlePath); AssetBundleInfo abInfo = null; if (_asset_bundles.TryGetValue(assetBundleName, out abInfo)) { if (abInfo.IsLoader) { abInfo.AsyncLoadAsset <T>(request.AssetPath, request); } else { AssetBundleLoaderRequest abRequest = AssetBundleLoaderRequest.New(assetBundlePath); abRequest.OnCompleted += (rt) => { rt.AsyncLoadAsset <T>(request.AssetPath, request); }; InternalLoadAssetBundleAsync(abInfo, abRequest); } } }
public AssetLoaderRequest LoadAsset(string rABName, bool bIsSimulate) { var rRequest = new AssetLoaderRequest(rABName, string.Empty, false, bIsSimulate, true); this.LoadAssetSync(rRequest); return(rRequest); }
private IEnumerator LoadAllAssets_ByAssetbundle(AssetLoaderRequest rRequest, AssetBundle rAssetbundle) { var rAllAssetsRequest = rAssetbundle.LoadAllAssetsAsync(); yield return(rAllAssetsRequest); rRequest.AllAssets = rAllAssetsRequest.allAssets; }
private IEnumerator LoadAsset_Async(AssetLoaderRequest rRequest) { ABLoadEntry rAssetLoadEntry = null; if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry)) { Debug.LogErrorFormat("Can not find assetbundle: -- {0}", rRequest.Path); rRequest.Asset = null; rRequest.SetResult(rRequest); yield break; } //引用计数加1 rAssetLoadEntry.RefCount++; // 确认未加载完成并且正在被加载、一直等待其加载完成 while (rAssetLoadEntry.IsLoading && !rAssetLoadEntry.IsLoadCompleted) { yield return(new WaitForEndOfFrame()); } // 如果该资源加载完成了 if (!rAssetLoadEntry.IsLoading && rAssetLoadEntry.IsLoadCompleted) { // 从缓存的Assetbundle里面加载资源 yield return(LoadAssetObject(rRequest, rAssetLoadEntry, false)); rRequest.SetResult(rRequest); yield break; } // 开始加载资源依赖项 if (rAssetLoadEntry.ABDependNames != null && !rRequest.IsSimulate) { for (int i = rAssetLoadEntry.ABDependNames.Length - 1; i >= 0; i--) { string rDependABPath = rAssetLoadEntry.ABDependNames[i]; string rDependABName = rDependABPath; var rDependAssetRequest = new AssetLoaderRequest(rDependABName, "", false, rRequest.IsSimulate, false); yield return(LoadAsset_Async(rDependAssetRequest)); } } //开始加载当前的资源包 rAssetLoadEntry.IsLoading = true; rAssetLoadEntry.IsLoadCompleted = false; // 真正的从AB包里面加载资源 yield return(LoadAssetObject(rRequest, rAssetLoadEntry, true)); rRequest.SetResult(rRequest); rAssetLoadEntry.IsLoading = false; rAssetLoadEntry.IsLoadCompleted = true; }
public IAsyncOperation <AssetLoaderRequest> LoadAllAssets(string rAssetbundleName, bool bIsSimulate) { if (!this.LoadedAssetbundles.Contains(rAssetbundleName)) { this.LoadedAssetbundles.Add(rAssetbundleName); } var rRequest = new AssetLoaderRequest(rAssetbundleName, "AllAssets", false, bIsSimulate, true); return(rRequest.Start(LoadAsset_Async(rRequest))); }
public IAsyncOperation <AssetLoaderRequest> LoadScene(string rAssetbundleName, string rScenePath) { if (!this.LoadedScenebundles.Contains(rAssetbundleName)) { this.LoadedScenebundles.Add(rAssetbundleName); } var rSceneRequest = new AssetLoaderRequest(rAssetbundleName, rScenePath, true, false, false); return(rSceneRequest.Start(LoadAsset_Async(rSceneRequest))); }
private void LoadAssetSync_OneEntry(AssetLoaderRequest rRequest, ABLoadEntry rABLoadEntry) { // 从缓存的Assetbundle里面加载资源 rABLoadEntry.IsLoading = true; rABLoadEntry.IsLoadCompleted = false; this.LoadAssetObjectSync(rRequest, rABLoadEntry); rABLoadEntry.IsLoading = false; rABLoadEntry.IsLoadCompleted = true; // 如果判断此时的RefCount为0的话,那么就unload掉该项资源 this.AutoCheckUnloadAsset(rABLoadEntry); }
private IEnumerator LoadAssetAsync(AssetLoaderRequest rRequest) { this.mIsLoadingRefCount++; ABLoadEntry rAssetLoadEntry = null; if (!rRequest.IsSimulate) { if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry)) { Debug.LogErrorFormat("---Can not find assetbundle: -- {0}", rRequest.Path); rRequest.Asset = null; this.mIsLoadingRefCount--; rRequest.SetResult(rRequest); yield break; } } else { rAssetLoadEntry = new ABLoadEntry() { ABName = rRequest.Path, ABPath = ABLoaderVersion.Instance.GetABPath_With_Space(LoaderSpace.Streaming, rRequest.Path), ABDependNames = new string[0], }; } // 得到该资源的所有依赖项 var rABAllDependenceEntries = this.GetABEntryAllDependencies(rAssetLoadEntry); this.mTempEntries.Clear(); for (int i = 0; i < rABAllDependenceEntries.Count; i++) { rABAllDependenceEntries[i].RefCount++; if (!this.mTempEntries.Contains(rABAllDependenceEntries[i])) { this.mTempEntries.Add(rABAllDependenceEntries[i]); } } for (int i = 0; i < this.mTempEntries.Count; i++) { // 构建依赖项的Request var rItem = this.mTempEntries[i]; var rDependenceLoaderRequest = new AssetLoaderRequest( rItem.ABPath, string.Empty, true, rRequest.IsSimulate, rRequest.IsLoadAllAssets); yield return(this.LoadAssetAsync_OneEntry(rDependenceLoaderRequest, rItem)); } yield return(this.LoadAssetAsync_OneEntry(rRequest, rABAllDependenceEntries[rABAllDependenceEntries.Count - 1])); this.mIsLoadingRefCount--; rRequest.SetResult(rRequest); }
private void Awake() { DontDestroyOnLoad(this); List <IManager> managers = new List <IManager>() { new ResourcesSceneManager(), new ResourcesManager() }; Managers.Add(managers); string assetName2 = "L_Resources/DataConfig/dataconfig_soundmissionfenbao.bytes"; AssetLoaderRequest request = AssetLoaderRequest.New(assetName2); Managers.Resources.LoadAssetAsyncByPath <TextAsset>(request); request.OnLoaderCompleted = (asset) => { Debug.Log("异步加载单个资源:" + asset.name); }; string assetName1 = "L_Resources/Animation/Quad.controller"; Object gameObject = Managers.Resources.LoadAsset <Object>(assetName1); Debug.Log("同步加载单个资源:" + gameObject.name); string assetPath1 = "L_Resources/DataConfig"; Object[] objects1 = Managers.Resources.LoadAllAssets(assetPath1); Debug.Log("加载某个路径下的所有资源:" + objects1.Length); string assetPath2 = "L_Resources/DynamicSceneConfig"; Object[] objects2 = Managers.Resources.LoadAllAssets(assetPath2); Debug.Log("加载某个路径下的所有资源:" + objects2.Length); string sceneName = "Scenes/SampleScene.unity"; //StartCoroutine(unloadScene()); loadRequest = Managers.SceneManager.LoadSceneAsync(sceneName); List <string> abAssetNames = new List <string>() { "dataconfig", "l_lua.protocolgenerated.accelerate" }; //StartCoroutine(loadAssets(abAssetNames)); }
public void LoadAssetAsyncByPath <T>(AssetLoaderRequest request) where T : Object { if (!UseAssetBundle) { string assetPath = string.Format("Assets/{0}", request.AssetPath); T t = LoadAssetAtPath <T>(assetPath); if (request.OnLoaderCompleted != null) { request.OnLoaderCompleted(t); } } else { _resource_load.LoadAssetAsync <T>(request); } }
public void AsyncLoadAsset <T>(string fileName, AssetLoaderRequest asset) where T : Object { if (_bundle == null) { return; } fileName = string.Format("{0}/{1}", "Assets", fileName); AssetBundleRequest request = _bundle.LoadAssetAsync <T>(fileName); asset.Setup(request); request.completed += (rt) => { asset.Completed(request.asset); }; }
private void LoadAssetSync(AssetLoaderRequest rRequest) { this.mIsLoadingRefCount++; ABLoadEntry rAssetLoadEntry = null; if (!rRequest.IsSimulate) { if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry)) { Debug.LogErrorFormat("---Can not find assetbundle: -- {0}", rRequest.Path); rRequest.Asset = null; this.mIsLoadingRefCount--; return; } } else { rAssetLoadEntry = new ABLoadEntry() { ABName = rRequest.Path, ABPath = ABLoaderVersion.Instance.GetABPath_With_Space(LoaderSpace.Streaming, rRequest.Path), ABDependNames = new string[0], }; } // 得到该资源的所有依赖项 var rABAllDependenceEntries = this.GetABEntryAllDependencies(rAssetLoadEntry); for (int i = 0; i < rABAllDependenceEntries.Count; i++) { rABAllDependenceEntries[i].RefCount++; } for (int i = 0; i < rABAllDependenceEntries.Count - 1; i++) { // 构建依赖项的Request var rDependenceLoaderRequest = new AssetLoaderRequest( rABAllDependenceEntries[i].ABPath, string.Empty, true, rRequest.IsSimulate, rRequest.IsLoadAllAssets); this.LoadAssetSync_OneEntry(rDependenceLoaderRequest, rABAllDependenceEntries[i]); } this.LoadAssetSync_OneEntry(rRequest, rABAllDependenceEntries[rABAllDependenceEntries.Count - 1]); this.mIsLoadingRefCount--; }
private IEnumerator LoadAssetAsync_OneEntry(AssetLoaderRequest rRequest, ABLoadEntry rABLoadEntry) { // 确认未加载完成并且正在被加载、一直等待其加载完成 while (rABLoadEntry.IsLoading && !rABLoadEntry.IsLoadCompleted) { // 如果两个都为false,则断开协程停下来 if (rABLoadEntry.RefCount == 0) { yield break; } yield return(0); } // 从缓存的Assetbundle里面加载资源 rABLoadEntry.IsLoading = true; rABLoadEntry.IsLoadCompleted = false; yield return(LoadAssetObjectAsync(rRequest, rABLoadEntry)); rABLoadEntry.IsLoading = false; rABLoadEntry.IsLoadCompleted = true; // 如果判断此时的RefCount为0的话,那么就unload掉该项资源 this.AutoCheckUnloadAsset(rABLoadEntry); }
public IAsyncOperation <AssetLoaderRequest> LoadAllAssetsAsync(string rABName, bool bIsSimulate) { var rRequest = new AssetLoaderRequest(rABName, string.Empty, false, bIsSimulate, true); return(rRequest.Start(this.LoadAssetAsync(rRequest))); }
public IAsyncOperation <AssetLoaderRequest> LoadSceneAsync(string rABName, string rAssetName, LoadSceneMode rLoadSceneMode, bool bIsSimulate) { var rRequest = new AssetLoaderRequest(rABName, rAssetName, rLoadSceneMode, false, bIsSimulate); return(rRequest.Start(this.LoadAssetAsync(rRequest))); }
private IEnumerator LoadAssetObject(AssetLoaderRequest rRequest, ABLoadEntry rAssetLoadEntry, bool bRealLoad) { string rAssetLoadUrl = rAssetLoadEntry.ABPath; if (rRequest.IsSimulate) { Debug.Log("---Simulate Load ab: " + rAssetLoadUrl); #if UNITY_EDITOR if (!string.IsNullOrEmpty(rRequest.AssetName) && !rRequest.IsScene) { if (!rRequest.IsLoadAllAssets) { string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(rAssetLoadEntry.ABName, rRequest.AssetName); if (rAssetPaths.Length == 0) { Debug.LogError("There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName); yield break; } Object rTargetAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(rAssetPaths[0]); rRequest.Asset = rTargetAsset; } else { string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(rAssetLoadEntry.ABName); if (rAssetPaths.Length == 0) { Debug.LogError("There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName); yield break; } rRequest.AllAssets = new Object[rAssetPaths.Length]; for (int i = 0; i < rAssetPaths.Length; i++) { Object rAssetObj = UnityEditor.AssetDatabase.LoadAssetAtPath(rAssetPaths[i], typeof(Object)); if (rAssetObj != null) { rRequest.AllAssets[i] = rAssetObj; } } } } #endif } else { if (bRealLoad) { Debug.Log("---Real Load ab: " + rAssetLoadUrl); // 如果是一个直接的资源,将资源的对象取出来 var rABCreateRequest = AssetBundle.LoadFromFileAsync(rAssetLoadUrl); yield return(rABCreateRequest); rAssetLoadEntry.CacheAsset = rABCreateRequest.assetBundle; } else { Debug.Log("---Load asset: " + rAssetLoadUrl); } // 加载Object if (!string.IsNullOrEmpty(rRequest.AssetName)) { if (!rRequest.IsScene) { if (!rRequest.IsLoadAllAssets) { var rABRequest = rAssetLoadEntry.CacheAsset.LoadAssetAsync(rRequest.AssetName); yield return(rABRequest); rRequest.Asset = rABRequest.asset; } else { yield return(LoadAllAssets_ByAssetbundle(rRequest, rAssetLoadEntry.CacheAsset)); } } else { rAssetLoadEntry.CacheAsset.GetAllScenePaths(); } } } }
public IEnumerator StartLoadSceneAsync(AssetBundleLoad load) { if (!_uninstallable_scene.ContainsKey(_scene_name)) { _uninstallable_scene.Add(_scene_name, false); } _current = 0; _totalCount = 2; //最少有2个,一个ab,一个场景加载 CalculationDependencyCount(_asset_bundle_name, load); TextAsset xmlInfo = load.LoadAsset <TextAsset>(_xml_asset_bundle_name); Dictionary <XmlNode, XmlNodeList> sceneObjects = GetSceneObjects(xmlInfo); AssetBundleLoaderRequest abRequest = AssetBundleLoaderRequest.New(_asset_bundle_name); load.LoadSingleAssetBundleAsync(abRequest); while (!abRequest.IsDone) { yield return(null); } UpdateProgress(); // 加载场景依赖项 yield return(LoadDependency(_asset_bundle_name, load)); yield return(SceneManager.LoadSceneAsync(_scene_name, _load_mode)); UpdateProgress(); // 复位Object对象 if (sceneObjects != null) { var itr = sceneObjects.Keys.GetEnumerator(); while (itr.MoveNext()) // 遍历Object的节点 { XmlElement objectNode = itr.Current as XmlElement; string objectPath = objectNode.GetAttribute("Path"); string objectName = objectNode.GetAttribute("Name"); AssetLoaderRequest request = AssetLoaderRequest.New(objectPath); load.LoadAssetAsync <Object>(request); while (!request.IsDone) { yield return(null); } UpdateProgress(); GameObject gameObject = (GameObject)GameObject.Instantiate(request.asset); gameObject.name = objectName; foreach (XmlElement el in objectNode.ChildNodes) { float x = float.Parse(el.GetAttribute("X")); float y = float.Parse(el.GetAttribute("Y")); float z = float.Parse(el.GetAttribute("Z")); if (el.Name.Equals("Position")) { gameObject.transform.position = new Vector3(x, y, z); } else if (el.Name.Equals("Rotate")) { gameObject.transform.eulerAngles = new Vector3(x, y, z); } else if (el.Name.Equals("Scale")) { gameObject.transform.localScale = new Vector3(x, y, z); } } } itr.Dispose(); } UpdateProgress(); IsDone = true; if (loadCompleted != null) { loadCompleted(this); } _uninstallable_scene[_scene_name] = true; }
private void LoadAllAssets_ByAssetbundle_Sync(AssetLoaderRequest rRequest, AssetBundle rAssetbundle) { rRequest.AllAssets = rAssetbundle.LoadAllAssets(); }
private void LoadAssetObjectSync(AssetLoaderRequest rRequest, ABLoadEntry rAssetLoadEntry) { string rAssetLoadUrl = rAssetLoadEntry.ABPath; if (rRequest.IsSimulate) { Debug.Log("---Simulate Load ab: " + rAssetLoadUrl); #if UNITY_EDITOR if (!string.IsNullOrEmpty(rRequest.AssetName) && !rRequest.IsScene) { if (!rRequest.IsLoadAllAssets) { string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(rAssetLoadEntry.ABName, rRequest.AssetName); if (rAssetPaths.Length == 0) { Debug.LogError("---There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName); return; } Object rTargetAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(rAssetPaths[0]); rRequest.Asset = rTargetAsset; } else { string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(rAssetLoadEntry.ABName); if (rAssetPaths.Length == 0) { Debug.LogError("---There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName); return; } rRequest.AllAssets = new Object[rAssetPaths.Length]; for (int i = 0; i < rAssetPaths.Length; i++) { Object rAssetObj = UnityEditor.AssetDatabase.LoadAssetAtPath(rAssetPaths[i], typeof(Object)); if (rAssetObj != null) { rRequest.AllAssets[i] = rAssetObj; } } } } else { UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode( rRequest.AssetName, new LoadSceneParameters() { loadSceneMode = rRequest.SceneMode }); string rSceneName = Path.GetFileNameWithoutExtension(rRequest.AssetName); rRequest.Scene = SceneManager.GetSceneByName(rSceneName); SceneManager.SetActiveScene(rRequest.Scene); } #endif } else { if (rAssetLoadEntry.CacheAsset == null) { Debug.Log("---Real Load ab: " + rAssetLoadUrl); // 如果是一个直接的资源,将资源的对象取出来 rAssetLoadEntry.CacheAsset = AssetBundle.LoadFromFile(rAssetLoadUrl); } else { Debug.Log("---Load asset: " + rAssetLoadUrl); } // 加载Object if (!string.IsNullOrEmpty(rRequest.AssetName)) { if (!rRequest.IsScene) { if (!rRequest.IsLoadAllAssets) { rRequest.Asset = rAssetLoadEntry.CacheAsset.LoadAsset(rRequest.AssetName); } else { LoadAllAssets_ByAssetbundle_Sync(rRequest, rAssetLoadEntry.CacheAsset); } } else { rAssetLoadEntry.CacheAsset.GetAllScenePaths(); string rSceneName = Path.GetFileNameWithoutExtension(rRequest.AssetName); SceneManager.LoadScene(rSceneName, rRequest.SceneMode); rRequest.Scene = SceneManager.GetSceneByName(rSceneName); SceneManager.SetActiveScene(rRequest.Scene); } } } }