forked from liuhaopen/UGUI-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextMenu.cs
201 lines (176 loc) · 7.35 KB
/
ContextMenu.cs
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
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace U3DExtends {
static public class ContextMenu
{
static List<string> mEntries = new List<string>();
static GenericMenu mMenu;
static public void AddItem(string item, bool isChecked, GenericMenu.MenuFunction callback)
{
if (callback != null)
{
if (mMenu == null) mMenu = new GenericMenu();
int count = 0;
for (int i = 0; i < mEntries.Count; ++i)
{
string str = mEntries[i];
if (str == item) ++count;
}
mEntries.Add(item);
if (count > 0) item += " [" + count + "]";
mMenu.AddItem(new GUIContent(item), isChecked, callback);
}
else AddDisabledItem(item);
}
static public void AddItemWithArge(string item, bool isChecked, GenericMenu.MenuFunction2 callback, object arge)
{
if (callback != null)
{
if (mMenu == null) mMenu = new GenericMenu();
int count = 0;
for (int i = 0; i < mEntries.Count; ++i)
{
string str = mEntries[i];
if (str == item) ++count;
}
mEntries.Add(item);
if (count > 0) item += " [" + count + "]";
mMenu.AddItem(new GUIContent(item), isChecked, callback, arge);
}
else AddDisabledItem(item);
}
static public void Show()
{
if (mMenu != null)
{
mMenu.ShowAsContext();
mMenu = null;
mEntries.Clear();
}
}
//增加几种对齐菜单
static public void AddAlignMenu()
{
AddItem("对齐/左对齐 ←", false, AlignTool.AlignInHorziontalLeft);
AddItem("对齐/右对齐 →", false, AlignTool.AlignInHorziontalRight);
AddItem("对齐/上对齐 ↑", false, AlignTool.AlignInVerticalUp);
AddItem("对齐/下对齐 ↓", false, AlignTool.AlignInVerticalDown);
AddItem("对齐/水平均匀 |||", false, AlignTool.UniformDistributionInHorziontal);
AddItem("对齐/垂直均匀 ☰", false, AlignTool.UniformDistributionInVertical);
AddItem("对齐/一样大 ■", false, AlignTool.ResizeMax);
AddItem("对齐/一样小 ●", false, AlignTool.ResizeMin);
}
//增加层次菜单
static public void AddPriorityMenu()
{
AddItem("层次/最里层 ↑↑↑", false, PriorityTool.MoveToTopWidget);
AddItem("层次/最外层 ↓↓↓", false, PriorityTool.MoveToBottomWidget);
AddItem("层次/往里挤 ↑", false, PriorityTool.MoveUpWidget);
AddItem("层次/往外挤 ↓", false, PriorityTool.MoveDownWidget);
}
//增加UI控件菜单
static public void AddUIMenu()
{
AddItem("添加控件/Empty", false, UIEditorHelper.CreateEmptyObj);
AddItem("添加控件/Image", false, UIEditorHelper.CreateImageObj);
AddItem("添加控件/RawImage", false, UIEditorHelper.CreateRawImageObj);
AddItem("添加控件/Button", false, UIEditorHelper.CreateButtonObj);
AddItem("添加控件/Text", false, UIEditorHelper.CreateTextObj);
AddItem("添加控件/HScroll", false, UIEditorHelper.CreateHScrollViewObj);
AddItem("添加控件/VScroll", false, UIEditorHelper.CreateVScrollViewObj);
}
//增加UI组件菜单
static public void AddUIComponentMenu()
{
AddItem("添加组件/Image", false, UIEditorHelper.AddImageComponent);
//AddItem("添加组件/RawImage", false, UIEditorHelper.CreateRawImageObj);
//AddItem("添加组件/Button", false, UIEditorHelper.CreateButtonObj);
//AddItem("添加组件/Text", false, UIEditorHelper.CreateTextObj);
AddItem("添加组件/HLayout", false, UIEditorHelper.AddHorizontalLayoutComponent);
AddItem("添加组件/VLayout", false, UIEditorHelper.AddVerticalLayoutComponent);
AddItem("添加组件/GridLayout", false, UIEditorHelper.AddGridLayoutGroupComponent);
}
//增加显示隐藏菜单
static public void AddShowOrHideMenu()
{
bool hasHideWidget = false;
foreach (var item in Selection.gameObjects)
{
if (!item.activeSelf)
{
hasHideWidget = true;
break;
}
}
if (hasHideWidget)
AddItem("显示", false, UILayoutTool.ShowAllSelectedWidgets);
else
AddItem("隐藏", false, UILayoutTool.HideAllSelectedWidgets);
}
static public void AddCommonItems(GameObject[] targets)
{
if (targets == null || targets.Length <= 0)
{
AddItem("新建", false, UIEditorHelper.CreatNewLayoutForMenu);
AddItem("打开界面", false, UIEditorHelper.LoadLayout);
AddItem("打开文件夹", false, UIEditorHelper.LoadLayoutWithFolder);
}
if (targets != null && targets.Length > 0)
{
AddItem("保存", false, UIEditorHelper.SaveLayoutForMenu);
AddItem("另存为", false, UIEditorHelper.SaveAnotherLayoutContextMenu);
AddItem("重新加载", false, UIEditorHelper.ReLoadLayoutForMenu);
AddSeparator("///");
AddItem("复制选中控件名", false, UIEditorHelper.CopySelectWidgetName);
//如果选中超过1个节点的话
if (targets.Length > 1)
{
AddAlignMenu();
AddItem("同流合污", false, UILayoutTool.MakeGroup);
}
AddSeparator("///");
if (targets.Length == 1)
{
AddUIMenu();
AddUIComponentMenu();
AddPriorityMenu();
if (UIEditorHelper.IsNodeCanDivide(targets[0]))
AddItem("分道扬镖", false, UILayoutTool.UnGroup);
Decorate uiBase = targets[0].GetComponent<Decorate>();
if (uiBase != null)
{
if (uiBase.gameObject.hideFlags == HideFlags.NotEditable)
{
AddItem("解锁", false, UIEditorHelper.UnLockWidget);
}
else
{
AddItem("锁定", false, UIEditorHelper.LockWidget);
}
}
}
AddShowOrHideMenu();
AddSeparator("///");
AddItem("添加参考图", false, UIEditorHelper.CreateDecorate);
if (targets.Length == 1 && targets[0].transform.childCount > 0)
AddItem("优化层级", false, UILayoutTool.OptimizeBatchForMenu);
}
AddItem("排序所有界面", false, UILayoutTool.ResortAllLayout);
AddItem("清空界面", false, UIEditorHelper.ClearAllCanvas);
}
static public void AddSeparator(string path)
{
if (mMenu == null) mMenu = new GenericMenu();
if (Application.platform != RuntimePlatform.OSXEditor)
mMenu.AddSeparator(path);
}
static public void AddDisabledItem(string item)
{
if (mMenu == null) mMenu = new GenericMenu();
mMenu.AddDisabledItem(new GUIContent(item));
}
}
}
#endif