public Texture GetTexture(string assetName) { string assetPath = GetAssetPathByAssetName <Texture>(assetName); if (String.IsNullOrEmpty(assetName)) { // Debug.LogError("Null!"+assetName); return(null); } assetName = assetName.ToLower(); string bundleName = assetName + BytesExt; AddAssetBundleNameDic(assetPath, bundleName); BundleWrapper bundle = LoadBundle(bundleName); if (bundle.IsEmpty()) { return(null); } string[] names = bundle.GetAllAssetNames(); Texture sp = bundle.LoadAsset <Texture>(names[0]); return(sp); }
IEnumerator AsyncLoadCoroutine(string assetName, Action <SpriteAtlas> callBack) { string[] moduleNameParts = assetName.Split('_'); string moduleFolderName = moduleNameParts[0] + "_" + moduleNameParts[1]; BundleWrapper bundle = LoadBundle(moduleFolderName.ToLower() + AtlasExt); //yield return null; AssetBundleRequest req = bundle.LoadAllAssetsAsync(); if (req == null) { SpriteAtlas atlas1 = LoadAtlas(assetName); yield return(null); callBack(atlas1); yield break; } while (req.isDone == false) { yield return(null); } string[] names = bundle.GetAllAssetNames(); SpriteAtlas atlas = bundle.LoadAsset <SpriteAtlas>(names[0]); //yield return null; callBack(atlas); }
/// <summary> /// 卸载图集 /// </summary> /// <param name="assetName"></param> public void UnloadAtlas(string assetName) { string bundleName = assetName.ToLower() + AtlasExt; BundleWrapper bundle = LoadBundle(bundleName); bundle.Unload(true); _cachedBundles.Remove(bundleName); Debug.Log("<color='#ff5566'>UnloadAtlas " + bundleName + "</color>"); }
public AudioClip GetAudioClip(string assetName) { string assetPath = GetAssetPathByAssetName <AudioClip>(assetName); assetName = assetName.ToLower(); AddAssetBundleNameDic(assetPath, assetName + AudioExt); BundleWrapper bundle = LoadBundle(assetName + AudioExt); if (bundle.IsEmpty()) { return(null); } string[] names = bundle.GetAllAssetNames(); AudioClip ac = bundle.LoadAsset <AudioClip>(names[0]); return(ac); }
public ExternalBehaviorTree GetBehaviorTreeAsset(string assetName) { string assetPath = GetAssetPathByAssetName <TextAsset>(assetName); assetName = assetName.ToLower(); AddAssetBundleNameDic(assetPath, assetName + BytesExt); BundleWrapper bundle = LoadBundle(assetName + BytesExt); if (bundle.IsEmpty()) { return(null); } string[] names = bundle.GetAllAssetNames(); ExternalBehaviorTree ta = bundle.LoadAsset <ExternalBehaviorTree>(names[0]); return(ta); }
/// <summary> /// 加载图集 /// </summary> /// <param name="assetName"></param> /// <returns></returns> public SpriteAtlas LoadAtlas(string assetName) { string[] moduleNameParts = assetName.Split('_'); string moduleFolderName = moduleNameParts[0] + "_" + moduleNameParts[1]; #if UNITY_EDITOR && !USE_BUNDLE string assetPath = "Assets/BundleAssets/UISpriteAtlas/" + moduleFolderName + ".spriteatlas"; SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(assetPath); return(atlas); #else BundleWrapper bundle = LoadBundle(moduleFolderName.ToLower() + AtlasExt); bundle.LoadAllAssets(); string[] names = bundle.GetAllAssetNames(); SpriteAtlas atlas = bundle.LoadAsset <SpriteAtlas>(names[0]); return(atlas); #endif }
public Sprite GetSprite(string assetName) { string assetPath = GetAssetPathByAssetName <Texture>(assetName); assetName = assetName.ToLower(); string bundleName = assetName + BytesExt; AddAssetBundleNameDic(assetPath, bundleName); BundleWrapper bundle = LoadBundle <Sprite>(bundleName); if (bundle == null) { return(null); } string[] names = bundle.GetAllAssetNames(); Sprite sp = bundle.LoadAsset <Sprite>(names[0]); return(sp); }
private BundleWrapper LoadBundle <T>(string bundleName) where T : Object { BundleWrapper bundleWrapper = new BundleWrapper(); AssetBundle bundle = null; if (_cachedBundles.ContainsKey(bundleName)) { bundle = _cachedBundles[bundleName]; bundleWrapper.Init(bundleName, bundle); } else { #if UNITY_EDITOR && !USE_BUNDLE string assetPath = GetAssetPathByBundleName(bundleName); if (!string.IsNullOrEmpty(assetPath)) { if (bundleName.StartsWith("/")) { bundleName = bundleName.Substring(1); } UnityEngine.Object resource = null; resource = AssetDatabase.LoadAssetAtPath <T>(assetPath); if (resource == null) //找不到時用bundleName查找 { resource = GetResourceByBundleNameEditor <T>(bundleName); } bundleWrapper.Init(bundleName, resource); } else { if (bundleName.StartsWith("/")) { bundleName = bundleName.Substring(1); } UnityEngine.Object resource = GetResourceByBundleNameEditor <T>(bundleName); if (resource != null) { bundleWrapper.Init(bundleName, resource); } } //// if (bundleName.StartsWith("/")) //// bundleName = bundleName.Substring(1); //string[] bundles = AssetDatabase.GetAssetPathsFromAssetBundle(bundleName); //if (bundles.Length == 0) //{ // Debug.LogError("bundleName no found:" + bundleName); //} //else //{ // UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<T>(bundles[0]); // bundleWrapper.Init(bundleName, resource); //} #else string path = PathUtil.GetPath(_platformBundlePath + bundleName, AssetLoader.ExternalHotfixPath); bundle = AssetBundle.LoadFromFile(path); if (bundle != null) { _cachedBundles.Add(bundleName, bundle); } bundleWrapper.Init(bundleName, bundle); #endif } #if UNITY_EDITOR && USE_BUNDLE var materials = bundle.LoadAllAssets <Material>(); foreach (var material in materials) { var shaderName = material.shader.name; var newShader = Shader.Find(shaderName); if (newShader != null) { material.shader = newShader; } else { Debug.LogWarning("unable to refresh shader: " + shaderName + " in material " + material.name); } } #endif return(bundleWrapper); }
public T GetAsset <T>(string assetName) where T : UnityEngine.Object { string asset_Path = GetAssetPathByAssetName <T>(assetName); assetName = assetName.ToLower(); UnityEngine.Object obj = null; bool useBundle = false; #if USE_BUNDLE useBundle = true; #endif if (_loadedAssets.ContainsKey(assetName) == false) { string assetKey = assetName + PrefabExt; AddAssetBundleNameDic(asset_Path, assetKey); if (_prefabDict.ContainsKey(assetKey) == false && useBundle) { // Debug.LogError("No Assets Found -> " + assetName); } else { BundleWrapper bundle = LoadBundle(assetKey); #if UNITY_EDITOR && !USE_BUNDLE string[] dps = AssetDatabase.GetAssetBundleDependencies(assetKey, true); LoadDependencies(dps); return((T)bundle.GetResource()); #endif string[] allAssetNames = bundle.GetAllAssetNames(); foreach (var assetPath in allAssetNames) { AssetItem aItem = new AssetItem() { AssetBundle = bundle.GetBundle(), AssetName = assetName, AssetPath = assetPath }; _loadedAssets.Add(assetName, aItem); } string[] dependencies = _manifest.GetAllDependencies(assetKey); LoadDependencies(dependencies); AssetItem item = _loadedAssets[assetName]; obj = item.AssetBundle.LoadAsset <T>(item.AssetPath); } } else { string assetKey = assetName + PrefabExt; string[] dependencies = _manifest.GetAllDependencies(assetKey); LoadDependencies(dependencies); AssetItem item = _loadedAssets[assetName]; obj = item.AssetBundle.LoadAsset <T>(item.AssetPath); } return((T)obj); }