2using System.Collections.Generic;
16 List<string> availableAssetBundles =
new List<string>();
17 Dictionary<string, AssetBundle> loadedAssetBundles =
new Dictionary<string, AssetBundle>();
35 string full_path = pb_FileUtility.GetFullPath(path);
68 private void _RegisterAssetBundle(
string full_path)
70 if(!availableAssetBundles.Contains(full_path))
71 availableAssetBundles.Add(full_path);
74 private AssetBundle _LoadAssetBundle(
string full_path)
76 AssetBundle bundle =
null;
78 if(!loadedAssetBundles.TryGetValue(full_path, out bundle))
80 _RegisterAssetBundle(full_path);
82#if UNITY_5_2 || UNITY_5_1
83 bundle = AssetBundle.CreateFromFile(full_path);
85 bundle = AssetBundle.LoadFromFile(full_path);
87 loadedAssetBundles.Add(full_path, bundle);
93 private void _UnloadAssetBundle(
string full_path,
bool unloadAllLoadedObjects)
95 if(loadedAssetBundles.ContainsKey(full_path))
97 loadedAssetBundles[full_path].Unload(unloadAllLoadedObjects);
98 loadedAssetBundles.Remove(full_path);
102 private void _UnloadAssetBundle(AssetBundle bundle)
104 foreach(KeyValuePair<string, AssetBundle> bundles
in loadedAssetBundles)
106 if(bundles.Value == bundle)
108 loadedAssetBundles.Remove(bundles.Key);
114 private bool _GetAssetPath<T>(T asset, out pb_AssetBundlePath path) where T :
UnityEngine.Object
116 foreach(
string bundle_path
in availableAssetBundles)
120 AssetBundle bundle = _LoadAssetBundle(bundle_path);
122 foreach(KeyValuePair<string, UnityEngine.Object> kvp
in LoadBundleAssetsWithPaths(bundle))
124 if(kvp.Value.GetType() == typeof(T))
126 path =
new pb_AssetBundlePath(bundle_path, kvp.Key);
134 catch(System.Exception e)
136 Debug.LogWarning(
"Failed loading AssetBundle: " + bundle_path +
"\n" + e.ToString());
145 private Dictionary<string,
UnityEngine.Object> LoadBundleAssetsWithPaths(AssetBundle bundle)
149 foreach(
string str
in bundle.GetAllAssetNames())
151 dic.Add(str, bundle.LoadAsset(str));
157 private AssetBundle _LoadAssetBundleWithName(
string name)
160 foreach(
string str
in availableAssetBundles)
162 if(
string.Compare( System.IO.Path.GetFileName(str), name,
true) == 0 )
164 return _LoadAssetBundle(str);
169 foreach(
string dir
in pb_Config.AssetBundle_SearchDirectories)
171 string[] paths =
Directory.GetFiles(dir, name, SearchOption.AllDirectories);
175 foreach(
string path
in paths)
179 AssetBundle bundle = _LoadAssetBundle(path);
190 private T _LoadAsset<T>(pb_AssetBundlePath path) where T :
UnityEngine.Object
192 AssetBundle bundle = _LoadAssetBundleWithName(path.assetBundleName);
197 if( bundle.Contains(path.filePath) )
199 T obj = bundle.LoadAsset<T>(path.filePath);
override bool dontDestroyOnLoad
static AssetBundle LoadAssetBundle(string path)
static void RegisterAssetBundle(string path)
static bool GetAssetPath< T >(T asset, out pb_AssetBundlePath path)
static AssetBundle LoadAssetBundleWithName(string name)
static T LoadAsset< T >(pb_AssetBundlePath path)