//同步获取bundle中的全部资源 public Object[] GetAssets(string bundle, string requester, System.Type assetType) { if (bundle == string.Empty) { return(null); } #if UNITY_EDITOR if (editorMode) { string path = string.Format("{0}{1}", ResourceConfig.AssetBundlePathPrefix, bundle); return(GetAssetsInEditorMode(path, assetType)); } #endif AssetBundleInfoNode node = Get(bundle, true); if (node == null) { Debug.LogError(string.Format("尝试获取 {0} 全部资源失败,没有这个bundle", bundle)); return(null); } var assetRequested = GetAssetRequested(requester); return(assetRequested.GetAssets(node, assetType, requester)); }
//直接clone Prefab public GameObject ClonePrefab(AssetBundleInfoNode assetBundleInfoNode, string asset, string requester) { PrefabAsset assetNode = assetBundleInfoNode.ClonePrefab(asset, requester); if (assetNode == null) { return(null); } this.requester = requester; if (prefabRequests == null) { prefabRequests = new Dictionary <string, List <PrefabAsset> >(System.StringComparer.CurrentCultureIgnoreCase); } List <PrefabAsset> list = null; if (!prefabRequests.TryGetValue(assetBundleInfoNode.bundleName, out list)) { list = new List <PrefabAsset>(); prefabRequests.Add(assetBundleInfoNode.bundleName, list); } list.Add(assetNode); return(assetNode.clone); }
//同步获取资源 public Object GetAsset(string bundle, string asset, System.Type assetType, string requester) { if (bundle == string.Empty || asset == string.Empty || assetType == null) { return(null); } #if UNITY_EDITOR if (editorMode) { return(GetAssetInEditorMode(bundle, asset, assetType)); } #endif AssetBundleInfoNode node = Get(bundle, true); if (node == null) { return(null); } var assetRequested = GetAssetRequested(requester); return(assetRequested.GetAsset(node, asset, assetType, requester)); }
//尝试获取bundle节点 public AssetBundleInfoNode Get(string abName, bool considerRecycleBin) { AssetBundleInfoNode node = null; if (considerRecycleBin) { var recycleBinItem = GetFromRecycleBin(abName); if (recycleBinItem != null) { node = recycleBinItem.Cancel(); } if (node != null) { return(node); } } if (assetBundleInfoNodes.TryGetValue(abName, out node)) { return(node); } else { Debug.LogError(string.Format("Get bundle info ==>> {0} <<== failed!!!", abName)); return(null); } }
public static AssetAsyncRequest CreateBundleRequest( string bundleName, AssetBundleInfoNode assetBundleInfoNode, System.Action <string> bundleLoadedCallback, Dictionary <string, UnityEngine.Object> assetDic) { if (assetBundleInfoNode == null || string.IsNullOrEmpty(bundleName)) { Debug.LogError("Create asset request failed ::: Bundle name or asset bundle is null"); return(null); } AssetAsyncRequest request = new AssetAsyncRequest(); request.Reset(); request.assetRequestType = AssetRequestType.All; request.bundleName = bundleName; request.assetBundleInfoNode = assetBundleInfoNode; request.bundleLoadedCallback = bundleLoadedCallback; request.assetDic = assetDic; return(request); }
//直接clone Prefab public GameObject ClonePrefab(string bundle, string asset, string requester) { if (bundle == string.Empty || asset == string.Empty || string.IsNullOrEmpty(requester)) { Debug.LogError(string.Format("克隆资源出错:{0} -> {1} from {2}", bundle, asset, requester.ToString())); return(null); } //本地下请求资源 #if UNITY_EDITOR if (editorMode) { return(CloneGameObjInEditorMode(bundle, asset, requester)); } #endif AssetBundleInfoNode node = Get(bundle, true); if (node == null) { Debug.LogError(string.Format("clone {0} 失败,没有这个bundle", bundle)); return(null); } var assetRequested = GetAssetRequested(requester); return(assetRequested.ClonePrefab(node, asset, requester)); }
/// <summary> /// 异步请求bundle下所有资源 /// </summary> /// <param name="bundles">bundle数组</param> /// <param name="requester">请求者</param> /// <param name="bundleCompleteCallback">加载完毕回调</param> /// <param name="assetsDic">资源dic</param> /// <returns></returns> public bool RequestAllAssetsAsync( string[] bundles, string requester, System.Action <string> bundleCompleteCallback, Dictionary <string, UnityEngine.Object> assetsDic) { if (bundles == null || bundles.Length == 0) { return(false); } if (editorMode) { //编辑器模式下不做检测,直接认为存在 foreach (var item in bundles) { bundleCompleteCallback(item); } return(true); } bool error = true; //使用bundle方式请求全部资源 for (int i = 0; i < bundles.Length; ++i) { AssetBundleInfoNode node = Get(bundles[i], true); if (node != null) { //已经全部加载过了 if (node.allLoaded && bundleCompleteCallback != null) { bundleCompleteCallback(bundles[i]); } //可能并没有全部加载,开始加载吧 else { AppendRequest(bundles[i], node, null, null, null, requester, bundleCompleteCallback, null, null, assetsDic); } } else { error = false; } } return(error); }
private void Reset() { requester = null; assetBundleInfoNode = null; bundleName = null; assetNameArray = null; assetType = null; assetTypeArray = null; bundleLoadedCallback = null; assetsLoadedCallbackSameType = null; assetsLoadedCallback = null; assetDic = null; markCancel = false; error = false; }
public AssetBundle GetAssetBundle(string bundle, string requester) { if (editorMode) { Debug.LogWarning("Get asset bundle failed. It's in editor mode now."); return(null); } AssetBundleInfoNode node = Get(bundle, true); if (node == null) { Debug.LogError(string.Format("Get Asset Bundle {0} failed", bundle)); return(null); } return(node.GetAssetBundle(requester)); }
/// <summary> /// 异步请求资源(同种类型) /// </summary> /// <param name="bundle">bundle名字</param> /// <param name="assets">资源名字</param> /// <param name="types">资源类型</param> /// <param name="requester">请求者</param> /// <param name="assetsCompleteCallback">资源加载完毕回调</param> /// <param name="assetsDic">存放资源的映射</param> /// <returns></returns> public bool RequestAssetsAsync( string bundle, string[] assets, System.Type type, string requester, System.Action <string, string[], System.Type> assetsCompleteCallback, Dictionary <string, Object> assetsDic) { if (string.IsNullOrEmpty(bundle) || assets == null || type == null) { return(false); } //本地下请求资源 if (editorMode) { //编辑器模式下不做检测,直接认为存在 assetsCompleteCallback(bundle, assets, type); return(true); } AssetBundleInfoNode node = Get(bundle, true); if (node != null) { if (!node.CheckAssetInThisBundle(assets)) { return(false); } if (node.allLoaded && assetsCompleteCallback != null) { assetsCompleteCallback(bundle, assets, type); } else { AppendRequest(bundle, node, assets, type, null, requester, null, null, assetsCompleteCallback, assetsDic); } return(true); } return(false); }
//同步获取bundle中的全部资源 public Object[] GetAssets(AssetBundleInfoNode assetBundleInfoNode, System.Type assetType, string requester) { var assets = assetBundleInfoNode.GetAssets(requester, assetType); if (assets != null && assets.Length > 0) { this.requester = requester; if (originalRequests == null) { originalRequests = new HashSet <string>(System.StringComparer.CurrentCultureIgnoreCase); } if (!originalRequests.Contains(assetBundleInfoNode.bundleName)) { originalRequests.Add(assetBundleInfoNode.bundleName); } } return(assets); }
//直接获取AssetBundle public AssetBundle GetAssetBundle(AssetBundleInfoNode assetBundleInfoNode, string requester) { AssetBundle assetBundle = assetBundleInfoNode.GetAssetBundle(requester); if (assetBundle != null) { this.requester = requester; if (originalRequests == null) { originalRequests = new HashSet <string>(System.StringComparer.CurrentCultureIgnoreCase); } if (!originalRequests.Contains(assetBundleInfoNode.bundleName)) { originalRequests.Add(assetBundleInfoNode.bundleName); } } return(assetBundle); }
//加入回收站,等待回收 public void PushToRecycleBin(AssetBundleInfoNode assetBundleInfoNode) { if (assetBundleInfoNode == null) { return; } if (CheckIsInAsyncList(assetBundleInfoNode)) { Debug.LogWarning(string.Format("Push to recycle bin failed.In async list ===> {0}", assetBundleInfoNode.bundleName)); return; } RecycleBinItem recycleBinItem = RecycleBinItem.Get(); recycleBinItem.assetBundleInfoNode = assetBundleInfoNode; recycleBinItem.timeStamp = Time.unscaledTime; //记录时间,用于排序 recycleBin.Add(recycleBinItem); }
private bool CheckIsInAsyncList(AssetBundleInfoNode infoNode) { for (int i = asyncWaitingList.Count - 1; i >= 0; --i) { if (string.Compare(infoNode.bundleName, asyncWaitingList[i].bundleName, true) == 0) { return(true); } } //加载中的标记取消 for (int i = 0; i < asyncLoadingList.Count; ++i) { if (string.Compare(infoNode.bundleName, asyncLoadingList[i].bundleName, true) == 0) { return(true); } } return(false); }
public void SetPrefabClonePoolCapacity(string bundle, string asset, int capacity) { #if UNITY_EDITOR if (editorMode) { return; } #endif if (bundle == string.Empty || asset == string.Empty) { return; } AssetBundleInfoNode node = Get(bundle, true); if (node == null) { Debug.LogError(string.Format("Capackty {0} 失败,没有这个bundle", bundle)); return; } node.SetCloneCapacity(asset, capacity); }
//追加异步请求 private void AppendRequest( string bundleName, AssetBundleInfoNode assetBundleInfoNode, string[] assets, System.Type type, System.Type[] types, string requester, System.Action <string> bundleCallback, System.Action <string, string[], System.Type[]> assetCallback, System.Action <string, string[], System.Type> assetCallbackSameType, Dictionary <string, UnityEngine.Object> assetDic) { AssetAsyncRequest req = null; if (assets != null && type != null) { req = AssetAsyncRequest.CreatePartRequest(bundleName, assetBundleInfoNode, assets, type, assetCallbackSameType, assetDic); } else if (assets != null && types != null) { req = AssetAsyncRequest.CreatePartRequest(bundleName, assetBundleInfoNode, assets, types, assetCallback, assetDic); } else { req = AssetAsyncRequest.CreateBundleRequest(bundleName, assetBundleInfoNode, bundleCallback, assetDic); } //加入异步加载队列 if (req != null) { asyncWaitingList.Add(req); if (DebugMode) { MgrLog(string.Format("Append async request... ==> {0}", asyncWaitingList.Count)); } } }
public static PrefabAssetNode Create(AssetBundleInfoNode infoNode, string assetName) { if (infoNode == null) { Debug.LogError("Create AssetInfoNode failed. Empty infoNode..."); return(null); } GameObject original = infoNode.GetAsset <GameObject>(assetName, string.Format("PrefabAssetNode_{0}", assetName)); if (original == null) { Debug.LogError("Create AssetInfoNode failed. Can not get gameobject asset ==> " + assetName); return(null); } PrefabAssetNode node = nodePool.Get(); node.infoNode = infoNode; node.assetName = assetName; node.originPrefab = original; return(node); }
//归还所有 public void ReturnAll() { //归还对prefab的占用 if (prefabRequests != null && prefabRequests.Count > 0) { foreach (var item in prefabRequests) { if (item.Value != null && item.Value.Count > 0) { for (int i = 0; i < item.Value.Count; i++) { item.Value[i].Requester = string.Empty; } } item.Value.Clear(); } prefabRequests.Clear(); } //归还对original的占用 if (originalRequests != null && originalRequests.Count > 0) { AssetBundleInfoNode node = null; foreach (var bundle in originalRequests) { node = ResourceManager.Instance.Get(bundle, false); if (node != null) { node.RemoveOriginalAssetRequester(requester); } } originalRequests.Clear(); } CheckEmpty(); }
public static AssetAsyncRequest CreatePartRequest( string bundleName, AssetBundleInfoNode assetBundleInfoNode, string[] assetNameArray, System.Type assetType, System.Action <string, string[], System.Type> assetsLoadedCallbackSameType, Dictionary <string, UnityEngine.Object> assetDic) { if (assetBundleInfoNode == null || string.IsNullOrEmpty(bundleName)) { Debug.LogError("Create asset request failed ::: Bundle name or asset bundle is null"); return(null); } if (assetNameArray == null || assetType == null) { Debug.LogError("Create asset request failed"); return(null); } AssetAsyncRequest request = new AssetAsyncRequest(); request.Reset(); request.assetRequestType = AssetRequestType.Part_SameType; request.bundleName = bundleName; request.assetBundleInfoNode = assetBundleInfoNode; request.assetNameArray = assetNameArray; request.assetType = assetType; request.assetsLoadedCallbackSameType = assetsLoadedCallbackSameType; request.assetDic = assetDic; return(null); }
private void SetupBundleRelationships() { var bundle = AssetBundle.LoadFromFile(ManifestPath); if (bundle) { AssetBundleManifest abManifest = bundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); if (abManifest == null) { Debug.LogError("Setup bundle info nodes failed, none manifest file... => " + ManifestPath); return; } //setup relationship assetBundleInfoNodes.Clear(); string[] assets = abManifest.GetAllAssetBundles(); foreach (var item in assets) { if (item.Contains("lua/")) { continue; } //Create bundle info node if (!assetBundleInfoNodes.ContainsKey(item.ToLower())) { AssetBundleInfoNode node = new AssetBundleInfoNode(item); assetBundleInfoNodes.Add(item.ToLower(), node); } } foreach (var item in assetBundleInfoNodes) { //Get dependencies string[] dependencies = abManifest.GetDirectDependencies(item.Key); foreach (var dependence in dependencies) { //Ignore shader bundle if (dependence.Contains("shaders.unity3d")) { continue; } if (assetBundleInfoNodes.ContainsKey(dependence.ToLower())) { AssetBundleInfoNode son = assetBundleInfoNodes[dependence]; AssetBundleInfoNode parent = item.Value; if (parent.dependentNode.Contains(son) || son.beDependentNode.Contains(parent)) { continue; } else { //Link son.beDependentNode.Add(parent); parent.dependentNode.Add(son); } } else { Debug.LogError(string.Format("Bundle node error: can not find out dependence node, file name : {0}", dependence)); } } } bundle.Unload(true); WarmUpShaders(); Debug.Log("======>> Bundle relationship compelted!! <<=========="); } else { Debug.LogError("Get manifest failed!! => " + ManifestPath); } }