Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit ffd13fd

Browse files
committed
增加Addressables的资源管理,以及对应的FileWatcher
1 parent dea05e1 commit ffd13fd

File tree

4 files changed

+236
-78
lines changed

4 files changed

+236
-78
lines changed

GameFramework/Editor/AssetManagement/AddressablesEditor.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using UnityEditor.AddressableAssets.Settings;
99
using System.IO;
1010
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
11+
using LitJson;
1112

1213
namespace Wanderer.GameFramework
1314
{
@@ -95,5 +96,94 @@ public static void ShellBuildPlayerContent(string activeProfileId = "Default")
9596
//BuildPlayerWindow.DefaultBuildMethods.BuildPlayer();
9697
}
9798

99+
/// <summary>
100+
/// 设置Addressable的所有资源
101+
/// </summary>
102+
[MenuItem("Tools/Asset Management/Assets To Addressables")]
103+
public static void SetAddressablesAssets()
104+
{
105+
JsonData config = ProjectSettingsConfig.LoadJsonData(AssetGroupEditor.ConfigName);
106+
var settings = AddressableAssetSettingsDefaultObject.Settings;
107+
if (settings != null)
108+
{
109+
string binPath = ContentUpdateScript.GetContentStateDataPath(false);
110+
if (config != null && config.Count > 0)
111+
{
112+
for (int i = 0; i < config.Count; i++)
113+
{
114+
JsonData item = config[i];
115+
string groupName = item["GroupName"].ToString();
116+
var group = settings.FindGroup(groupName);
117+
if (group == null)
118+
{
119+
group = settings.CreateGroup(groupName, false, false, false, null);
120+
}
121+
122+
ContentUpdateGroupSchema cugSchema = group.GetSchema<ContentUpdateGroupSchema>();
123+
if (cugSchema == null)
124+
{
125+
cugSchema = group.AddSchema<ContentUpdateGroupSchema>();
126+
}
127+
cugSchema.StaticContent = ((int)item["UpdateRestriction"] == 1);
128+
BundledAssetGroupSchema bagSchema = group.GetSchema<BundledAssetGroupSchema>();
129+
if (bagSchema == null)
130+
{
131+
bagSchema = group.AddSchema<BundledAssetGroupSchema>();
132+
}
133+
bagSchema.BuildPath.SetVariableByName(settings, item["BuildPath"].ToString());
134+
bagSchema.LoadPath.SetVariableByName(settings, item["LoadPath"].ToString());
135+
if (cugSchema.StaticContent)
136+
{
137+
bagSchema.UseAssetBundleCrc = false;
138+
bagSchema.UseAssetBundleCrcForCachedBundles = false;
139+
}
140+
141+
//Filter
142+
StringBuilder filterBuilder = new StringBuilder();
143+
for (int filterIndex = 0; filterIndex < item["Filter"].Count; filterIndex++)
144+
{
145+
filterBuilder.Append($"t:{item["Filter"][filterIndex].ToString()} ");
146+
}
147+
//SearchInFolders
148+
List<string> folders = new List<string>();
149+
for (int folderIndex = 0; folderIndex < item["SearchInFolders"].Count; folderIndex++)
150+
{
151+
folders.Add(item["SearchInFolders"][folderIndex].ToString());
152+
}
153+
//Labels
154+
List<string> labels = new List<string>();
155+
for (int labelIndex = 0; labelIndex < item["Labels"].Count; labelIndex++)
156+
{
157+
labels.Add(item["Labels"][labelIndex].ToString());
158+
}
159+
160+
//Find All Asset
161+
var findAssets = AssetDatabase.FindAssets(filterBuilder.ToString(), folders.ToArray());
162+
for (int findIndex = 0; findIndex < findAssets.Length; findIndex++)
163+
{
164+
string guid = findAssets[findIndex];
165+
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
166+
if (AssetDatabase.IsValidFolder(assetPath) || assetPath.EndsWith(".cs"))
167+
{
168+
continue;
169+
}
170+
var entry = group.GetAssetEntry(guid);
171+
if (entry == null)
172+
{
173+
entry = settings.CreateOrMoveEntry(guid, group);
174+
}
175+
entry.labels.Clear();
176+
foreach (var itemLabel in labels)
177+
{
178+
entry.SetLabel(itemLabel, true);
179+
}
180+
}
181+
}
182+
}
183+
EditorUtility.SetDirty(settings);
184+
AssetDatabase.Refresh();
185+
}
186+
}
187+
98188
}
99189
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
using System.IO;
6+
using LitJson;
7+
using UnityEditor.AddressableAssets;
8+
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
9+
using System.Text;
10+
using UnityEditor.AddressableAssets.Build;
11+
12+
namespace Wanderer.GameFramework
13+
{
14+
public class AssetFileWatcher
15+
{
16+
private static FileSystemWatcher _fileWatcher;
17+
private static bool _isRuning = false;
18+
19+
[InitializeOnLoadMethod]
20+
private static void RunAssetFileWatcher()
21+
{
22+
//Debug.Log($"AssetFileWatcher.RunAssetFileWatcher");
23+
_fileWatcher = new FileSystemWatcher();
24+
_fileWatcher.Path = Path.Combine(Application.dataPath, "Game");
25+
_fileWatcher.IncludeSubdirectories = true;
26+
27+
_fileWatcher.Created += (sender, e) => { UpdateAddressables(e); };
28+
//_fileWatcher.Deleted += (sender, e) => { UpdateAddressables(e); };
29+
_fileWatcher.Renamed += (sender, e) => { UpdateAddressables(e); };
30+
31+
_fileWatcher.EnableRaisingEvents = true;
32+
33+
EditorApplication.update += OnUpdate;
34+
}
35+
36+
private static void OnUpdate()
37+
{
38+
if (!_isRuning)
39+
return;
40+
AssetDatabase.Refresh();
41+
#if ADDRESSABLES_SUPPORT
42+
AddressablesEditor.SetAddressablesAssets();
43+
#endif
44+
_isRuning = false;
45+
}
46+
47+
private static void UpdateAddressables(FileSystemEventArgs e)
48+
{
49+
//Debug.Log($"File watcher: {e.FullPath}");
50+
if (_isRuning)
51+
return;
52+
_isRuning = true;
53+
}
54+
}
55+
}

