Skip to content

Commit 8b613e2

Browse files
committed
完成ResourceSystem优化
1 parent d0dbb08 commit 8b613e2

File tree

2 files changed

+51
-140
lines changed

2 files changed

+51
-140
lines changed

CrazyCar/Assets/Plugins/NiceVibrations/ThirdParty/Newtonsoft/Newtonsoft.Json.dll.meta

Lines changed: 0 additions & 33 deletions
This file was deleted.

CrazyCar/Assets/Scripts/System/ResourceSystem.cs

Lines changed: 51 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,6 @@ public class ResourceSystem : AbstractSystem, IResourceSystem {
4545
public ResourceType curResourceType { get; set; }
4646

4747
private Dictionary<ABType, ABInfo> abDic = new Dictionary<ABType, ABInfo>();
48-
private AssetBundle avatar;
49-
private AssetBundle equip;
50-
private string avatarHash = "";
51-
private uint avatarCRC = 0;
52-
private string avatarURL = "";
53-
private float avatarSize = 1000;
54-
private string localAvatarString = Application.streamingAssetsPath + "/avatar_" + Util.GetPlatform().ToLower();
55-
56-
private string equipHash = "";
57-
private uint equipCRC = 0;
58-
private string equipURL = "";
59-
private float equipSize = 1000;
60-
private string localEquipString = Application.streamingAssetsPath + "/equip_" + Util.GetPlatform().ToLower();
61-
62-
private ProgressCallback progressCallback;
6348

6449
public void CheckNewResource() {
6550
//Debug.Log("CheckNewResource");
@@ -114,15 +99,15 @@ private void CheckCoroutine() {
11499
}
115100
abDic[ABType.Avatar].size = float.Parse((string)data["avatar"]["size"]);
116101

117-
equipHash = (string)data["equip"]["hash"];
118-
equipCRC = uint.Parse((string)data["equip"]["crc"]);
102+
abDic[ABType.Equip].hash = (string)data["equip"]["hash"];
103+
abDic[ABType.Equip].crc = uint.Parse((string)data["equip"]["crc"]);
119104
string equipStr = (string)data["equip"]["url"];
120105
if (equipStr.Contains("http")) {
121-
equipURL = equipStr;
106+
abDic[ABType.Equip].url = equipStr;
122107
} else {
123-
equipURL = this.GetSystem<INetworkSystem>().HttpBaseUrl + (string)data["equip"]["url"];
108+
abDic[ABType.Equip].url = this.GetSystem<INetworkSystem>().HttpBaseUrl + (string)data["equip"]["url"];
124109
}
125-
equipSize = float.Parse((string)data["equip"]["size"]);
110+
abDic[ABType.Equip].size = float.Parse((string)data["equip"]["size"]);
126111

127112
GetLocalResource();
128113
},
@@ -158,20 +143,20 @@ private IEnumerator ParseLocalResource() {
158143
string localAvatarHash = (string)maniData["avatar"];
159144
string localEquipHash = (string)maniData["equip"];
160145

161-
Debug.Log("++++++ remote avatarHash = " + avatarHash + " localAvatarHash = " + localAvatarHash);
162-
Debug.Log("++++++ remote equipHash = " + equipHash + " localEquipHash = " + localEquipHash);
163-
if (localAvatarHash == avatarHash) {
164-
avatar = AssetBundle.LoadFromFile(localAvatarString);
146+
Debug.Log("++++++ remote avatarHash = " + abDic[ABType.Avatar].hash + " localAvatarHash = " + localAvatarHash);
147+
Debug.Log("++++++ remote equipHash = " + abDic[ABType.Equip].hash + " localEquipHash = " + localEquipHash);
148+
if (localAvatarHash == abDic[ABType.Avatar].hash) {
149+
abDic[ABType.Avatar].assetBundle = AssetBundle.LoadFromFile(abDic[ABType.Avatar].localABPath);
165150
}
166-
if (localEquipHash == equipHash) {
167-
equip = AssetBundle.LoadFromFile(localEquipString);
151+
if (localEquipHash == abDic[ABType.Equip].hash) {
152+
abDic[ABType.Equip].assetBundle = AssetBundle.LoadFromFile(abDic[ABType.Equip].localABPath);
168153
}
169154
}
170155

171156
// 下载线上之后,会缓存到一个文件夹,不会替换本地文件
172157
//Caching.ClearCache();
173-
if (!Caching.IsVersionCached(avatarURL, Hash128.Parse(avatarHash)) && avatar == null ||
174-
(!Caching.IsVersionCached(equipURL, Hash128.Parse(equipHash)) && equip == null)) {
158+
if (!Caching.IsVersionCached(abDic[ABType.Avatar].url, Hash128.Parse(abDic[ABType.Avatar].hash)) && abDic[ABType.Avatar].assetBundle == null ||
159+
(!Caching.IsVersionCached(abDic[ABType.Equip].url, Hash128.Parse(abDic[ABType.Equip].hash)) && abDic[ABType.Equip].assetBundle == null)) {
175160
curResourceType = ResourceType.ToDownload;
176161
} else {
177162
curResourceType = ResourceType.Loaded;
@@ -185,89 +170,48 @@ public void DownloadAssets(Action success, ProgressCallback progressCallback, Ac
185170
}
186171

187172
private IEnumerator Download(Action success, ProgressCallback progressCallback, Action fail) {
188-
if (avatar == null) {
189-
Debug.Log("Try to Load avatar From Web");
190-
float lastProgress = -1;
191-
var _req = UnityWebRequestAssetBundle.GetAssetBundle(avatarURL, Hash128.Parse(avatarHash), avatarCRC);
192-
_req.SendWebRequest();
193-
long lastTime = Util.GetTime();
194-
Debug.Log("dowload avatar before");
195-
while (!_req.isDone) {
196-
if (Mathf.Approximately(lastProgress, _req.downloadProgress)) {
197-
if (Util.GetTime() - lastTime > 100 * 1000) {
198-
//fail
199-
fail?.Invoke();
200-
yield break;
173+
foreach (var item in abDic) {
174+
if (item.Value.assetBundle == null) {
175+
Debug.Log("Try to Load " + item.Value.name + " From Web");
176+
float lastProgress = -1;
177+
var _req = UnityWebRequestAssetBundle.GetAssetBundle(item.Value.url, Hash128.Parse(item.Value.hash), item.Value.crc);
178+
_req.SendWebRequest();
179+
long lastTime = Util.GetTime();
180+
Debug.Log("dowload " + item.Value.name + " before");
181+
while (!_req.isDone) {
182+
if (Mathf.Approximately(lastProgress, _req.downloadProgress)) {
183+
if (Util.GetTime() - lastTime > 100 * 1000) {
184+
//fail
185+
fail?.Invoke();
186+
yield break;
187+
} else {
188+
}
201189
} else {
190+
lastProgress = _req.downloadProgress;
191+
lastTime = Util.GetTime();
202192
}
203-
} else {
204-
lastProgress = _req.downloadProgress;
205-
lastTime = Util.GetTime();
206-
}
207-
208-
try {
209-
progressCallback(_req.downloadProgress, avatarSize * 1024 * 1024, true);
210-
} catch {
211-
Debug.LogError("bundleError");
212-
}
213-
214-
yield return null;
215-
}
216-
217-
Debug.Log("dowload avatar finish");
218-
try {
219-
avatar = DownloadHandlerAssetBundle.GetContent(_req);
220-
} catch (System.Exception e) {
221-
Debug.Log(e.ToString());
222-
}
223193

224-
Debug.Log("get avatar bundle finish");
225-
if (avatar == null) {
226-
fail?.Invoke();
227-
yield break;
228-
}
229-
}
230-
231-
if (equip == null) {
232-
Debug.Log("Try to Load equip From Web");
233-
float lastProgress = -1;
234-
var _req = UnityWebRequestAssetBundle.GetAssetBundle(equipURL, Hash128.Parse(equipHash), equipCRC);
235-
_req.SendWebRequest();
236-
long lastTime = Util.GetTime();
237-
Debug.Log("dowload equip before");
238-
while (!_req.isDone) {
239-
if (Mathf.Approximately(lastProgress, _req.downloadProgress)) {
240-
if (Util.GetTime() - lastTime > 10 * 1000) {
241-
//fail
242-
fail?.Invoke();
243-
yield break;
244-
} else {
194+
try {
195+
progressCallback(_req.downloadProgress, item.Value.size * 1024 * 1024, true);
196+
} catch {
197+
Debug.LogError("bundleError");
245198
}
246-
} else {
247-
lastProgress = _req.downloadProgress;
248-
lastTime = Util.GetTime();
199+
200+
yield return null;
249201
}
250202

203+
Debug.Log("dowload " + item.Value.name + " finish");
251204
try {
252-
progressCallback(_req.downloadProgress, equipSize * 1024 * 1024, true);
253-
} catch {
254-
Debug.LogError("bundleError");
205+
item.Value.assetBundle = DownloadHandlerAssetBundle.GetContent(_req);
206+
} catch (System.Exception e) {
207+
Debug.Log(e.ToString());
255208
}
256209

257-
yield return null;
258-
}
259-
260-
Debug.Log("dowload equip finish");
261-
try {
262-
equip = DownloadHandlerAssetBundle.GetContent(_req);
263-
} catch (System.Exception e) {
264-
Debug.Log(e.ToString());
265-
}
266-
267-
Debug.Log("get equip bundle finish");
268-
if (equip == null) {
269-
fail?.Invoke();
270-
yield break;
210+
Debug.Log("get " + item.Value.name + " bundle finish");
211+
if (item.Value.assetBundle == null) {
212+
fail?.Invoke();
213+
yield break;
214+
}
271215
}
272216
}
273217

@@ -278,14 +222,14 @@ public Sprite GetAvatarResource(int aid) {
278222
Sprite resultSprite;
279223
try {
280224
#if !UNITY_EDITOR
281-
Debug.Log("+++ " + "Assets/AB/Avatar/" + aid + ".png");
282-
resultSprite = avatar.LoadAsset<Sprite>("Assets/AB/Avatar/" + aid + ".png");
225+
Debug.Log("+++ " + abDic[ABType.Avatar].localPrafabPath + aid + ".png");
226+
resultSprite = abDic[ABType.Avatar].assetBundle.LoadAsset<Sprite>(abDic[ABType.Avatar].localPrafabPath + aid + ".png");
283227
if (resultSprite == null) {
284228
return null;
285229
}
286230
#else
287231
resultSprite = UnityEditor.AssetDatabase.LoadAssetAtPath<Sprite>(
288-
"Assets/AB/Avatar/" + aid.ToString() + ".png");
232+
abDic[ABType.Avatar].localPrafabPath + aid.ToString() + ".png");
289233

290234
#endif
291235
return resultSprite;
@@ -302,14 +246,14 @@ public EquipResource GetEquipResource(string rid) {
302246
return this.GetModel<IEquipModel>().EquipResource[rid];
303247
}
304248
#if !UNITY_EDITOR
305-
var o = equip.LoadAsset<GameObject>("Assets/AB/Equip/Items/" + rid + ".prefab");
249+
var o = abDic[ABType.Equip].assetBundle.LoadAsset<GameObject>(abDic[ABType.Equip].localPrafabPath + rid + ".prefab");
306250
if(o == null) {
307251
return null;
308252
}
309253
var e = o.GetComponent<EquipResource>();
310254
#else
311255
var e = UnityEditor.AssetDatabase.LoadAssetAtPath<EquipResource>(
312-
"Assets/AB/Equip/Items/" + rid + ".prefab");
256+
abDic[ABType.Equip].localPrafabPath + rid + ".prefab");
313257

314258
#endif
315259
this.GetModel<IEquipModel>().EquipResource[rid] = e;

0 commit comments

Comments
 (0)