Tanoda
pb_AssetBundles.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5
6namespace GILES
7{
12 public class pb_AssetBundles : pb_MonoBehaviourSingleton<pb_AssetBundles>
13 {
14 public override bool dontDestroyOnLoad { get { return true; } }
15
16 List<string> availableAssetBundles = new List<string>();
17 Dictionary<string, AssetBundle> loadedAssetBundles = new Dictionary<string, AssetBundle>();
18
19#region Interface
20
25 public static void RegisterAssetBundle(string path)
26 {
27 pb_AssetBundles.instance._RegisterAssetBundle(pb_FileUtility.GetFullPath(path));
28 }
29
33 public static AssetBundle LoadAssetBundle(string path)
34 {
35 string full_path = pb_FileUtility.GetFullPath(path);
36 return pb_AssetBundles.instance._LoadAssetBundle(full_path);
37 }
38
43 public static AssetBundle LoadAssetBundleWithName(string name)
44 {
45 return pb_AssetBundles.instance._LoadAssetBundleWithName(name);
46 }
47
52 public static bool GetAssetPath<T>(T asset, out pb_AssetBundlePath path) where T : UnityEngine.Object
53 {
54 return pb_AssetBundles.instance._GetAssetPath<T>(asset, out path);
55 }
56
60 public static T LoadAsset<T>(pb_AssetBundlePath path) where T : UnityEngine.Object
61 {
62 return pb_AssetBundles.instance._LoadAsset<T>(path);
63 }
64#endregion
65
66#region Implementation
67
68 private void _RegisterAssetBundle(string full_path)
69 {
70 if(!availableAssetBundles.Contains(full_path))
71 availableAssetBundles.Add(full_path);
72 }
73
74 private AssetBundle _LoadAssetBundle(string full_path)
75 {
76 AssetBundle bundle = null;
77
78 if(!loadedAssetBundles.TryGetValue(full_path, out bundle))
79 {
80 _RegisterAssetBundle(full_path);
81
82#if UNITY_5_2 || UNITY_5_1
83 bundle = AssetBundle.CreateFromFile(full_path);
84#else
85 bundle = AssetBundle.LoadFromFile(full_path);
86#endif
87 loadedAssetBundles.Add(full_path, bundle);
88 }
89
90 return bundle;
91 }
92
93 private void _UnloadAssetBundle(string full_path, bool unloadAllLoadedObjects)
94 {
95 if(loadedAssetBundles.ContainsKey(full_path))
96 {
97 loadedAssetBundles[full_path].Unload(unloadAllLoadedObjects);
98 loadedAssetBundles.Remove(full_path);
99 }
100 }
101
102 private void _UnloadAssetBundle(AssetBundle bundle)
103 {
104 foreach(KeyValuePair<string, AssetBundle> bundles in loadedAssetBundles)
105 {
106 if(bundles.Value == bundle)
107 {
108 loadedAssetBundles.Remove(bundles.Key);
109 return;
110 }
111 }
112 }
113
114 private bool _GetAssetPath<T>(T asset, out pb_AssetBundlePath path) where T : UnityEngine.Object
115 {
116 foreach(string bundle_path in availableAssetBundles)
117 {
118 try
119 {
120 AssetBundle bundle = _LoadAssetBundle(bundle_path);
121
122 foreach(KeyValuePair<string, UnityEngine.Object> kvp in LoadBundleAssetsWithPaths(bundle))
123 {
124 if(kvp.Value.GetType() == typeof(T))
125 {
126 path = new pb_AssetBundlePath(bundle_path, kvp.Key);
127
128 return true;
129 }
130 }
131
132 }
133
134 catch(System.Exception e)
135 {
136 Debug.LogWarning("Failed loading AssetBundle: " + bundle_path + "\n" + e.ToString());
137 }
138 }
139
140 path = null;
141
142 return false;
143 }
144
145 private Dictionary<string, UnityEngine.Object> LoadBundleAssetsWithPaths(AssetBundle bundle)
146 {
147 Dictionary<string, UnityEngine.Object> dic = new Dictionary<string, UnityEngine.Object>();
148
149 foreach(string str in bundle.GetAllAssetNames())
150 {
151 dic.Add(str, bundle.LoadAsset(str));
152 }
153
154 return dic;
155 }
156
157 private AssetBundle _LoadAssetBundleWithName(string name)
158 {
159 // First check if bundle is already loaded
160 foreach(string str in availableAssetBundles)
161 {
162 if( string.Compare( System.IO.Path.GetFileName(str), name, true) == 0 )
163 {
164 return _LoadAssetBundle(str);
165 }
166 }
167
168 // if not, search known assetbundle directories for match (returns first matching bundle path)
169 foreach(string dir in pb_Config.AssetBundle_SearchDirectories)
170 {
171 string[] paths = Directory.GetFiles(dir, name, SearchOption.AllDirectories);
172
173 if(paths.Length > 0)
174 {
175 foreach(string path in paths)
176 {
177 try
178 {
179 AssetBundle bundle = _LoadAssetBundle(path);
180 if(bundle != null)
181 return bundle;
182 } catch {}
183 }
184 }
185 }
186
187 return null;
188 }
189
190 private T _LoadAsset<T>(pb_AssetBundlePath path) where T : UnityEngine.Object
191 {
192 AssetBundle bundle = _LoadAssetBundleWithName(path.assetBundleName);
193
194 if(bundle == null)
195 return (T) null;
196
197 if( bundle.Contains(path.filePath) )
198 {
199 T obj = bundle.LoadAsset<T>(path.filePath);
200
201 return obj;
202 }
203
204 return (T) null;
205 }
206#endregion
207 }
208}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
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)