GameFramework/Editor/AssetManagement/AssetFileWatcher.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GameFramework/Editor/AssetManagement/AssetGroupEditor.cs

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -77,88 +77,90 @@ private void OnMenuInit()
7777
}
7878
else if (itemIndex == 1)
7979
{
80-
var settings = AddressableAssetSettingsDefaultObject.Settings;
81-
if (settings != null)
82-
{
83-
string binPath = ContentUpdateScript.GetContentStateDataPath(false);
84-
if (_config != null && _config.Count > 0)
85-
{
86-
for (int i = 0; i < _config.Count; i++)
87-
{
88-
JsonData item = _config[i];
89-
string groupName = item["GroupName"].ToString();
90-
var group = settings.FindGroup(groupName);
91-
if (group == null)
92-
{
93-
group = settings.CreateGroup(groupName,false,false,false,null);
94-
}
80+
AddressablesEditor.SetAddressablesAssets();
81+
EditorApplication.ExecuteMenuItem("Window/Asset Management/Addressables/Groups");
82+
//var settings = AddressableAssetSettingsDefaultObject.Settings;
83+
//if (settings != null)
84+
//{
85+
// string binPath = ContentUpdateScript.GetContentStateDataPath(false);
86+
// if (_config != null && _config.Count > 0)
87+
// {
88+
// for (int i = 0; i < _config.Count; i++)
89+
// {
90+
// JsonData item = _config[i];
91+
// string groupName = item["GroupName"].ToString();
92+
// var group = settings.FindGroup(groupName);
93+
// if (group == null)
94+
// {
95+
// group = settings.CreateGroup(groupName,false,false,false,null);
96+
// }
9597

96-
ContentUpdateGroupSchema cugSchema = group.GetSchema<ContentUpdateGroupSchema>();
97-
if (cugSchema == null)
98-
{
99-
cugSchema = group.AddSchema<ContentUpdateGroupSchema>();
100-
}
101-
cugSchema.StaticContent = ((int)item["UpdateRestriction"] == 1);
102-
BundledAssetGroupSchema bagSchema =group.GetSchema<BundledAssetGroupSchema>();
103-
if (bagSchema == null)
104-
{
105-
bagSchema= group.AddSchema<BundledAssetGroupSchema>();
106-
}
107-
bagSchema.BuildPath.SetVariableByName(settings, item["BuildPath"].ToString());
108-
bagSchema.LoadPath.SetVariableByName(settings, item["LoadPath"].ToString());
109-
if (cugSchema.StaticContent)
110-
{
111-
bagSchema.UseAssetBundleCrc = false;
112-
bagSchema.UseAssetBundleCrcForCachedBundles = false;
113-
}
98+
// ContentUpdateGroupSchema cugSchema = group.GetSchema<ContentUpdateGroupSchema>();
99+
// if (cugSchema == null)
100+
// {
101+
// cugSchema = group.AddSchema<ContentUpdateGroupSchema>();
102+
// }
103+
// cugSchema.StaticContent = ((int)item["UpdateRestriction"] == 1);
104+
// BundledAssetGroupSchema bagSchema =group.GetSchema<BundledAssetGroupSchema>();
105+
// if (bagSchema == null)
106+
// {
107+
// bagSchema= group.AddSchema<BundledAssetGroupSchema>();
108+
// }
109+
// bagSchema.BuildPath.SetVariableByName(settings, item["BuildPath"].ToString());
110+
// bagSchema.LoadPath.SetVariableByName(settings, item["LoadPath"].ToString());
111+
// if (cugSchema.StaticContent)
112+
// {
113+
// bagSchema.UseAssetBundleCrc = false;
114+
// bagSchema.UseAssetBundleCrcForCachedBundles = false;
115+
// }
114116

115-
//Filter
116-
StringBuilder filterBuilder = new StringBuilder();
117-
for (int filterIndex = 0; filterIndex < item["Filter"].Count; filterIndex++)
118-
{
119-
filterBuilder.Append($"t:{item["Filter"][filterIndex].ToString()} ");
120-
}
121-
//SearchInFolders
122-
List<string> folders = new List<string>();
123-
for (int folderIndex = 0; folderIndex < item["SearchInFolders"].Count; folderIndex++)
124-
{
125-
folders.Add(item["SearchInFolders"][folderIndex].ToString());
126-
}
127-
//Labels
128-
List<string> labels = new List<string>();
129-
for (int labelIndex = 0; labelIndex < item["Labels"].Count; labelIndex++)
130-
{
131-
labels.Add(item["Labels"][labelIndex].ToString());
132-
}
117+
// //Filter
118+
// StringBuilder filterBuilder = new StringBuilder();
119+
// for (int filterIndex = 0; filterIndex < item["Filter"].Count; filterIndex++)
120+
// {
121+
// filterBuilder.Append($"t:{item["Filter"][filterIndex].ToString()} ");
122+
// }
123+
// //SearchInFolders
124+
// List<string> folders = new List<string>();
125+
// for (int folderIndex = 0; folderIndex < item["SearchInFolders"].Count; folderIndex++)
126+
// {
127+
// folders.Add(item["SearchInFolders"][folderIndex].ToString());
128+
// }
129+
// //Labels
130+
// List<string> labels = new List<string>();
131+
// for (int labelIndex = 0; labelIndex < item["Labels"].Count; labelIndex++)
132+
// {
133+
// labels.Add(item["Labels"][labelIndex].ToString());
134+
// }
133135

134-
//Find All Asset
135-
var findAssets = AssetDatabase.FindAssets(filterBuilder.ToString(), folders.ToArray());
136-
for (int findIndex = 0; findIndex < findAssets.Length; findIndex++)
137-
{
138-
string guid = findAssets[findIndex];
139-
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
140-
if (AssetDatabase.IsValidFolder(assetPath)|| assetPath.EndsWith(".cs"))
141-
{
142-
continue;
143-
}
144-
var entry = group.GetAssetEntry(guid);
145-
if (entry == null)
146-
{
147-
entry = settings.CreateOrMoveEntry(guid, group);
148-
}
149-
entry.labels.Clear();
150-
foreach (var itemLabel in labels)
151-
{
152-
entry.SetLabel(itemLabel, true);
153-
}
154-
}
155-
}
156-
}
157-
EditorUtility.SetDirty(settings);
158-
AssetDatabase.Refresh();
136+
// //Find All Asset
137+
// var findAssets = AssetDatabase.FindAssets(filterBuilder.ToString(), folders.ToArray());
138+
// for (int findIndex = 0; findIndex < findAssets.Length; findIndex++)
139+
// {
140+
// string guid = findAssets[findIndex];
141+
// string assetPath = AssetDatabase.GUIDToAssetPath(guid);
142+
// if (AssetDatabase.IsValidFolder(assetPath)|| assetPath.EndsWith(".cs"))
143+
// {
144+
// continue;
145+
// }
146+
// var entry = group.GetAssetEntry(guid);
147+
// if (entry == null)
148+
// {
149+
// entry = settings.CreateOrMoveEntry(guid, group);
150+
// }
151+
// entry.labels.Clear();
152+
// foreach (var itemLabel in labels)
153+
// {
154+
// entry.SetLabel(itemLabel, true);
155+
// }
156+
// }
157+
// }
158+
// }
159+
// EditorUtility.SetDirty(settings);
160+
// AssetDatabase.Refresh();
159161

160-
EditorApplication.ExecuteMenuItem("Window/Asset Management/Addressables/Groups");
161-
}
162+
// EditorApplication.ExecuteMenuItem("Window/Asset Management/Addressables/Groups");
163+
//}
162164
}
163165
else
164166
{

0 commit comments

Comments
 (0)