Skip to content

Commit d4cd093

Browse files
author
DESKTOP-UBV38B7\codingworks
committed
修改AssetBundle打包工具的配置信息为本地文件
1 parent 2e4e131 commit d4cd093

File tree

4 files changed

+106
-19
lines changed

4 files changed

+106
-19
lines changed

Assets/GameFramework/Config/AssetBundleVersionInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class AssetBundleVersionInfo
1515
{
1616
public int Version;
1717
public bool IsEncrypt;
18+
public string ManifestAssetBundle;
1819
public List<ResourcesInfo> Resources;
1920
}
2021

Assets/GameFramework/Editor/AssetBundleEditor/AssetBundleEditor.cs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
// <email> [email protected] </email>
77
// <time> #2018年6月22日 18点32分# </time>
88
//-----------------------------------------------------------------------
9+
10+
using System;
911
using UnityEngine;
1012
using UnityEditor;
1113
using System.Collections.Generic;
14+
using System.IO;
1215

1316
namespace GameFramework.Taurus
1417
{
@@ -77,11 +80,11 @@ private void Init()
7780
_assetBundle = new AssetBundleInfo();
7881
AssetBundleTool.ReadAssetBundleConfig(_assetBundle, _validAssets);
7982

80-
_buildPath = EditorPrefs.GetString(Application.productName + "_BuildPath", "");
81-
_buildTarget = (BuildTarget)EditorPrefs.GetInt(Application.productName + "_BuildTarget", 5);
82-
_zipMode = (ZipMode)EditorPrefs.GetInt(Application.productName + "_ZipMode", 0);
83-
_encryptMode = (EncryptMode)EditorPrefs.GetInt(Application.productName + "_EncryptMode", 1);
84-
_assetVersion = EditorPrefs.GetInt(Application.productName + "_AssetVersion", 1);
83+
_buildPath = AssetBundleTool.EditorConfigInfo.BuildPath;
84+
_buildTarget = (BuildTarget) AssetBundleTool.EditorConfigInfo.BuildTarget;
85+
_zipMode = (ZipMode) AssetBundleTool.EditorConfigInfo.ZipMode;
86+
_encryptMode = (EncryptMode) AssetBundleTool.EditorConfigInfo.EncryptMode;
87+
_assetVersion = AssetBundleTool.EditorConfigInfo.AssetVersion;
8588

8689
Resources.UnloadUnusedAssets();
8790
}
@@ -150,7 +153,8 @@ private void TitleGUI()
150153
if (path.Length != 0)
151154
{
152155
_buildPath = path;
153-
EditorPrefs.SetString(Application.productName + "_BuildPath", _buildPath);
156+
AssetBundleTool.EditorConfigInfo.BuildPath = _buildPath;
157+
AssetBundleTool.SaveEditorConfigInfo();
154158
}
155159
}
156160

@@ -377,30 +381,34 @@ private void SettingGUI()
377381
if (buildTarget != _buildTarget)
378382
{
379383
_buildTarget = buildTarget;
380-
EditorPrefs.SetInt(Application.productName + "_BuildTarget", (int)_buildTarget);
384+
AssetBundleTool.EditorConfigInfo.BuildTarget = (int) _buildTarget;
385+
AssetBundleTool.SaveEditorConfigInfo();
381386
}
382387

383388
GUI.Label(new Rect(5, 30, 80, 20), "Zip Mode: ");
384389
ZipMode zipMode = (ZipMode)EditorGUI.EnumPopup(new Rect(90, 30, 155, 20), _zipMode, _preDropDown);
385390
if (zipMode != _zipMode)
386391
{
387392
_zipMode = zipMode;
388-
EditorPrefs.SetInt(Application.productName + "_ZipMode", (int)_zipMode);
393+
AssetBundleTool.EditorConfigInfo.ZipMode = (int) _zipMode;
394+
AssetBundleTool.SaveEditorConfigInfo();
389395
}
390396

