public static void SetAssetBundleNameByDirectory(string strPath, Object obj) { if (obj == null) { return; } AssetImporter assetImport = null; string strDirectory = Path.GetDirectoryName(strPath); strPath = strPath.Replace(" ", ""); assetImport = AssetImporter.GetAtPath(strPath); strDirectory = strDirectory.Replace("\\", "/"); string strName = ""; if (strDirectory.StartsWith("Assets/RawResources/bootstrap")) { strName = "bootstrap/bootstrap"; } else { string[] dirList = strDirectory.Split('/'); if (strDirectory == "Assets/RawResources/Scene/Prefab") { strDirectory = "scene"; } else { strDirectory = dirList[dirList.Length - 1]; } strName = obj.name.Replace(" ", ""); strName = strDirectory + "/" + strName; strName = strName.ToLower(); } if (assetImport != null) { assetImport.assetBundleName = strName + ".u3d"; } if (PrefabUtility.IsPartOfPrefabAsset(obj)) { GameObject prefabRoot = PrefabUtility.LoadPrefabContents(strPath); try { AssetBundleReference bundleRef = prefabRoot.AddMissingComponent <AssetBundleReference>(); bundleRef.assetBundleName = strName + ".u3d"; PrefabUtility.SaveAsPrefabAsset(prefabRoot, strPath); } finally { PrefabUtility.UnloadPrefabContents(prefabRoot); } } }
public IEnumerator TestYeildLoadAssetBundle() { BundleLoaderEnumerator bundleLoaderEnumerator = m_AssetManager.YieldLoadAssetBundle("prefabs/myprefab", false); yield return(bundleLoaderEnumerator); AssetBundleReference result = bundleLoaderEnumerator.assetBundleReference; Assert.AreNotEqual(result, null); bundleLoaderEnumerator.Dispose(); }
public void SyncLoad() { AssetReference ar = m_AssetManager.LoadAssetSync("ArtResources/Prefabs/MyPrefab.prefab"); Assert.AreNotEqual(ar, null); AssetBundleReference abr = m_AssetManager.LoadAssetBundleSync("mymaterial"); #if !UNITY_EDITOR || ASSET_BUNDLE_LOADER Assert.AreNotEqual(abr, null); #else Assert.AreEqual(abr, null); #endif }
private void LoadAsset <T>(string id, AssetBundle bundle, AssetBundleReference reference, Action <T> loaded) where T : Object { var request = bundle.LoadAssetAsync <T>(id); request.completed += op => { if ((reference == null) || (--reference.Counter == 0)) { bundle.Unload(false); } _cachedAssets[id] = request.asset; loaded((T)request.asset); }; }
public static GameObject InstantiateGameObject(string assetBundleName, string assetName) { UnityEngine.Object obj = LoadAsset(assetBundleName, assetName, Typeof_GameObject, true); if (obj != null) { GameObject go = GameUtil.Instantiate(obj) as GameObject; if (go != null) { AssetBundleReference bundleRef = go.AddMissingComponent <AssetBundleReference>(); bundleRef.assetBundleName = assetBundleName; } return(go); } return(null); }
public RaftMod() { if (mods.ContainsKey(Metadata.ModName)) { throw new DuplicateModException(Metadata.ModName); } mods[Metadata.ModName] = this; if (defaultAssetBundle == null) { defaultAssetBundle = "./mods/" + Metadata.ModName + "/" + Metadata.ModName + "." + Metadata.ModVersion; } RegisterAssetBundle(assetBundle = new AssetBundleReference(defaultAssetBundle)); }
public IEnumerator TestLoadAssetBundle() { AssetBundleReference result = null; bool isDone = false; m_AssetManager.LoadAssetBundle("prefabs/myprefab", false, (abr) => { isDone = true; result = abr; }); while (!isDone) { yield return(null); } Assert.AreNotEqual(result, null); }
public override GameObject GetGameObject() { if (m_Request != null && m_Request.isDone) { if (m_Request.asset == null) { return(null); } GameObject go = GameUtil.Instantiate(m_Request.asset) as GameObject; if (go != null) { AssetBundleReference bundleRef = go.AddMissingComponent <AssetBundleReference>(); bundleRef.assetBundleName = m_AssetBundleName; } return(go); } return(null); }
public static void SetAssetBundlesName() { Object[] objList = Selection.objects; AssetImporter assetImport = null; foreach (Object obj in objList) { assetImport = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(obj)); string strName = obj.name.Replace(" ", ""); assetImport.assetBundleName = strName + ".u3d"; if (obj is GameObject) { GameObject go = obj as GameObject; AssetBundleReference bundleRef = go.AddMissingComponent <AssetBundleReference>(); bundleRef.assetBundleName = strName + ".u3d"; } } }
public void Load <T>(string id, Action <T> loaded) where T : Object { if (_cachedAssets.TryGetValue(id, out var asset)) { loaded((T)asset); return; } if ((_objectBundleMapping != null) && _objectBundleMapping.TryGetValue(id, out var bundleId)) { if (!_assetBundleReferences.TryGetValue(bundleId, out var reference)) { var request = AssetBundle.LoadFromFileAsync(Path.Combine(GetBundleRoot(Application.platform), bundleId)); if (request.isDone) { LoadAsset(id, request.assetBundle, null, loaded); } else { reference = new AssetBundleReference(); reference.Loaded += (sender, bundle) => LoadAsset(id, bundle, reference, loaded); _assetBundleReferences[bundleId] = reference; request.completed += op => { _assetBundleReferences.Remove(bundleId); reference.Invoke(request.assetBundle); }; } } else { reference.Loaded += (sender, bundle) => LoadAsset(id, bundle, reference, loaded); reference.Counter++; } } else { var request = AssetBundle.LoadFromFileAsync(Path.Combine(GetBundleRoot(Application.platform), id)); request.completed += op => LoadAsset(id, request.assetBundle, null, loaded); } }
public IEnumerator TestLoadAssetBundle() { string bundleName = "prefabs/myprefab"; AssetBundleReference result = null; bool isDone = false; m_AssetManager.LoadAssetBundle(bundleName, false, (abr) => { isDone = true; result = abr; }); while (!isDone) { yield return(null); } Assert.AreNotEqual(result, null); #if ASSETMANAGE_BUNDLE_CACHE_ON //check file Exists string filepath = AssetPaths.FullPathForFilename(bundleName); Assert.AreEqual(System.IO.File.Exists(filepath), true); //check cache info RequestManager rm = m_AssetManager.requestManager as RequestManager; if (rm != null && rm.cacheManager != null) { Assert.AreNotEqual(rm.cacheManager.cacheItems, null); CacheItem cacheItem = null; if (rm.cacheManager.cacheItems.TryGetValue(bundleName, out cacheItem)) { Assert.AreEqual(string.IsNullOrEmpty(cacheItem.hash), false); } else { Assert.Fail("Can't find cache item " + bundleName); } } #endif }
/// <summary> /// Loads the asset. /// </summary> /// <param name="resourceReference"> /// The resource reference for the asset. /// </param> /// <param name="resourceType"> /// The type of asset. /// </param> /// <param name="async"> /// Whether to load the asset asynchronously or not. If <c>true</c>, the asset bundle must already be loaded. /// </param> private IEnumerator AssetBundleAdaptorImpl(IResourceReference resourceReference, System.Type resourceType, bool async) { if (resourceReference == null) { throw new System.ArgumentNullException("resourceReference"); } else if (string.IsNullOrEmpty(resourceReference.AssetBundleName)) { throw new System.ArgumentException("resourceReference.AssetBundleName cannot be null.", "resourceReference"); } m_Asset = null; m_Error = null; m_KeepWaiting = true; if (async) { AssetBundleLoaderRequest assetBundleLoaderRequest; assetBundleLoaderRequest = AssetBundleLoader.LoadAsync(resourceReference.AssetBundleName); yield return(assetBundleLoaderRequest); if (!string.IsNullOrEmpty(assetBundleLoaderRequest.error)) { m_Error = assetBundleLoaderRequest.error; m_KeepWaiting = false; assetBundleLoaderRequest.Dispose(); yield break; } AssetBundleRequest assetBundleRequest; assetBundleRequest = assetBundleLoaderRequest.assetBundle.LoadAssetAsync(resourceReference.AssetPath, resourceType); yield return(assetBundleRequest); if (assetBundleRequest.asset == null) { m_Error = string.Format( "Could not load asset from asset bundle: asset bundle name = {0}, asset path = {1}, asset type = {2}", resourceReference.AssetBundleName, resourceReference.AssetPath, resourceType ); m_KeepWaiting = false; assetBundleLoaderRequest.Dispose(); yield break; } m_Asset = assetBundleRequest.asset; m_KeepWaiting = false; assetBundleLoaderRequest.Dispose(); yield break; } else { AssetBundleReference assetBundleReference; assetBundleReference = AssetBundleReference.FindReference(resourceReference.AssetBundleName); if (assetBundleReference == null) { m_Error = string.Format( "Could not load asset from asset bundle: asset bundle not loaded: {0}", resourceReference.AssetBundleName ); m_KeepWaiting = false; yield break; } Object asset; asset = assetBundleReference.assetBundle.LoadAsset(resourceReference.AssetPath, resourceType); if (asset == null) { m_Error = string.Format( "Could not load asset from asset bundle: asset bundle name = {0}, asset path = {1}, asset type = {2}", resourceReference.AssetBundleName, resourceReference.AssetPath, resourceType ); m_KeepWaiting = false; yield break; } m_Asset = asset; m_KeepWaiting = false; yield break; } }
//尝试加载一个AB //加载过了直接返回,没有加载过,会执行异步加载 //如果加载的是依赖,会增加依赖计数 private IEnumerator _LoadAssetBundleInternalAsync(string abPath, bool isDep) { AssetBundleReference abRef; if (_loadedAssetBundles.TryGetValue(abPath, out abRef)) { //已经加载了,持有一个依赖引用 if (isDep) { abRef.Depend(); } yield break; } //如果在加载队列里 if (_loadingAssetBundles.Contains(abPath)) { //等待加载完成 while (_loadingAssetBundles.Contains(abPath)) { yield return(null); } //加载完成了,但是可能加载失败,获取一次 if (isDep) { if (_loadedAssetBundles.TryGetValue(abPath, out abRef)) { //持有一个依赖引用 abRef.Depend(); } } yield break; } _loadingAssetBundles.Add(abPath); AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(GetFullPath(abPath)); yield return(request); //加载完成s if (request.assetBundle == null) { if (_loadingAssetBundles.Contains(abPath)) { _loadingAssetBundles.Remove(abPath); } yield break; } abRef = new AssetBundleReference(); abRef._assetbundle = request.assetBundle; abRef._path = abPath; _loadedAssetBundles.Add(abPath, abRef); //如果还在加载队列里,则记录依赖引用;不在加载队列里,说明提交底层加载后,AB却被Unload了 if (_loadingAssetBundles.Contains(abPath)) { if (isDep) { abRef.Depend(); } _loadingAssetBundles.Remove(abPath); } }
private IEnumerator NavigateToContentImpl() { if (SceneNavigator.Instance) { if (WillNavigateToContent != null) { WillNavigateToContent(this.ContentInfo); } CreateLoadingSpinner( ProjectInfo.Instance.NavigateToContentLoadingSpinner, () => { return((int)(this.ProgressReport != null ? this.ProgressReport.TotalProgress * 100f : 0)); }); // go to the navigate to content scene SceneNavigator.Instance.NavigateToScene( ProjectInfo.Instance.NavigateToContentScene, Instantiate(ProjectInfo.Instance.NavigateToContentTransition), Instantiate(ProjectInfo.Instance.NavigateToContentTransition), null, true, true ); while (SceneNavigator.Instance.IsBusy) { yield return(null); } // unload the home scene yield return(Resources.UnloadUnusedAssets()); // save auto rotation and orientation settings SaveScreenAutoRotationAndOrientation(); // event this.ContentInfo.OnProjectNavigatorWillActivateContent(); // apply the content settings ApplyContentSettings(this.ContentInfo); #if !UNITY_EDITOR AssetBundleAdaptorType resourceContentAssetBundleAdaptorType = AssetBundleAdaptorMap.Instance.GetAdaptorType(this.ContentInfo.ResourceAssetBundleName); AssetBundleAdaptorType sceneContentAssetBundleAdaptorType = AssetBundleAdaptorMap.Instance.GetAdaptorType(this.ContentInfo.SceneAssetBundleName); if ((resourceContentAssetBundleAdaptorType == AssetBundleAdaptorType.OnDemandResources || resourceContentAssetBundleAdaptorType == AssetBundleAdaptorType.AssetBundleServer) || (sceneContentAssetBundleAdaptorType == AssetBundleAdaptorType.OnDemandResources || sceneContentAssetBundleAdaptorType == AssetBundleAdaptorType.AssetBundleServer)) { // Query to see if resources that we need to load are already downloaded and available using (var resourceQueryYieldInstruction = new ResourceQueryYieldInstruction(this, this.ContentInfo.ResourceAssetBundleName, this.ContentInfo.SceneAssetBundleName)) { Debug.Log("ProjectNavigator-> Querying For Content Resource Availability.", DebugContext.SagoApp); yield return(resourceQueryYieldInstruction); // If resource and scene are not already downloaded and available we need to test internet reachability before before start downloading. if (!resourceQueryYieldInstruction.IsResourceAvailable) { // Testing internet reachability and stop loading ODR if there is any error. using (InternetReachabilityYieldInstruction reachabilityYieldInstruction = new InternetReachabilityYieldInstruction(this)) { yield return(reachabilityYieldInstruction); if (!reachabilityYieldInstruction.IsInternetReachable) { this.Error = reachabilityYieldInstruction.Error; NavigateToError(this.Error, "Internet is not reachable."); // Since internet is not reachable we need to stop all remaining processes. yield break; } } } } } #endif if (NavigateToContentWillLoadAssetBundles != null) { NavigateToContentWillLoadAssetBundles(this.ContentInfo); } // load the content asset bundles if (AssetBundleOptions.UseAssetBundlesInEditor) { Debug.LogFormat(DebugContext.SagoApp, "ProjectNavigator-> Finding or creating reference to asset bundle: {0}", this.ContentInfo.ResourceAssetBundleName); this.ResourceAssetBundle = AssetBundleReference.FindOrCreateReference(this.ContentInfo.ResourceAssetBundleName); this.ResourceAssetBundle.Retain(); this.ProgressReport.Index = 0; this.ProgressReport.Count = 3; this.ProgressReport.Item = new LoadAssetBundleProgressReportItem(this.ResourceAssetBundle); yield return(this.ResourceAssetBundle); Debug.LogFormat(DebugContext.SagoApp, "ProjectNavigator-> Completed finding or creating reference to asset bundle: {0}", this.ContentInfo.ResourceAssetBundleName); if (!string.IsNullOrEmpty(this.ResourceAssetBundle.error)) { Debug.LogError(this.ResourceAssetBundle.error, DebugContext.SagoApp); if (string.Equals(this.ResourceAssetBundle.error, LowDiskSpaceAssetBundleAdaptor.LowDiskSpaceError)) { this.Error = ProjectNavigatorError.LowDiskSpace; } else if (string.Equals(this.ResourceAssetBundle.error, AssetBundleAdaptorError.NoInternet)) { this.Error = ProjectNavigatorError.OdrErrorNoInternet; } else { this.Error = ProjectNavigatorError.OdrErrorUnknown; } if (NavigateToContentDidLoadAssetBundles != null) { NavigateToContentDidLoadAssetBundles(this.ContentInfo, false); } NavigateToError(this.Error, this.ResourceAssetBundle.error); yield break; } Debug.LogFormat(DebugContext.SagoApp, "ProjectNavigator-> Finding or creating reference to asset bundle: {0}", this.ContentInfo.SceneAssetBundleName); this.SceneAssetBundle = AssetBundleReference.FindOrCreateReference(this.ContentInfo.SceneAssetBundleName); this.SceneAssetBundle.Retain(); this.ProgressReport.Index = 1; this.ProgressReport.Item = new LoadAssetBundleProgressReportItem(this.SceneAssetBundle); yield return(this.SceneAssetBundle); Debug.LogFormat(DebugContext.SagoApp, "ProjectNavigator-> Completed finding or creating reference to asset bundle: {0}", this.ContentInfo.SceneAssetBundleName); if (!string.IsNullOrEmpty(this.SceneAssetBundle.error)) { Debug.LogError(this.SceneAssetBundle.error, DebugContext.SagoApp); if (string.Equals(this.SceneAssetBundle.error, LowDiskSpaceAssetBundleAdaptor.LowDiskSpaceError)) { this.Error = ProjectNavigatorError.LowDiskSpace; } else if (string.Equals(this.SceneAssetBundle.error, AssetBundleAdaptorError.NoInternet)) { this.Error = ProjectNavigatorError.OdrErrorNoInternet; } else { this.Error = ProjectNavigatorError.OdrErrorUnknown; } if (NavigateToContentDidLoadAssetBundles != null) { NavigateToContentDidLoadAssetBundles(this.ContentInfo, false); } NavigateToError(this.Error, this.SceneAssetBundle.error); yield break; } } if (NavigateToContentDidLoadAssetBundles != null) { NavigateToContentDidLoadAssetBundles(this.ContentInfo, true); } // event this.ContentInfo.OnProjectNavigatorDidActivateContent(); if (NavigateToContentWillLoadMainScene != null) { NavigateToContentWillLoadMainScene(this.ContentInfo); } // load the initial scene SceneNavigator.Instance.LoadScene(this.ContentInfo.MainScene); this.ProgressReport.Index = 2; this.ProgressReport.Item = new LoadSceneProgressReportItem(this.ContentInfo.MainScene); while (!SceneNavigator.Instance.SceneIsLoaded(this.ContentInfo.MainScene)) { yield return(null); } if (NavigateToContentDidLoadMainScene != null) { NavigateToContentDidLoadMainScene(this.ContentInfo, true); } // go to the initial scene SceneNavigator.Instance.NavigateToScene( this.ContentInfo.MainScene, Instantiate(ProjectInfo.Instance.NavigateToContentTransition), Instantiate(ProjectInfo.Instance.NavigateToContentTransition), null, true, true ); while (!SceneNavigator.Instance.IsReady) { yield return(null); } this.ProgressReport.Reset(); // set state this.State = ProjectNavigatorState.Content; if (DidNavigateToContent != null) { DidNavigateToContent(this.ContentInfo); } } }
public LoadAssetBundleProgressReportItem(AssetBundleReference assetBundleReference) { AssetBundleReference = assetBundleReference; }
public void TestLoadAssetBundleSync() { AssetBundleReference result = m_AssetManager.LoadAssetBundleSync("myprefab", false); Assert.AreNotEqual(result, null); }