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 3e7c10b

Browse files
author
wanderer
committed
增加SpriteAtlas,增加PrefabSpritePacker
1 parent 5c98207 commit 3e7c10b

File tree

4 files changed

+233
-16
lines changed

4 files changed

+233
-16
lines changed

‎GameFramework/Editor/AssetBundleEditor/AssetBundleEditor.cs‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class AssetBundleEditor : EditorWindow
3333
"Shader",
3434
"Sprite",
3535
"Texture",
36-
"VideoClip","TextAsset","ScriptableObject","AnimatorController"};
36+
"VideoClip","TextAsset","ScriptableObject","AnimatorController","SpriteAtlas"};
3737

3838
//窗口的位置
3939
private Rect _foldersWindowRect = Rect.zero;
@@ -429,27 +429,41 @@ private void DrawJsonData(JsonData jsonData)
429429
}
430430

431431
key = "SearchInFolders";
432-
JsonData selectFoldersJsonData = jsonData.Keys.Contains(key) ? jsonData[key] : null;
433-
string buttonContent = "No data";
434-
if (selectFoldersJsonData != null && selectFoldersJsonData.Count > 0)
432+
if (!jsonData.Keys.Contains(key))
435433
{
436-
buttonContent = selectFoldersJsonData[0].ToString();
437-
if (selectFoldersJsonData.Count > 1)
438-
{
439-
buttonContent = $"[{selectFoldersJsonData.Count}] {buttonContent}...";
440-
}
434+
jsonData[key] = new JsonData();
435+
jsonData[key].SetJsonType(JsonType.Array);
441436
}
442-
if (GUILayout.Button(buttonContent,GUILayout.Width(300))&&!_haspopupFoldersWindow)
437+
JsonData selectFoldersJsonData = jsonData[key];
438+
content = selectFoldersJsonData.ToJson();
439+
newContent = EditorGUILayout.TextField(content, GUILayout.Width(300));
440+
if (!content.Equals(newContent))
443441
{
444-
_foldersWindowRect = new Rect(Event.current.mousePosition, Vector2.one * 200);
445-
_selectFoldersJsonData = selectFoldersJsonData;
446-
if (_selectFoldersJsonData == null)
442+
var jd = JsonMapper.ToObject(newContent);
443+
if (jd != null)
447444
{
448-
_selectFoldersJsonData = new JsonData();
449-
_selectFoldersJsonData.SetJsonType(JsonType.Array);
450-
jsonData[key] = _selectFoldersJsonData;
445+
jsonData[key] = jd;
451446
}
452447
}
448+
//if (selectFoldersJsonData != null && selectFoldersJsonData.Count > 0)
449+
//{
450+
// buttonContent = selectFoldersJsonData[0].ToString();
451+
// if (selectFoldersJsonData.Count > 1)
452+
// {
453+
// buttonContent = $"[{selectFoldersJsonData.Count}] {buttonContent}...";
454+
// }
455+
//}
456+
//if (GUILayout.Button(buttonContent,GUILayout.Width(300))&&!_haspopupFoldersWindow)
457+
//{
458+
// _foldersWindowRect = new Rect(Event.current.mousePosition, Vector2.one * 200);
459+
// _selectFoldersJsonData = selectFoldersJsonData;
460+
// if (_selectFoldersJsonData == null)
461+
// {
462+
// _selectFoldersJsonData = new JsonData();
463+
// _selectFoldersJsonData.SetJsonType(JsonType.Array);
464+
// jsonData[key] = _selectFoldersJsonData;
465+
// }
466+
//}
453467
key = "Split";
454468
if (!jsonData.Keys.Contains(key))
455469
{

‎GameFramework/Editor/Other/BinaryToText.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ static void OpenWindow()
1818

1919
private void OnGUI()
2020
{
21+
if (GUILayout.Button("Save As"))
22+
{
23+
string file = EditorUtility.OpenFilePanel("open file", "", "*");
24+
if (!string.IsNullOrEmpty(file))
25+
{
26+
string saveFile = EditorUtility.SaveFilePanel("save file","","","");
27+
if (!string.IsNullOrEmpty(saveFile))
28+
{
29+
int bufferCount = 1024 * 4;
30+
using (EncryptFileStream fileStream = new EncryptFileStream(file, FileMode.Open, FileAccess.Read, FileShare.None, bufferCount, false))
31+
{
32+
using (FileStream writeStream = new FileStream(saveFile, FileMode.OpenOrCreate,FileAccess.Write,FileShare.None, bufferCount))
33+
{
34+
byte[] buffer = new byte[bufferCount];
35+
int count;
36+
do
37+
{
38+
count = fileStream.Read(buffer, 0, buffer.Length);
39+
writeStream.Write(buffer, 0, count);
40+
}
41+
while (count>0);
42+
}
43+
}
44+
}
45+
}
46+
}
2147
if (GUILayout.Button("Open File"))
2248
{
2349
string file = EditorUtility.OpenFilePanel("open file","","*");
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using UnityEditor;
5+
using UnityEditor.U2D;
6+
using UnityEngine;
7+
using UnityEngine.U2D;
8+
9+
namespace Wanderer.GameFramework
10+
{
11+
12+
public class PrefabSpritePacker
13+
{
14+
[MenuItem("Assets/Prefab To SpriteAtlas")]
15+
private static void PrefabToSpriteAtlas()
16+
{
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)
25+
{
26+
string assetPath = AssetDatabase.GUIDToAssetPath(item);
27+
foreach (var item02 in GameObjectGetDependencies(AssetDatabase.LoadAssetAtPath<GameObject>(assetPath)))
28+
{
29+
objects.Add(item02);
30+
}
31+
}
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);
43+
}
44+
}
45+
}
46+
47+
48+
[MenuItem("Assets/SpriteAtlas Not In Build")]
49+
private static void SpriteAtlasNotInBuild()
50+
{
51+
var assets = AssetDatabase.FindAssets("t:SpriteAtlas");
52+
if (assets != null)
53+
{
54+
foreach (var item in assets)
55+
{
56+
string assetPath = AssetDatabase.GUIDToAssetPath(item);
57+
SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(assetPath);
58+
if (spriteAtlas != null)
59+
{
60+
spriteAtlas.SetIncludeInBuild(false);
61+
}
62+
}
63+
AssetDatabase.Refresh();
64+
}
65+
}
66+
67+
68+
private static void GameObjectToSpriteAtlas(GameObject go)
69+
{
70+
string assetPath = AssetDatabase.GetAssetPath(go);
71+
HashSet<Object> objects = new HashSet<Object>();
72+
foreach (var item in GameObjectGetDependencies(go))
73+
{
74+
objects.Add(item);
75+
}
76+
SaveSpriteAtlas(objects, $"{assetPath}.spriteatlas");
77+
}
78+
79+
private static List<Object> GameObjectGetDependencies(GameObject go)
80+
{
81+
string assetPath = AssetDatabase.GetAssetPath(go);
82+
string[] dependencies = AssetDatabase.GetDependencies(assetPath);
83+
if (dependencies != null)
84+
{
85+
List<Object> depSprites = new List<Object>();
86+
foreach (var item in dependencies)
87+
{
88+
// string _otherGUID = AssetDatabase.AssetPathToGUID(item);
89+
var itemSprite = AssetDatabase.LoadAssetAtPath<Object>(item);
90+
if (itemSprite is Sprite || itemSprite is Texture2D)
91+
{
92+
depSprites.Add(itemSprite);
93+
}
94+
}
95+
return depSprites;
96+
}
97+
return new List<Object>();
98+
}
99+
100+
101+
public static void SaveSpriteAtlas(HashSet<Object> objs, string saPath)
102+
{
103+
if (objs == null || objs.Count == 0)
104+
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;
119+
120+
// SpriteAtlasUtility.
121+
Object[] objArray = new Object[objs.Count];
122+
int index = 0;
123+
foreach (var item in objs)
124+
{
125+
objArray[index] = item;
126+
index++;
127+
}
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}");
142+
}
143+
144+
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+
//}
164+
165+
}
166+
}

‎GameFramework/Editor/Other/PrefabSpritePacker.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.

0 commit comments

Comments
(0)

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