1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
| /// <summary> /// 使用ab加载资源(Editor模式下、线上平台) /// </summary> public class ABLoadModule : ILoadModule { public int SyncCount; // 同步加载并发数 private AssetBundleManifest m_manifest = null; private HashSet<string> m_bundleNames = new HashSet<string>(); // 加载队列 List<Loader> m_loadings = new List<Loader>();//正在加载 List<Loader> m_loaderQueue = new List<Loader>();//等待加载 Dictionary<string, AssetBundleLoader> m_dicAllLoader = new Dictionary<string, AssetBundleLoader>();
float m_lastClear = 0; // 上一次清除时间
public void Init(int syncCout) { SyncCount = syncCout; LoadManifest(); }
public void UnInit() { for (int i = 0; i < m_loadings.Count; i++) { m_loadings[i].Stop(); } m_loadings.Clear();
m_loaderQueue.Clear(); ABCachePool.Instance.ClearAllCache(); }
private List<int> m_lstRmTemp = new List<int>(); public void Update(float dt) { for (int i = m_loadings.Count - 1; i >= 0; i--) { Loader loader = m_loadings[i]; loader.Update(dt); if (loader.IsFinish) { if (loader.Type == Loader.LoaderType.BUNDLE) { AssetBundleLoader bLoader = loader as AssetBundleLoader; if (m_dicAllLoader.ContainsKey(bLoader.BundleName)) { m_dicAllLoader.Remove(bLoader.BundleName); } } m_loadings.RemoveAt(i); LoaderPool.Instance.RecycleLoader(loader); } }
int remain = Mathf.Min(SyncCount - m_loadings.Count, m_loaderQueue.Count) ;
for (int i = 0; i < m_loaderQueue.Count; i++) { Loader loader = m_loaderQueue[i]; if (loader.Type == Loader.LoaderType.BUNDLE) { AssetBundleLoader bLoader = loader as AssetBundleLoader; if (!bLoader.IsPrepareToLoad()) { continue; //如果有依赖未加载完直接跳过 } } else if (loader.Type == Loader.LoaderType.BUNDLEASSET) { BundleAssetLoader bLoader = loader as BundleAssetLoader; if (!bLoader.IsPrepareToLoad()) { continue; //Asset是否准备好加载 } }
m_loadings.Add(loader); loader.Load(); loader.Update(dt); m_lstRmTemp.Add(i); if (m_lstRmTemp.Count >= remain) { break; } }
if (m_lstRmTemp.Count > 0) { for (int i = m_lstRmTemp.Count - 1; i >= 0; i--) { m_loaderQueue.RemoveAt(m_lstRmTemp[i]); } m_lstRmTemp.Clear(); }
UpdateAssetBundleCache(); }
// 更新AssetBundle缓存(主要执行定时清理) public void UpdateAssetBundleCache() { // 每5秒回收一次 if (Time.realtimeSinceStartup - m_lastClear < 5) { return; }
m_lastClear = Time.realtimeSinceStartup;
/// 检查无引用的AB节点 ABCachePool.Instance.ClearNoneRefCache(true); }
public void Clear() { ABCachePool.Instance.ClearNoneRefCache(false);
Resources.UnloadUnusedAssets(); }
#region LoadManifest // 加载资源清单 public void LoadManifest() { if (m_manifest != null) { Object.DestroyImmediate(m_manifest, true); m_manifest = null; }
m_bundleNames.Clear();
string manifestName = FileHelper.MANIFEST_NAME;//manifest文件名 string strFullPath = FileHelper.SearchFilePath(manifestName);//获取manifest路径
AssetBundleLoader bLoader = LoaderPool.Instance.GetLoader<AssetBundleLoader>(); bLoader.Init(strFullPath, manifestName, null, delegate (object data) { AssetBundleCache ab = data as AssetBundleCache; if (ab != null) { m_manifest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest", typeof(AssetBundleManifest)); }
ABCachePool.Instance.UnReferenceCache(manifestName, true); // 不走统一接口是因为manifest文件没有后缀
if (m_manifest != null) { string[] bundles = m_manifest.GetAllAssetBundles();
for (int i = 0; i < bundles.Length; ++i) { m_bundleNames.Add(bundles[i]); } } }, false); bLoader.Load(); } #endregion
public void LoadPrefab(string strPath, string name, LoadedCallback onLoaded, bool async = true) { LoadAssetFromBundle(strPath, name, typeof(GameObject), onLoaded, async); }
public void LoadMusic(string name, LoadedCallback onLoaded, bool async = true) { string path = string.Format("music/{0}", name); LoadAssetFromBundle(path, name, typeof(AudioClip), onLoaded, async); }
public void LoadFont(string name, LoadedCallback onLoaded, bool async = true, bool inBundle = false) { string path = string.Format("ui/font/{0}", name); LoadAssetFromBundle(path, name, typeof(Font), onLoaded, async); }
#region LoadScene public void LoadScene(string name, string scenePath, bool isAdditive, LoadedCallback onLoaded) { var activeSceneName = SceneManager.GetActiveScene().name; // 如果当前场景是要加载的场景 直接返回 if (activeSceneName == name) { if (onLoaded != null) { onLoaded(null); } return; }
if (isAdditive) { RealLoadScene(name, scenePath, isAdditive, onLoaded); } else //大场景先加载idle过渡 { RealLoadScene(name, scenePath, isAdditive, onLoaded); } }
private void RealLoadScene(string name, string scenePath, bool isAdditive, LoadedCallback onLoaded) { string abPath = "scenes/" + name; LoadAssetBundle(abPath, delegate (object data) { if (data == null) { CallFunc_LoadedBack(onLoaded, null); return; } __LoadScene(name, scenePath, isAdditive, onLoaded); //场景的bundle在SceneLoader中自动卸载 }); }
private void __LoadScene(string name, string scenePath, bool isAdditive, LoadedCallback onLoaded, bool async = true) { SceneLoader sLoader = LoaderPool.Instance.GetLoader<SceneLoader>(); sLoader.Init(name, scenePath, isAdditive, onLoaded, async); StartLoad(sLoader, true); } #endregion
//从Bundle中加载资源 public void LoadAssetFromBundle(string path, string name, System.Type type, LoadedCallback onLoaded, bool async = true) { LoadAssetBundle(path, (data) => { AssetBundleCache abCache = data as AssetBundleCache; if (abCache == null) { Debug.LogWarningFormat("LoadAssetFromBundle, load ab fail:{0}", path); CallFunc_LoadedBack(onLoaded, null); return; }
// 开启任务去做加载 BundleAssetLoader assetLoader = LoaderPool.Instance.GetLoader<BundleAssetLoader>(); assetLoader.Init(abCache, name, type, onLoaded, async); StartLoad(assetLoader, async); }, async); }
// 加载AssetBundle(先从persistentData读,没有找到则从streamingAssets读,带后缀) public void LoadAssetBundle(string path, LoadedCallback onLoaded, bool async = true) { string name = FileHelper.GenBundlePath(path); if (!HasAssetBundle(name)) { if (onLoaded != null) { onLoaded(null); } return; }
// 加载依赖 LoadDependencies(name, async);
// 检查是否有缓存 有缓存说明依赖资源也是加载过了的 AssetBundleCache abCache = ABCachePool.Instance.ReferenceCacheByName(name); if (abCache != null) { if (onLoaded != null) { onLoaded(abCache); } return; }
string fullpath = FileHelper.SearchFilePath(name); AssetBundleLoader bLoader = null; m_dicAllLoader.TryGetValue(name, out bLoader); if (bLoader == null) { string[] dependencies = m_manifest.GetDirectDependencies(name);
bLoader = LoaderPool.Instance.GetLoader<AssetBundleLoader>(); bLoader.Init(fullpath, name, dependencies, onLoaded, async); m_dicAllLoader.Add(name, bLoader); StartLoad(bLoader, async); } else { if (onLoaded != null) { bLoader.AddLoadedCallback(onLoaded); } else { bLoader.AddReferenced(); } } }
// 依赖 // 加载依赖 //asyncInFact 实际加载方式,如果依赖bundle是异步加载并且正在加载中,那么整个bundle的加载方式变成异步加载 void LoadDependencies(string name, bool async) { if (m_manifest == null) { return; }
string[] dependencies = m_manifest.GetDirectDependencies(name); for (int i = 0; i < dependencies.Length; ++i) { LoadAssetBundle(dependencies[i], null, async); } }
void StartLoad(Loader loader, bool async, bool toHead = false) { // 异步加载或者加载还未准备好,则当做异步处理,外部控制是否加入队列开头 // 同步加载,并且已经具备加载条件,则直接调用Load进行加载 if (async || !loader.IsPrepareToLoad()) { if (toHead) { m_loaderQueue.Insert(0, loader); } else { m_loaderQueue.Add(loader); } } else { m_loadings.Add(loader); loader.Load(); } }
public bool HasAssetBundle(string path) { path = path.Replace("/", "_").ToLower(); return m_bundleNames.Count == 0 || m_bundleNames.Contains(path) || string.Equals(path, FileHelper.MANIFEST_NAME); }
// 卸载关卡场景 public void UnloadLevelScene(string sceneName, bool immediate) { SceneManager.UnloadSceneAsync(sceneName); UnloadSceneAssetBundle(sceneName, immediate); }
// 卸载场景的AssetBundle public void UnloadSceneAssetBundle(string sceneName, bool immediate) { if (Config.Instance.UseAssetBundle) { string strABPath = "scenes/" + sceneName; UnloadAssetBundle(strABPath, immediate); } }
// 卸载AssetBundle public void UnloadAssetBundle(string path, bool immediate = false) { string name = FileHelper.GenBundlePath(path); AssetBundleCache cache = ABCachePool.Instance.UnReferenceCache(name, immediate); if (cache != null) { UnloadDependencies(name, immediate); } }
// 卸载依赖 void UnloadDependencies(string name, bool immediate) { if (m_manifest == null) { return; }
string[] dependencies = m_manifest.GetDirectDependencies(name); for (int i = 0; i < dependencies.Length; ++i) { UnloadAssetBundle(dependencies[i], immediate); } }
private void CallFunc_LoadedBack(LoadedCallback callback, object data) { if (callback != null) { callback(data); } } }
|