forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildPlayerSceneTreeView.cs
More file actions
323 lines (284 loc) · 11.5 KB
/
BuildPlayerSceneTreeView.cs
File metadata and controls
323 lines (284 loc) · 11.5 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEditor.IMGUI.Controls;
namespace UnityEditor
{
internal class BuildPlayerSceneTreeViewItem : TreeViewItem
{
private const string kAssetsFolder = "Assets/";
private const string kSceneExtension = ".unity";
public static int kInvalidCounter = -1;
public bool active;
public int counter;
public string fullName;
public GUID guid;
public void UpdateName()
{
var name = AssetDatabase.GUIDToAssetPath(guid.ToString());
if (name != fullName)
{
fullName = name;
displayName = fullName;
if (displayName.StartsWith(kAssetsFolder))
displayName = displayName.Remove(0, kAssetsFolder.Length);
var ext = displayName.LastIndexOf(kSceneExtension);
if (ext > 0)
displayName = displayName.Substring(0, ext);
}
}
public BuildPlayerSceneTreeViewItem(int id, int depth, GUID g, bool state) : base(id, depth)
{
active = state;
counter = kInvalidCounter;
guid = g;
fullName = "";
UpdateName();
}
}
internal class BuildPlayerSceneTreeView : TreeView
{
public BuildPlayerSceneTreeView(TreeViewState state) : base(state)
{
showBorder = true;
EditorBuildSettings.sceneListChanged += HandleExternalSceneListChange;
}
internal void UnsubscribeListChange()
{
EditorBuildSettings.sceneListChanged -= HandleExternalSceneListChange;
}
private void HandleExternalSceneListChange()
{
Reload();
}
protected override TreeViewItem BuildRoot()
{
var root = new TreeViewItem(-1, -1);
root.children = new List<TreeViewItem>();
List<EditorBuildSettingsScene> scenes = new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes);
foreach (var sc in scenes)
{
var item = new BuildPlayerSceneTreeViewItem(sc.guid.GetHashCode(), 0, sc.guid, sc.enabled);
root.AddChild(item);
}
return root;
}
protected override bool CanBeParent(TreeViewItem item)
{
return false;
}
protected override void BeforeRowsGUI()
{
int counter = 0;
foreach (var item in rootItem.children)
{
var bpst = item as BuildPlayerSceneTreeViewItem;
if (bpst == null)
continue;
bpst.UpdateName();
//Need to set counter here because RowGUI is only called on items that are visible.
if (bpst.active)
{
bpst.counter = counter;
counter++;
}
else
bpst.counter = BuildPlayerSceneTreeViewItem.kInvalidCounter;
}
base.BeforeRowsGUI();
}
protected override void RowGUI(RowGUIArgs args)
{
var sceneItem = args.item as BuildPlayerSceneTreeViewItem;
if (sceneItem != null)
{
var sceneWasDeleted = sceneItem.guid.Empty();
var sceneExists = !sceneWasDeleted && File.Exists(sceneItem.fullName);
using (new EditorGUI.DisabledScope(!sceneExists))
{
var newState = sceneItem.active;
if (!sceneExists)
newState = false;
newState = GUI.Toggle(new Rect(args.rowRect.x, args.rowRect.y, 16f, 16f), newState, "");
if (newState != sceneItem.active)
{
if (GetSelection().Contains(sceneItem.id))
{
var selection = GetSelection();
foreach (var id in selection)
{
var item = FindItem(id, rootItem) as BuildPlayerSceneTreeViewItem;
item.active = newState;
}
}
else
{
sceneItem.active = newState;
}
EditorBuildSettings.scenes = GetSceneList();
}
base.RowGUI(args);
if (sceneItem.counter != BuildPlayerSceneTreeViewItem.kInvalidCounter)
{
TreeView.DefaultGUI.LabelRightAligned(args.rowRect, "" + sceneItem.counter, args.selected, args.focused);
}
else if (sceneItem.displayName == string.Empty || !sceneExists)
{
TreeView.DefaultGUI.LabelRightAligned(args.rowRect, "Deleted", args.selected, args.focused);
}
}
}
else
base.RowGUI(args);
}
protected override DragAndDropVisualMode HandleDragAndDrop(DragAndDropArgs args)
{
DragAndDropVisualMode visualMode = DragAndDropVisualMode.None;
var draggedIDs = DragAndDrop.GetGenericData("BuildPlayerSceneTreeViewItem") as List<int>;
if (draggedIDs != null && draggedIDs.Count > 0)
{
visualMode = DragAndDropVisualMode.Move;
if (args.performDrop)
{
int newIndex = FindDropAtIndex(args);
var result = new List<TreeViewItem>();
int toInsert = 0;
foreach (var item in rootItem.children)
{
if (toInsert == newIndex)
{
foreach (var id in draggedIDs)
{
result.Add(FindItem(id, rootItem));
}
}
toInsert++;
if (!draggedIDs.Contains(item.id))
{
result.Add(item);
}
}
if (result.Count < rootItem.children.Count) //must be appending.
{
foreach (var id in draggedIDs)
{
result.Add(FindItem(id, rootItem));
}
}
rootItem.children = result;
EditorBuildSettings.scenes = GetSceneList();
ReloadAndSelect(draggedIDs);
Repaint();
}
}
else if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
{
visualMode = DragAndDropVisualMode.Copy;
if (args.performDrop)
{
var scenes = new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes);
var scenesToAdd = new List<EditorBuildSettingsScene>();
var selection = new List<int>();
foreach (var path in DragAndDrop.paths)
{
if (AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(SceneAsset))
{
var guid = new GUID(AssetDatabase.AssetPathToGUID(path));
selection.Add(guid.GetHashCode());
bool unique = true;
foreach (var scene in scenes)
{
if (scene.path == path)
{
unique = false;
break;
}
}
if (unique)
scenesToAdd.Add(new EditorBuildSettingsScene(path, true));
}
}
int newIndex = FindDropAtIndex(args);
scenes.InsertRange(newIndex, scenesToAdd);
EditorBuildSettings.scenes = scenes.ToArray();
ReloadAndSelect(selection);
Repaint();
}
}
return visualMode;
}
private void ReloadAndSelect(IList<int> hashCodes)
{
Reload();
SetSelection(hashCodes, TreeViewSelectionOptions.RevealAndFrame);
SelectionChanged(hashCodes);
}
protected override void DoubleClickedItem(int id)
{
BuildPlayerSceneTreeViewItem item = FindItem(id , rootItem) as BuildPlayerSceneTreeViewItem;
int instanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(item.fullName);
EditorGUIUtility.PingObject(instanceID);
}
protected int FindDropAtIndex(DragAndDropArgs args)
{
int indexToDrop = args.insertAtIndex;
// covers if(args.dragAndDropPosition == DragAndDropPosition.OutsideItems) and a safety check.
if (indexToDrop < 0 || indexToDrop > rootItem.children.Count)
indexToDrop = rootItem.children.Count;
return indexToDrop;
}
protected override bool CanStartDrag(CanStartDragArgs args)
{
return true;
}
protected override void SetupDragAndDrop(SetupDragAndDropArgs args)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.paths = null;
DragAndDrop.objectReferences = new UnityEngine.Object[] {};
DragAndDrop.SetGenericData("BuildPlayerSceneTreeViewItem", new List<int>(args.draggedItemIDs));
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
DragAndDrop.StartDrag("BuildPlayerSceneTreeView");
}
protected override void KeyEvent()
{
if ((Event.current.keyCode == KeyCode.Delete || Event.current.keyCode == KeyCode.Backspace) &&
(GetSelection().Count > 0))
{
RemoveSelection();
}
}
protected override void ContextClicked()
{
if (GetSelection().Count > 0)
{
GenericMenu menu = new GenericMenu();
menu.AddItem(EditorGUIUtility.TrTextContent("Remove Selection"), false, RemoveSelection);
menu.ShowAsContext();
}
}
protected void RemoveSelection()
{
foreach (var nodeID in GetSelection())
{
rootItem.children.Remove(FindItem(nodeID, rootItem));
}
EditorBuildSettings.scenes = GetSceneList();
Reload();
Repaint();
}
public EditorBuildSettingsScene[] GetSceneList()
{
var sceneList = new EditorBuildSettingsScene[rootItem.children.Count];
for (int index = 0; index < rootItem.children.Count; index++)
{
var sceneItem = rootItem.children[index] as BuildPlayerSceneTreeViewItem;
sceneList[index] = new EditorBuildSettingsScene(sceneItem.fullName, sceneItem.active);
}
return sceneList;
}
}
}