1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class LoadManager
6 {
7 #region 单例
8 private static LoadManager instance;
9 public static LoadManager Intance
10 {
11 get {
12 if (instance == null)
13 {
14 instance = new LoadManager();
15 }
16 return instance;
17 }
18 }
19
20 #endregion
21 private LoadManager() {
22 abDic = new Dictionary<string, AssetBundle>();
23 abPath = Application.streamingAssetsPath + "/AssetBundle/";
24 singleABName = "AssetBundle";
25 }
26
27
28 /// <summary>
29 /// 用来存储已经加载了的AB包,键是AB包的名字,值就是AB包。
30 /// </summary>
31 public Dictionary<string, AssetBundle> abDic;
32
33 //用来存储单一的ab包。
34 public AssetBundle singleAB;
35
36 //单一的构建清单,所有的ab包的依赖全部从这获取。
37 public AssetBundleManifest singleManifest;
38
39 //存储ab包的路径
40 public string abPath;
41
42 //单一的ab包的名字
43 public string singleABName;
44
45 /// <summary>
46 /// 加载单一的ab包和单一的构建清单
47 /// </summary>
48 void LoadSingleAssetBundle()
49 {
50 //每次加载单一的ab包需要判断是否加载过,
51 //singleAB为null没加载过,不为null就是加载过
52 if (null == singleAB)
53 {
54 singleAB = AssetBundle.LoadFromFile(abPath + singleABName);
55 }
56
57 //从单一的ab包中加载单一的构建清单
58 if (singleManifest == null)
59 {
60 singleManifest = singleAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
61 }
62 }
63
64 /// <summary>
65 /// 加载指定ab包的所有的依赖项
66 /// </summary>
67 /// <param name="abName"></param>
68 void LoadAllDependencies(string abName)
69 {
70 LoadSingleAssetBundle();
71 //首先先获取指定的这个ab包的所有的依赖项
72 //从单一的构建清单中获取
73 string[] deps = singleManifest.GetAllDependencies(abName);
74
75 //遍历去加载依赖项
76 for (int i = 0; i < deps.Length; i++)
77 {
78 //加载该依赖项前,先判断之前加载没加载过该依赖项
79 //就是判断存储ab包的字典里有没有这个ab包
80 if (!abDic.ContainsKey(deps[i]))
81 {
82 //如果未加载过,需要加载.
83 AssetBundle ab = AssetBundle.LoadFromFile(abPath + deps[i]);
84
85 //ab包加载完之后, 把加载来的ab包存储在字典里
86 abDic[deps[i]] = ab;
87 }
88
89 }
90 }
91
92 /// <summary>
93 /// 加载指定的ab包,并且返回该ab包
94 /// </summary>
95 /// <param name="abName"></param>
96 /// <returns></returns>
97 public AssetBundle LoadAssetBundle(string abName)
98 {
99 //先加载该ab包的所有的依赖项
100 LoadAllDependencies(abName);
101
102 //加载指定的ab包。
103 //加载前先判是否已经加载过,如果加载过,把加载过的ab包给你
104 //如果未加载过,就加载该ab包。
105 AssetBundle ab = null;
106 if (!abDic.TryGetValue(abName, out ab))//证明该ab包已经加载过
107 {
108 //如果进入到这,证明该键没有指定的值,那么证明该ab包未加载,需要加载
109 ab = AssetBundle.LoadFromFile(abPath + abName);
110 //把加载进来的ab包添加的字典中
111 abDic[abName] = ab;
112 }
113
114 return ab;
115 }
116
117 /// <summary>
118 /// 加载指定的ab包中的指定名字的指定类型的资源
119 /// </summary>
120 /// <typeparam name="T">指定资源的类型</typeparam>
121 /// <param name="abName">ab包的名字</param>
122 /// <param name="assetName">资源的名字</param>
123 /// <returns></returns>
124 public T LoadAssetByAB<T>(string abName, string assetName) where T : Object
125 {
126 //先获取指定的ab包
127 AssetBundle ab = LoadAssetBundle(abName);
128 if (ab != null)
129 {
130 //从ab包中加载指定的资源
131 return ab.LoadAsset<T>(assetName);
132 }
133 else
134 {
135 Debug.LogError("指定的ab包的名字有误!");
136 }
137 return null;
138 }
139
140 /// <summary>
141 /// 卸载指定的ab包
142 /// </summary>
143 /// <param name="abName"></param>
144 public void UnloadAssetBundle(string abName, bool unloadAllLoadedObjects)
145 {
146 //先判断有没有这个ab包
147 AssetBundle ab = null;
148 if (abDic.TryGetValue(abName, out ab))
149 {
150 //卸载ab包
151 ab.Unload(unloadAllLoadedObjects);
152 //从容器中删除该ab包
153 abDic.Remove(abName);
154 }
155 }
156
157 /// <summary>
158 /// 卸载全部的ab包
159 /// </summary>
160 /// <param name="unloadAllLoadedObjects"></param>
161 public void UnloadAllAssetBundle(bool unloadAllLoadedObjects)
162 {
163 //遍历每一个ab包,调用ab包的卸载的方法
164 //遍历键,通过键去获取值进行卸载
165 /*
166 foreach (var item in abDic.Keys)
167 {
168 //卸载ab包
169 abDic[item].Unload(unloadAllLoadedObjects);
170 }
171 */
172 //直接遍历值去卸载
173 foreach (var item in abDic.Values)
174 {
175 item.Unload(unloadAllLoadedObjects);
176 }
177
178 //把字典所有内容清空
179 abDic.Clear();
180
181 }
182
183
184 }