391397
GUI.Label(new Rect(5, 55, 80, 20), "Encrypt: ");
392398
EncryptMode encryptMode = (EncryptMode)EditorGUI.EnumPopup(new Rect(90, 55, 155, 20), _encryptMode, _preDropDown);
393399
if (encryptMode != _encryptMode)
394400
{
395401
_encryptMode = encryptMode;
396-
EditorPrefs.SetInt(Application.productName + "_EncryptMode", (int)_encryptMode);
402+
AssetBundleTool.EditorConfigInfo.EncryptMode = (int)encryptMode;
403+
AssetBundleTool.SaveEditorConfigInfo();
397404
}
398405

399406
GUI.Label(new Rect(5, 80, 150, 20), "Asset Version: " + _assetVersion);
400407
if (GUI.Button(new Rect(150, 80, 95, 20), "Reset", _preButton))
401408
{
402409
_assetVersion = 1;
403-
EditorPrefs.SetInt(Application.productName + "_AssetVersion", _assetVersion);
410+
AssetBundleTool.EditorConfigInfo.AssetVersion = _assetVersion;
411+
AssetBundleTool.SaveEditorConfigInfo();
404412
}
405413

406414
if (GUI.Button(new Rect(100, 105, 50, 20), "Sure", _preButton))
@@ -411,6 +419,34 @@ private void SettingGUI()
411419
GUI.EndGroup();
412420
}
413421
}
422+
423+
424+
//编辑器的配置信息
425+
[System.Serializable]
426+
public class EditorConfigInfo
427+
{
428+
/// <summary>
429+
/// 打包路径
430+
/// </summary>
431+
public string BuildPath;
432+
/// <summary>
433+
/// 压缩方式
434+
/// </summary>
435+
public int ZipMode=0;
436+
/// <summary>
437+
/// 目标平台
438+
/// </summary>
439+
public int BuildTarget=5;
440+
/// <summary>
441+
/// 加密模式
442+
/// </summary>
443+
public int EncryptMode=1;
444+
/// <summary>
445+
/// 资源版本
446+
/// </summary>
447+
public int AssetVersion=1;
448+
}
449+
414450
}
415451

416452
/// <summary>

Assets/GameFramework/Editor/AssetBundleEditor/AssetBundleTool.cs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// <time> #2018年6月22日 18点33分# </time>
88
//-----------------------------------------------------------------------
99

10+
using System;
1011
using UnityEditor;
1112
using System.IO;
1213
using UnityEngine;
@@ -20,7 +21,48 @@ public static class AssetBundleTool
2021
private static readonly string[] _invalidFileFormats = new string[] { ".cs", ".js", ".shader", ".dll", ".db" };
2122

