Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 64cb2c4

Browse files
author
wanderer
committed
ResourceManager添加图集请求的事件,并自动异步加载资源
1 parent 3e7c10b commit 64cb2c4

File tree

4 files changed

+119
-95
lines changed

4 files changed

+119
-95
lines changed
Lines changed: 101 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Reflection;
45
using UnityEditor;
56
using UnityEditor.U2D;
@@ -11,42 +12,59 @@ namespace Wanderer.GameFramework
1112

1213
public class PrefabSpritePacker
1314
{
15+
private static SpriteAtlasPackingSettings _packingSettings = new SpriteAtlasPackingSettings();
16+
private static TextureImporterPlatformSettings _androidSettings = new TextureImporterPlatformSettings();
17+
private static TextureImporterPlatformSettings _iOSSettings = new TextureImporterPlatformSettings();
18+
1419
[MenuItem("Assets/Prefab To SpriteAtlas")]
1520
private static void PrefabToSpriteAtlas()
1621
{
17-
string selectPath = AssetDatabase.GetAssetPath(Selection.activeObject);
18-
if (AssetDatabase.IsValidFolder(selectPath))
19-
{
20-
var assets = AssetDatabase.FindAssets("t:Prefab", new string[] { selectPath });
21-
if (assets != null)
22-
{
23-
HashSet<Object> objects = new HashSet<Object>();
24-
foreach (var item in assets)
22+
if (Selection.objects == null)
23+
return;
24+
25+
bool selectFolder = false;
26+
27+
if (Selection.assetGUIDs.Length == 1)
28+
{
29+
string selectPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
30+
if (AssetDatabase.IsValidFolder(selectPath))
31+
{
32+
selectFolder = true;
33+
34+
var assets = AssetDatabase.FindAssets("t:Prefab", new string[] { selectPath });
35+
if (assets != null)
2536
{
26-
stringassetPath=AssetDatabase.GUIDToAssetPath(item);
27-
foreach (var item02 in GameObjectGetDependencies(AssetDatabase.LoadAssetAtPath<GameObject>(assetPath)))
37+
HashSet<Object>objects=newHashSet<Object>();
38+
foreach (var item in assets)
2839
{
29-
objects.Add(item02);
30-
}
40+
string assetPath = AssetDatabase.GUIDToAssetPath(item);
41+
Debug.Log(assetPath);
42+
foreach (var item02 in GameObjectGetDependencies(AssetDatabase.LoadAssetAtPath<GameObject>(assetPath)))
43+
{
44+
objects.Add(item02);
45+
}
46+
}
47+
string fileName = Path.GetFileNameWithoutExtension(selectPath);
48+
SaveSpriteAtlas(objects, $"{selectPath}/{fileName}_spriteatlas.spriteatlas");
3149
}
32-
SaveSpriteAtlas(objects, $"{selectPath}/SpriteAtlas.spriteatlas");
33-
}
34-
}
35-
else
36-
{
37-
GameObject[] go = Selection.gameObjects;
38-
if (go == null)
39-
return;
40-
foreach (GameObject g in go)
41-
{
42-
GameObjectToSpriteAtlas(g);
50+
4351
}
4452
}
53+
//没有选择文件夹
54+
if (!selectFolder)
55+
{
56+
GameObject[] go = Selection.gameObjects;
57+
if (go == null)
58+
return;
59+
foreach (GameObject g in go)
60+
{
61+
GameObjectToSpriteAtlas(g);
62+
}
63+
}
4564
}
4665

47-
48-
[MenuItem("Assets/SpriteAtlas Not In Build")]
49-
private static void SpriteAtlasNotInBuild()
66+
[MenuItem("Assets/SpriteAtlas Global Setting")]
67+
private static void SpriteAtlasGlobalSetting()
5068
{
5169
var assets = AssetDatabase.FindAssets("t:SpriteAtlas");
5270
if (assets != null)
@@ -57,14 +75,17 @@ private static void SpriteAtlasNotInBuild()
5775
SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(assetPath);
5876
if (spriteAtlas != null)
5977
{
60-
spriteAtlas.SetIncludeInBuild(false);
78+
SetSpriteAtlas(spriteAtlas);
6179
}
6280
}
6381
AssetDatabase.Refresh();
6482
}
6583
}
6684

