Skip to content

Commit 5b57d15

Browse files
committed
添加assetbundle资源解密,增加assetbundle的卸载接口
1 parent a9fab85 commit 5b57d15

File tree

3 files changed

+84
-48
lines changed

3 files changed

+84
-48
lines changed

Assets/Game/Scripts/Resource/EditorResourceHelper.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,20 @@ public void LoadAssetAsync<T>(string assetName, Action<string, UnityEngine.Objec
4747
asyncCallback.Invoke(assetName,AssetDatabase.LoadAssetAtPath<T>(assetName));
4848
}
4949

50-
/// <summary>
51-
/// 异步加载场景
52-
/// </summary>
53-
/// <param name="sceneName"></param>
54-
public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Additive)
50+
/// <summary>
51+
/// 卸载资源 主要为卸载AssetBundle
52+
/// </summary>
53+
/// <param name="assetName">资源名称</param>
54+
/// <param name="allAssets">是否卸载调所有资源</param>
55+
public void UnloadAsset(string assetName, bool allAssets)
56+
{
57+
}
58+
59+
/// <summary>
60+
/// 异步加载场景
61+
/// </summary>
62+
/// <param name="sceneName"></param>
63+
public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Additive)
5564
{
5665
return UnitySceneManager.LoadSceneAsync(sceneName, mode);
5766
}

Assets/GameFramework/Resource/BundleResourceHelper .cs

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,17 @@ public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBu
7676
public T LoadAsset<T>(string assetName) where T : Object
7777
{
7878
assetName = assetName.ToLower();
79-
//if (_allObjects.ContainsKey(assetName))
80-
// return (T)_allObjects[assetName];
8179

8280
AssetBundle assetBundle;
8381
if (_allAssets.TryGetValue(assetName, out assetBundle))
8482
{
85-
//加载相关依赖
86-
string[] dependencies = _mainfest.GetAllDependencies(assetName);
87-
foreach (var item in dependencies)
88-
{
89-
AssetBundle.LoadFromFile(_readPath + "/" + item);
90-
}
83+
////加载相关依赖
84+
//string[] dependencies = _mainfest.GetAllDependencies(assetName);
85+
//foreach (var item in dependencies)
86+
//{
87+
// AssetBundle.LoadFromFile(_readPath + "/" + item);
88+
//}
9189
T asset = assetBundle.LoadAsset<T>(assetName);
92-
// _allObjects.Add(assetName, asset);
9390
return asset;
9491
}
9592
return null;
@@ -105,12 +102,12 @@ public void LoadAssetAsync<T>(string assetName, Action<string, UnityEngine.Objec
105102
AssetBundle assetBundle;
106103
if (_allAssets.TryGetValue(assetName, out assetBundle))
107104
{
108-
//加载相关依赖
109-
string[] dependencies = _mainfest.GetAllDependencies(assetName);
110-
foreach (var item in dependencies)
111-
{
112-
AssetBundle.LoadFromFile(_readPath + "/" + item);
113-
}
105+
////加载相关依赖
106+
//string[] dependencies = _mainfest.GetAllDependencies(assetName);
107+
//foreach (var item in dependencies)
108+
//{
109+
// AssetBundle.LoadFromFile(_readPath + "/" + item);
110+
//}
114111
AssetBundleRequest requetAsset = assetBundle.LoadAssetAsync<T>(assetName);
115112
requetAsset.completed += (asyncOperation) => { asyncCallback.Invoke(assetName, requetAsset.asset); };
116113
return;
@@ -119,6 +116,28 @@ public void LoadAssetAsync<T>(string assetName, Action<string, UnityEngine.Objec
119116
return;
120117
}
121118

119+
/// <summary>
120+
/// 卸载掉资源
121+
/// </summary>
122+
/// <param name="assetName"></param>
123+
/// <param name="allAssets"></param>
124+
public void UnloadAsset(string assetName, bool allAssets)
125+
{
126+
AssetBundle assetBundle;
127+
if (_allAssets.TryGetValue(assetName, out assetBundle))
128+
{
129+
if (!allAssets)
130+
_allAssets.Remove(assetName);
131+
else
132+
{
133+
foreach (var item in assetBundle.GetAllAssetNames())
134+
if(_allAssets.ContainsKey(item))
135+
_allAssets.Remove(item);
136+
}
137+
//卸载资源
138+
assetBundle.Unload(allAssets);
139+
}
140+
}
122141

123142
/// <summary>
124143
/// 异步加载场景
@@ -129,12 +148,12 @@ public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = Load
129148
AssetBundle assetBundle;
130149
if (_allAssets.TryGetValue(sceneName, out assetBundle))
131150
{
132-
//加载相关依赖
133-
string[] dependencies = _mainfest.GetAllDependencies(sceneName);
134-
foreach (var item in dependencies)
135-
{
136-
AssetBundle.LoadFromFile(_readPath + "/" + item);
137-
}
151+
////加载相关依赖
152+
//string[] dependencies = _mainfest.GetAllDependencies(sceneName);
153+
//foreach (var item in dependencies)
154+
//{
155+
// AssetBundle.LoadFromFile(_readPath + "/" + item);
156+
//}
138157
return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, mode);
139158
}
140159
return null;
@@ -164,14 +183,14 @@ public void Clear()
164183
}
165184

166185
#endregion
167-
186+
168187
#region 内部函数
169188
/// <summary>
170-
/// 加载mainfest
189+
/// 加载mainfest -- LoadFromFile
171190
/// </summary>
172191
private void LoadPlatformMainfest(string rootBundlePath, string folderPath)
173192
{
174-
//string assetBundlePath = _readPath + "/AssetBundles";
193+
//string assetBundlePath = _readPath + "/AssetBundles";
175194
AssetBundle mainfestAssetBundle = AssetBundle.LoadFromFile(rootBundlePath);
176195
_mainfest = mainfestAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//
177196
string[] assetBundleNames = _mainfest.GetAllAssetBundles();
@@ -192,25 +211,26 @@ private void LoadPlatformMainfest(string rootBundlePath, string folderPath)
192211

193212
private void LoadPlatformMainfest(string rootBundlePath, string folerPath,EnciphererKey keyAsset)
194213
{
195-
//从内存中加载&解密
196-
//
197-
//DirectoryInfo dir = new DirectoryInfo(folerPath);
198-
//FileSystemInfo[] infos = dir.GetFileSystemInfos();
199-
//foreach (FileSystemInfo info in infos)
200-
//{
201-
// if (info is FileInfo)
202-
// {
203-
// if (info.Extension == "")
204-
// {
205-
// byte[] bs = File.ReadAllBytes(info.FullName);
206-
// byte[] cipbs = Encipherer.AESDecrypt(bs, keyAsset);
207-
// AssetBundle.LoadFromMemory
208-
// File.WriteAllBytes(info.FullName, cipbs);
209-
// Debug.Log("完成资源包 " + info.Name + " 的加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
210-
// }
211-
// }
212-
//}
213-
}
214+
//从内存中加载&解密
215+
byte[] datas = Encipherer.AESDecrypt( File.ReadAllBytes(rootBundlePath),keyAsset);
216+
AssetBundle mainfestAssetBundle = AssetBundle.LoadFromMemory(datas);
217+
_mainfest = mainfestAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//
218+
string[] assetBundleNames = _mainfest.GetAllAssetBundles();
219+
foreach (var item in assetBundleNames)
220+
{
221+
datas = Encipherer.AESDecrypt(File.ReadAllBytes(folerPath + "/" + item), keyAsset);
222+
AssetBundle assetBundle = AssetBundle.LoadFromMemory(datas);
223+
string[] assetNames = assetBundle.GetAllAssetNames();
224+
if (assetBundle.isStreamedSceneAssetBundle)
225+
assetNames = assetBundle.GetAllScenePaths();
226+
foreach (var name in assetNames)
227+
{
228+
if (!_allAssets.ContainsKey(name))
229+
_allAssets.Add(name, assetBundle);
230+
}
231+
}
232+
mainfestAssetBundle.Unload(false);
233+
}
214234

215235
#endregion
216236

Assets/GameFramework/Resource/IResourceHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public interface IResourceHelper
3636
/// <param name="asyncCallback"></param>
3737
void LoadAssetAsync<T>(string assetName,Action<string,UnityEngine.Object> asyncCallback) where T : UnityEngine.Object;
3838

39+
/// <summary>
40+
/// 卸载资源 主要为卸载AssetBundle
41+
/// </summary>
42+
/// <param name="assetName">资源名称</param>
43+
/// <param name="allAssets">是否卸载调所有资源</param>
44+
void UnloadAsset(string assetName, bool allAssets);
45+
3946
/// <summary>
4047
/// 异步加载场景
4148
/// </summary>

0 commit comments

Comments
 (0)