2223
private static readonly string[] _invalidFolderName = new string[] { "Resources", "AssetBundleEditor", "Editor", "Gizmos", "StreamingAssets" };
23-
24+
25+
//编辑器配置文件信息
26+
private static AssetBundleEditor.EditorConfigInfo _editorConfigInfo;
27+
//编辑器配置文件的路径
28+
private static string _editorConfigPath;
29+
30+
public static AssetBundleEditor.EditorConfigInfo EditorConfigInfo
31+
{
32+
get
33+
{
34+
if (_editorConfigInfo == null)
35+
{
36+
#region 获取编辑器配置文件的信息
37+
_editorConfigPath = Path.Combine(Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/", StringComparison.Ordinal)), "ProjectSettings");
38+
if (!Directory.Exists(_editorConfigPath))
39+
Directory.CreateDirectory(_editorConfigPath);
40+
_editorConfigPath = Path.Combine(_editorConfigPath, "AssetBundleEditorConifg.json");
41+
if (!File.Exists(_editorConfigPath))
42+
_editorConfigInfo = new AssetBundleEditor.EditorConfigInfo();
43+
else
44+
{
45+
string content = File.ReadAllText(_editorConfigPath);
46+
_editorConfigInfo = string.IsNullOrEmpty(content) ? new AssetBundleEditor.EditorConfigInfo() : JsonUtility.FromJson<AssetBundleEditor.EditorConfigInfo>(content);
47+
}
48+
#endregion
49+
}
50+
51+
return _editorConfigInfo;
52+
}
53+
}
54+
55+
//保存编辑器配置信息
56+
public static void SaveEditorConfigInfo()
57+
{
58+
#region 设置编辑器配置文件的信息
59+
//写入文本文件
60+
if (EditorConfigInfo != null&& !string.IsNullOrEmpty(_editorConfigPath))
61+
File.WriteAllText(_editorConfigPath, JsonUtility.ToJson(EditorConfigInfo));
62+
#endregion
63+
}
64+
65+
2466
/// <summary>
2567
/// 读取资源文件夹下的所有子资源
2668
/// </summary>
@@ -284,7 +326,7 @@ public static void OpenFolder(string path)
284326
[MenuItem("Tools/Taurus/Build AssetBundles %#T")]
285327
public static void BuildAssetBundles()
286328
{
287-
string buildPath = EditorPrefs.GetString(Application.productName + "_BuildPath", "");
329+
string buildPath = EditorConfigInfo.BuildPath;
288330
if (!Directory.Exists(buildPath))
289331
{
290332
Debug.LogError("Please set build path!");
@@ -293,13 +335,13 @@ public static void BuildAssetBundles()
293335

294336
//打包资源
295337
Debug.Log("开始打包!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
296-
BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorPrefs.GetInt(Application.productName + "_ZipMode", 0);
297-
BuildTarget target = (BuildTarget)EditorPrefs.GetInt(Application.productName + "_BuildTarget", 5);
338+
BuildAssetBundleOptions option = (BuildAssetBundleOptions) EditorConfigInfo.ZipMode;
339+
BuildTarget target = (BuildTarget)EditorConfigInfo.BuildTarget;
298340
BuildPipeline.BuildAssetBundles(buildPath, option, target);
299341
Debug.Log("打包完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
300342

301343
//资源加密
302-
if ((EncryptMode)EditorPrefs.GetInt(Application.productName + "_EncryptMode", 0) == EncryptMode.AES)
344+
if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES)
303345
{
304346
string keyPath = Application.dataPath + "/Resources";
305347
if (!Directory.Exists(keyPath))
@@ -336,9 +378,12 @@ public static void BuildAssetBundles()
336378
//写入版本号信息
337379
string assetVersionPath = buildPath + "/AssetVersion.txt";
338380
AssetBundleVersionInfo version = new AssetBundleVersionInfo();
339-
version.Version = EditorPrefs.GetInt(Application.productName + "_AssetVersion", 1);
340-
version.IsEncrypt = (EncryptMode)EditorPrefs.GetInt(Application.productName + "_EncryptMode", 0) == EncryptMode.AES;
381+
version.Version = EditorConfigInfo.AssetVersion;
382+
version.IsEncrypt = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES;
341383
version.Resources = new List<ResourcesInfo>();
384+
int index = buildPath.LastIndexOf("/", StringComparison.Ordinal);
385+
if(index>0)
386+
version.ManifestAssetBundle = buildPath.Substring(index+1, buildPath.Length-index-1);
342387
DirectoryInfo dir1 = new DirectoryInfo(buildPath);
343388
FileSystemInfo[] infos1 = dir1.GetFileSystemInfos();
344389
foreach (FileSystemInfo info in infos1)
@@ -356,11 +401,15 @@ public static void BuildAssetBundles()
356401
}
357402
string content = JsonUtility.ToJson(version);
358403
File.WriteAllText(assetVersionPath, content);
359-
404+
360405
//版本号迭代
361-
EditorPrefs.SetInt(Application.productName + "_AssetVersion", EditorPrefs.GetInt(Application.productName + "_AssetVersion", 1) + 1);
406+
EditorConfigInfo.AssetVersion += 1;
407+
SaveEditorConfigInfo();
362408

363409
AssetDatabase.Refresh();
410+
411+
//打开打包文件夹
412+
EditorUtility.OpenWithDefaultApp(buildPath);
364413
}
365414
}
366415
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"BuildPath":"C:/Users/codingworks/Desktop/test","ZipMode":0,"BuildTarget":5,"EncryptMode":0,"AssetVersion":6}

0 commit comments

Comments
 (0)