-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteChooseEditorForUGUI
More file actions
224 lines (176 loc) · 7.54 KB
/
SpriteChooseEditorForUGUI
File metadata and controls
224 lines (176 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine.UI;
public class SpriteChooseEditor : EditorWindow
{
[MenuItem("Tools/UI/SpriteChooseWnd")]
public static void OpenChooseWnd()
{
GetWindow<SpriteChooseEditor>();
}
void OnEnable()
{
}
void OnGUI()
{
DrawSprites("Assets/Resources/atlas/Battle.png");
}
private Vector2 scroll_pos = Vector2.zero;
private string select_ui_name;
private string cur_select_sprite_name;
private int SPRITE_LABEL_HEIGHT = 22;
private int SPRITE_SIZE = 100;
const int RIGHT_SCROLL_GAP = 10; //滚动条距离窗口右边框的距离
private string search_text = "";
void DrawSprites(string atlas_path)
{
GUILayout.BeginVertical();
GUILayout.Space(10);
if (!File.Exists(atlas_path))
return;
UnityEngine.Object[] asset_list = AssetDatabase.LoadAllAssetRepresentationsAtPath(atlas_path);
List<Sprite> sprite_list = new List<Sprite>();
for (int index = 0; index < asset_list.Length; ++index)
{
Sprite handle_sprite = asset_list[index] as Sprite;
if (handle_sprite == null)
continue;
sprite_list.Add(handle_sprite);
}
int offset = (Screen.width - 190 - 218) / 2;
GUILayout.BeginHorizontal();
GUILayout.Space(offset);
string before = search_text;
string after = EditorGUILayout.TextField("", before, "SearchTextField", GUILayout.Width(200f));
if (before != after) search_text = after;
if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f)))
{
search_text = "";
GUIUtility.keyboardControl = 0;
}
GUILayout.Space(offset);
GUILayout.EndHorizontal();
List<Sprite> show_sprite_list = GetListOfSprites(sprite_list, search_text);
GUILayout.Space(10);
scroll_pos = GUILayout.BeginScrollView(scroll_pos, GUILayout.Width(Screen.width - RIGHT_SCROLL_GAP));
int col_show_count = (Screen.width - RIGHT_SCROLL_GAP) / (SPRITE_SIZE + 10);
int total_count = show_sprite_list.Count;
int row_index = 0;
for (int sprite_index = 0; sprite_index < total_count; )
{
GUILayout.BeginHorizontal();
for (int col_index = 0; col_index < col_show_count && sprite_index < total_count; ++col_index)
{
DrawSprite(row_index, col_index, show_sprite_list[sprite_index]);
++sprite_index;
}
GUILayout.EndHorizontal();
row_index++;
}
GUILayout.Space(row_index * (SPRITE_SIZE + SPRITE_LABEL_HEIGHT + 2));
GUILayout.EndScrollView();
GUILayout.Space(10);
GUILayout.EndVertical();
}
void DrawSprite(int row_index, int col_index, Sprite sprite)
{
Texture2D handle_texture = sprite.texture;
GUILayout.BeginVertical();
Rect uv = new Rect(sprite.rect.x / handle_texture.width, sprite.rect.y / handle_texture.height,
sprite.rect.width / handle_texture.width, sprite.rect.height / handle_texture.height);
Rect draw_rect = new Rect(col_index * (SPRITE_SIZE + 10), row_index * (SPRITE_SIZE + SPRITE_LABEL_HEIGHT + 2), SPRITE_SIZE, SPRITE_SIZE);
GUI.backgroundColor = new Color(.6f, 1.0f, 1.0f, 0.5f);
if (GUI.Button(draw_rect, ""))
{
cur_select_sprite_name = sprite.name;
foreach (GameObject handle_object in Selection.gameObjects)
{
Image handle_image = handle_object.GetComponent<Image>();
if (handle_image == null)
continue;
handle_image.sprite = sprite;
string image_path = AssetDatabase.GetAssetPath(handle_image.sprite);
string handle_path = Path.GetDirectoryName(image_path);
string handle_name = Path.GetFileNameWithoutExtension(image_path);
//string mat_name = CommonEditUtil.CombinePath(handle_path, handle_name + "Mat.mat");
//handle_image.material = AssetDatabase.LoadAssetAtPath<Material>(mat_name);
// RectTransform handle_rect = handle_object.GetComponent<RectTransform>();
// handle_rect.sizeDelta = sprite.rect.size;
EditorUtility.SetDirty(handle_object);
}
}
GUI.backgroundColor = Color.white;
if (cur_select_sprite_name == sprite.name)
DrawRectOutline(draw_rect, Color.green);
float scale = sprite.rect.width / sprite.rect.height;
float sprite_width = sprite.rect.width;
float sprite_height = sprite.rect.height;
if (sprite_width < sprite_height)
{
draw_rect.height = SPRITE_SIZE;
draw_rect.width = SPRITE_SIZE * scale;
draw_rect.x += (SPRITE_SIZE - draw_rect.width) / 2;
}
else
{
draw_rect.width = SPRITE_SIZE;
draw_rect.height = SPRITE_SIZE / scale;
draw_rect.y += (SPRITE_SIZE - draw_rect.height) / 2;
}
GUI.DrawTextureWithTexCoords(draw_rect, handle_texture, uv);
GUI.Label(new Rect(col_index * (SPRITE_SIZE + 10), row_index * (SPRITE_SIZE + SPRITE_LABEL_HEIGHT + 2) + SPRITE_SIZE + 2, SPRITE_SIZE, SPRITE_LABEL_HEIGHT), sprite.name, "ProgressBarBack");
GUILayout.EndVertical();
}
static public void DrawRectOutline(Rect rect, Color color)
{
if (Event.current.type == EventType.Repaint)
{
Texture2D tex = EditorGUIUtility.whiteTexture;
GUI.color = color;
GUI.DrawTexture(new Rect(rect.xMin, rect.yMin, 1f, rect.height), tex);
GUI.DrawTexture(new Rect(rect.xMax, rect.yMin, 1f, rect.height), tex);
GUI.DrawTexture(new Rect(rect.xMin, rect.yMin, rect.width, 1f), tex);
GUI.DrawTexture(new Rect(rect.xMin, rect.yMax, rect.width, 1f), tex);
GUI.color = Color.white;
}
}
List<Sprite> GetListOfSprites(List<Sprite> sprite_list, string match)
{
if (string.IsNullOrEmpty(match)) return sprite_list;
List<Sprite> list = new List<Sprite>();
// First try to find an exact match
for (int i = 0, imax = sprite_list.Count; i < imax; ++i)
{
Sprite s = sprite_list[i];
if (s != null && !string.IsNullOrEmpty(s.name) && string.Equals(match, s.name, StringComparison.OrdinalIgnoreCase))
{
list.Add(s);
return list;
}
}
// No exact match found? Split up the search into space-separated components.
string[] keywords = match.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < keywords.Length; ++i) keywords[i] = keywords[i].ToLower();
// Try to find all sprites where all keywords are present
for (int i = 0, imax = sprite_list.Count; i < imax; ++i)
{
Sprite s = sprite_list[i];
if (s != null && !string.IsNullOrEmpty(s.name))
{
string tl = s.name.ToLower();
int matches = 0;
for (int b = 0; b < keywords.Length; ++b)
{
if (tl.Contains(keywords[b])) ++matches;
}
if (matches == keywords.Length) list.Add(s);
}
}
return list;
}
}