67-
85+
/// <summary>
86+
/// GameObject设置SpriteAtlas
87+
/// </summary>
88+
/// <param name="go"></param>
6889
private static void GameObjectToSpriteAtlas(GameObject go)
6990
{
7091
string assetPath = AssetDatabase.GetAssetPath(go);
@@ -73,49 +94,48 @@ private static void GameObjectToSpriteAtlas(GameObject go)
7394
{
7495
objects.Add(item);
7596
}
76-
SaveSpriteAtlas(objects, $"{assetPath}.spriteatlas");
97+
SaveSpriteAtlas(objects, $"{assetPath}_spriteatlas.spriteatlas");
7798
}
7899

100+
/// <summary>
101+
/// 获取所有的引用
102+
/// </summary>
103+
/// <param name="go"></param>
104+
/// <returns></returns>
79105
private static List<Object> GameObjectGetDependencies(GameObject go)
80106
{
81107
string assetPath = AssetDatabase.GetAssetPath(go);
82108
string[] dependencies = AssetDatabase.GetDependencies(assetPath);
83109
if (dependencies != null)
84110
{
85-
List<Object> depSprites = new List<Object>();
111+
List<Object> depObjects = new List<Object>();
86112
foreach (var item in dependencies)
87113
{
88114
// string _otherGUID = AssetDatabase.AssetPathToGUID(item);
89-
var itemSprite = AssetDatabase.LoadAssetAtPath<Object>(item);
90-
if (itemSprite is Sprite || itemSprite is Texture2D)
115+
var itemObject = AssetDatabase.LoadAssetAtPath<Object>(item);
116+
if (itemObject is Sprite || itemObject is Texture2D)
91117
{
92-
depSprites.Add(itemSprite);
118+
if (itemObject != null)
119+
{
120+
depObjects.Add(itemObject);
121+
}
93122
}
94123
}
95-
return depSprites;
124+
return depObjects;
96125
}
97126
return new List<Object>();
98127
}
99128

100129

130+
/// <summary>
131+
/// 保存SpriteAtlas
132+
/// </summary>
133+
/// <param name="objs"></param>
134+
/// <param name="saPath"></param>
101135
public static void SaveSpriteAtlas(HashSet<Object> objs, string saPath)
102136
{
103137
if (objs == null || objs.Count == 0)
104138
return;
105-
106-
TextureImporterPlatformSettings androidSettings = new TextureImporterPlatformSettings();
107-
androidSettings.name = "Android";
108-
androidSettings.overridden = true;
109-
androidSettings.maxTextureSize = 4096;
110-
androidSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
111-
androidSettings.format = TextureImporterFormat.ETC2_RGBA8;
112-
113-
TextureImporterPlatformSettings iOSSettings = new TextureImporterPlatformSettings();
114-
iOSSettings.name = "iOS";
115-
iOSSettings.overridden = true;
116-
iOSSettings.maxTextureSize = 4096;
117-
iOSSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
118-
iOSSettings.format = TextureImporterFormat.PVRTC_RGBA4;
119139

120140
// SpriteAtlasUtility.
121141
Object[] objArray = new Object[objs.Count];
@@ -125,42 +145,42 @@ public static void SaveSpriteAtlas(HashSet<Object> objs, string saPath)
125145
objArray[index] = item;
126146
index++;
127147
}
128-
SpriteAtlas spriteAtlas = new SpriteAtlas();
129-
spriteAtlas.SetIncludeInBuild(false);
130-
spriteAtlas.Add(objArray);
131-
132-
spriteAtlas.SetPlatformSettings(androidSettings);
133-
// spriteAtlas.SetPlatformSettings(iOSSettings);
134-
135-
AssetDatabase.CreateAsset(spriteAtlas, saPath);
136-
AssetDatabase.Refresh();
137-
138-
spriteAtlas.SetPlatformSettings(iOSSettings);
139-
AssetDatabase.Refresh();
140-
// int result = spriteAtlas.GetSprites(depSprites.ToArray());
141-
// Debug.Log($"GetSprites:{result}");
148+
SpriteAtlas spriteAtlas = new SpriteAtlas();
149+
spriteAtlas.Add(objArray);
150+
SetSpriteAtlas(spriteAtlas);
151+
AssetDatabase.CreateAsset(spriteAtlas, saPath);
152+
AssetDatabase.Refresh();
142153
}
143154

