7
7
// <time> #2018年6月22日 18点33分# </time>
8
8
//-----------------------------------------------------------------------
9
9
10
+ using System ;
10
11
using UnityEditor ;
11
12
using System . IO ;
12
13
using UnityEngine ;
@@ -20,7 +21,48 @@ public static class AssetBundleTool
20
21
private static readonly string [ ] _invalidFileFormats = new string [ ] { ".cs" , ".js" , ".shader" , ".dll" , ".db" } ;
21
22
22
23
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
+
24
66
/// <summary>
25
67
/// 读取资源文件夹下的所有子资源
26
68
/// </summary>
@@ -284,7 +326,7 @@ public static void OpenFolder(string path)
284
326
[ MenuItem ( "Tools/Taurus/Build AssetBundles %#T" ) ]
285
327
public static void BuildAssetBundles ( )
286
328
{
287
- string buildPath = EditorPrefs . GetString ( Application . productName + "_BuildPath" , "" ) ;
329
+ string buildPath = EditorConfigInfo . BuildPath ;
288
330
if ( ! Directory . Exists ( buildPath ) )
289
331
{
290
332
Debug . LogError ( "Please set build path!" ) ;
@@ -293,13 +335,13 @@ public static void BuildAssetBundles()
293
335
294
336
//打包资源
295
337
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 ;
298
340
BuildPipeline . BuildAssetBundles ( buildPath , option , target ) ;
299
341
Debug . Log ( "打包完成!" + System . DateTime . Now . ToString ( "HH:mm:ss:fff" ) ) ;
300
342
301
343
//资源加密
302
- if ( ( EncryptMode ) EditorPrefs . GetInt ( Application . productName + "_EncryptMode" , 0 ) == EncryptMode . AES )
344
+ if ( ( EncryptMode ) EditorConfigInfo . EncryptMode == EncryptMode . AES )
303
345
{
304
346
string keyPath = Application . dataPath + "/Resources" ;
305
347
if ( ! Directory . Exists ( keyPath ) )
@@ -336,9 +378,12 @@ public static void BuildAssetBundles()
336
378
//写入版本号信息
337
379
string assetVersionPath = buildPath + "/AssetVersion.txt" ;
338
380
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 ;
341
383
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 ) ;
342
387
DirectoryInfo dir1 = new DirectoryInfo ( buildPath ) ;
343
388
FileSystemInfo [ ] infos1 = dir1 . GetFileSystemInfos ( ) ;
344
389
foreach ( FileSystemInfo info in infos1 )
@@ -356,11 +401,15 @@ public static void BuildAssetBundles()
356
401
}
357
402
string content = JsonUtility . ToJson ( version ) ;
358
403
File . WriteAllText ( assetVersionPath , content ) ;
359
-
404
+
360
405
//版本号迭代
361
- EditorPrefs . SetInt ( Application . productName + "_AssetVersion" , EditorPrefs . GetInt ( Application . productName + "_AssetVersion" , 1 ) + 1 ) ;
406
+ EditorConfigInfo . AssetVersion += 1 ;
407
+ SaveEditorConfigInfo ( ) ;
362
408
363
409
AssetDatabase . Refresh ( ) ;
410
+
411
+ //打开打包文件夹
412
+ EditorUtility . OpenWithDefaultApp ( buildPath ) ;
364
413
}
365
414
}
366
415
}
0 commit comments