155+
144156

145-
//static void AddPackAtlas(SpriteAtlas atlas, Object[] spt)
146-
//{
147-
// MethodInfo methodInfo = System.Type
148-
// .GetType("UnityEditor.U2D.SpriteAtlasExtensions, UnityEditor")
149-
// .GetMethod("Add", BindingFlags.Public | BindingFlags.Static);
150-
// if (methodInfo != null)
151-
// methodInfo.Invoke(null, new object[] { atlas, spt });
152-
// else
153-
// Debug.Log("methodInfo is null");
154-
// // PackAtlas(atlas);
155-
//}
156-
157-
//static void PackAtlas(SpriteAtlas atlas)
158-
//{
159-
// System.Type
160-
// .GetType("UnityEditor.U2D.SpriteAtlasUtility, UnityEditor")
161-
// .GetMethod("PackAtlases", BindingFlags.NonPublic | BindingFlags.Static)
162-
// .Invoke(null, new object[] { new[] { atlas }, EditorUserBuildSettings.activeBuildTarget });
163-
//}
157+
/// <summary>
158+
/// 设置图集参数
159+
/// </summary>
160+
/// <param name="spriteAtlas"></param>
161+
private static void SetSpriteAtlas(SpriteAtlas spriteAtlas)
162+
{
163+
_packingSettings.enableTightPacking = false;
164+
_packingSettings.enableRotation = false;
165+
_packingSettings.padding = 4;
166+
167+
_androidSettings.name = "Android";
168+
_androidSettings.overridden = true;
169+
// androidSettings.maxTextureSize = 2048;
170+
// _androidSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
171+
_androidSettings.format = TextureImporterFormat.ETC2_RGBA8;
172+
173+
_iOSSettings.name = "iOS";
174+
_iOSSettings.overridden = true;
175+
//iOSSettings.maxTextureSize = 2048;
176+
// _iOSSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
177+
_iOSSettings.format = TextureImporterFormat.PVRTC_RGBA4;
178+
179+
spriteAtlas.SetIncludeInBuild(false);
180+
spriteAtlas.SetPackingSettings(_packingSettings);
181+
spriteAtlas.SetPlatformSettings(_androidSettings);
182+
spriteAtlas.SetPlatformSettings(_iOSSettings);
183+
}
164184

165185
}
166186
}

‎GameFramework/Runtime/Resource/ResourceManager.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Threading.Tasks;
1616
using System;
1717
using System.IO;
18+
using UnityEngine.U2D;
1819

1920
namespace Wanderer.GameFramework
2021
{
@@ -96,11 +97,25 @@ public string LocalPath
9697
/// </summary>
9798
/// <value></value>
9899
public ResourceVersion Version { get; private set; }
100+
99101
#endregion
100102

101103
#region 构造函数
102104
public ResourceManager()
103105
{
106+
//自动监听图集请求,并异步加载图集资源
107+
SpriteAtlasManager.atlasRequested += (tag, callback) => {
108+
string assetPath = Asset.AllAssetPaths.Find(x =>x.EndsWith($"{tag.ToLower()}.spriteatlas"));
109+
if (!string.IsNullOrEmpty(assetPath))
110+
{
111+
Asset.LoadAsset<SpriteAtlas>(assetPath, callback);
112+
}
113+
else
114+
{
115+
throw new GameException($"The path to the SpriteAtlas file could not be found. {tag}");
116+
}
117+
};
118+
104119
//获取事件管理器
105120
_event = GameFrameworkMode.GetModule<EventManager>();
106121

@@ -154,8 +169,10 @@ public void SetResourceHelper(IAssetsHelper resourceHelper)
154169
{
155170
Asset?.Clear();
156171
Asset = resourceHelper;
172+
157173
}
158174

175+
159176
/// <summary>
160177
/// 设置对象池管理器的
161178
/// </summary>
@@ -262,6 +279,7 @@ public void OnUpdate()
262279

263280
public override void OnClose()
264281
{
282+
//资源清理
265283
Asset?.Clear();
266284
if (_gameObjectPoolHelper != null)
267285
_gameObjectPoolHelper.DestroyAll();

‎Libraries/7zip/Compress/LzmaAlone/LzmaAlone.csproj.meta‎

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

‎Libraries/7zip/Compress/LzmaAlone/LzmaAlone.sln.meta‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /