Skip to content

Commit 9d646b7

Browse files
committed
Applied pull request suggestions
1 parent 5cb4514 commit 9d646b7

2 files changed

Lines changed: 97 additions & 97 deletions

File tree

Assets/Editor Toolbox/Editor/SceneView/ToolboxEditorSceneViewObjectSelector.cs

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,21 @@ public class ToolboxEditorSceneViewObjectSelector : EditorWindow
1919
private Vector2 size;
2020
private Vector2 buttonSize;
2121
private GUIStyle buttonStyle;
22-
private Color selectionColor = new Color(0.50f, 0.70f, 1.00f);
22+
private static readonly Color selectionColor = new Color(0.50f, 0.70f, 1.00f);
2323

24-
private int shiftMinSelectionID = -1;
25-
private int shiftMaxSelectionID = -1;
26-
private int shiftLastID = -1;
27-
28-
private GameObject HighlightedObject
29-
{
30-
set
31-
{
32-
if (highlightedObject == value)
33-
{
34-
return;
35-
}
36-
37-
highlightedObject = value;
38-
UnityEditor.SceneView.RepaintAll();
39-
40-
if (highlightedObject != null)
41-
{
42-
EditorGUIUtility.PingObject(highlightedObject);
43-
}
44-
}
45-
}
24+
private int shiftMinSelectionId = -1;
25+
private int shiftMaxSelectionId = -1;
26+
private int shiftLastId = -1;
4627

4728
public static void Show(List<GameObject> gameObjects, Vector2 position)
4829
{
49-
Rect rect = new Rect(position, Vector2.one);
30+
var rect = new Rect(position, Vector2.one);
5031

51-
ToolboxEditorSceneViewObjectSelector window = CreateInstance<ToolboxEditorSceneViewObjectSelector>();
32+
var window = CreateInstance<ToolboxEditorSceneViewObjectSelector>();
5233
window.wantsMouseMove = true;
5334
window.wantsMouseEnterLeaveWindow = true;
5435
window.gameObjects = gameObjects;
55-
window.InitializeGameobjectPaths();
36+
window.InitializeGameObjectPaths();
5637
window.CalculateSize();
5738
window.ShowAsDropDown(rect, window.size);
5839
}
@@ -72,7 +53,7 @@ private Vector2 CalculateSize()
7253

7354
size = Vector2.zero;
7455

75-
foreach (GameObject go in gameObjects)
56+
foreach (var go in gameObjects)
7657
{
7758
GUIContent content = EditorGUIUtility.ObjectContent(go, typeof(GameObject));
7859
Vector2 currentSize = buttonStyle.CalcSize(content);
@@ -94,21 +75,21 @@ private Vector2 CalculateSize()
9475
return size;
9576
}
9677

97-
private void InitializeGameobjectPaths()
78+
private void InitializeGameObjectPaths()
9879
{
9980
gameObjectPaths = new List<string>();
100-
Stack<string> pathStack = new Stack<string>();
81+
var pathStack = new Stack<string>();
10182

10283
for (int i = 0; i < gameObjects.Count; i++)
10384
{
10485
pathStack.Clear();
105-
Transform transf = gameObjects[i].transform;
106-
pathStack.Push(transf.gameObject.name);
86+
Transform transform = gameObjects[i].transform;
87+
pathStack.Push(transform.gameObject.name);
10788

108-
while (transf.parent != null)
89+
while (transform.parent != null)
10990
{
110-
transf = transf.parent;
111-
pathStack.Push(transf.gameObject.name);
91+
transform = transform.parent;
92+
pathStack.Push(transform.gameObject.name);
11293
}
11394

11495
string path = string.Join("/", pathStack.ToArray());
@@ -128,37 +109,41 @@ private void OnGUI()
128109
return;
129110
}
130111

131-
//Debug.Log($"Event : {Event.current.type}");
132-
133-
if (Event.current.type == EventType.MouseMove)
112+
switch (Event.current.type)
134113
{
135-
OnGUI_MouseMove();
136-
}
137-
else if (Event.current.type == EventType.MouseLeaveWindow)
138-
{
139-
OnGUI_MouseLeave();
140-
}
141-
else
142-
{
143-
OnGUI_Normal();
114+
case EventType.MouseMove:
115+
{
116+
OnGUIMouseMove();
117+
break;
118+
}
119+
case EventType.MouseLeaveWindow:
120+
{
121+
OnGUIMouseLeave();
122+
break;
123+
}
124+
default:
125+
{
126+
OnGUINormal();
127+
break;
128+
}
144129
}
145130
}
146131

147-
private void OnGUI_Normal()
132+
private void OnGUINormal()
148133
{
149134
Rect rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
150135

151136
for (int i = 0; i < gameObjects.Count; i++)
152137
{
153-
GameObject gameObject = gameObjects[i];
138+
var gameObject = gameObjects[i];
154139

155140
if(gameObject == null)
156141
{
157142
//Can happen when something removes the gameobject during the window display.
158143
continue;
159144
}
160145

161-
GUIContent content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
146+
var content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
162147

163148
bool objectSelected = Selection.Contains(gameObject);
164149

@@ -178,21 +163,21 @@ private void OnGUI_Normal()
178163
}
179164
}
180165

181-
private void OnGUI_MouseMove()
166+
private void OnGUIMouseMove()
182167
{
183-
Rect rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
168+
var rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
184169

185170
for (int i = 0; i < gameObjects.Count; i++)
186171
{
187-
GameObject gameObject = gameObjects[i];
172+
var gameObject = gameObjects[i];
188173

189174
if (gameObject == null)
190175
{
191176
//Can happen when something removes the gameobject during the window display.
192177
continue;
193178
}
194179

195-
GUIContent content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
180+
var content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
196181

197182
GUI.Button(rect, content, buttonStyle);
198183

@@ -219,53 +204,53 @@ private void GameObjectButtonPress(int id)
219204

220205
private void UpdateShiftSelectionIDs(int id)
221206
{
222-
if(shiftLastID == -1)
207+
if(shiftLastId == -1)
223208
{
224-
shiftLastID = id;
209+
shiftLastId = id;
225210
}
226211

227-
if(shiftMinSelectionID == -1)
212+
if(shiftMinSelectionId == -1)
228213
{
229-
shiftMinSelectionID = id;
214+
shiftMinSelectionId = id;
230215
}
231216

232-
if (shiftMaxSelectionID == -1)
217+
if (shiftMaxSelectionId == -1)
233218
{
234-
shiftMaxSelectionID = id;
219+
shiftMaxSelectionId = id;
235220
}
236221

237-
if(id < shiftMinSelectionID)
222+
if(id < shiftMinSelectionId)
238223
{
239-
shiftMinSelectionID = id;
224+
shiftMinSelectionId = id;
240225
}
241-
else if(id >= shiftMaxSelectionID)
226+
else if(id >= shiftMaxSelectionId)
242227
{
243-
shiftMaxSelectionID = id;
228+
shiftMaxSelectionId = id;
244229
}
245-
else if(id > shiftMinSelectionID)
230+
else if(id > shiftMinSelectionId)
246231
{
247232
//ID is between min and max.
248-
if(shiftLastID < id)
233+
if(shiftLastId < id)
249234
{
250-
shiftMaxSelectionID = id;
235+
shiftMaxSelectionId = id;
251236
}
252237
else
253238
{
254-
shiftMinSelectionID = id;
239+
shiftMinSelectionId = id;
255240
}
256241
}
257242

258-
shiftLastID = id;
243+
shiftLastId = id;
259244
}
260245

261246
private void SelectObject(int id, bool control, bool shift)
262247
{
263-
GameObject gameObject = gameObjects[id];
248+
var gameObject = gameObjects[id];
264249

265250
if (shift)
266251
{
267252
UpdateShiftSelectionIDs(id);
268-
SelectObjects(shiftMinSelectionID, shiftMaxSelectionID);
253+
SelectObjects(shiftMinSelectionId, shiftMaxSelectionId);
269254
}
270255
else if (control)
271256
{
@@ -290,8 +275,8 @@ private void SelectObject(int id, bool control, bool shift)
290275

291276
private void SelectObjects(int minID, int maxID)
292277
{
293-
int size = maxID - minID + 1;
294-
Object[] newSelection = new Object[size];
278+
var size = maxID - minID + 1;
279+
var newSelection = new Object[size];
295280

296281
int index = 0;
297282

@@ -306,8 +291,8 @@ private void SelectObjects(int minID, int maxID)
306291

307292
private void AddObjectToSelection(GameObject gameObject)
308293
{
309-
Object[] currentSelection = Selection.objects;
310-
Object[] newSelection = new Object[currentSelection.Length + 1];
294+
var currentSelection = Selection.objects;
295+
var newSelection = new Object[currentSelection.Length + 1];
311296

312297
currentSelection.CopyTo(newSelection, 0);
313298
newSelection[newSelection.Length - 1] = gameObject;
@@ -317,10 +302,10 @@ private void AddObjectToSelection(GameObject gameObject)
317302

318303
private void RemoveObjectFromSelection(GameObject gameObject)
319304
{
320-
Object[] currentSelection = Selection.objects;
321-
Object[] newSelection = new Object[currentSelection.Length - 1];
305+
var currentSelection = Selection.objects;
306+
var newSelection = new Object[currentSelection.Length - 1];
322307

323-
int index = 0;
308+
var index = 0;
324309

325310
for (int i = 0; i < currentSelection.Length; i++)
326311
{
@@ -336,9 +321,29 @@ private void RemoveObjectFromSelection(GameObject gameObject)
336321
Selection.objects = newSelection;
337322
}
338323

339-
private void OnGUI_MouseLeave()
324+
private void OnGUIMouseLeave()
340325
{
341326
HighlightedObject = null;
342327
}
328+
329+
private GameObject HighlightedObject
330+
{
331+
set
332+
{
333+
if (highlightedObject == value)
334+
{
335+
return;
336+
}
337+
338+
highlightedObject = value;
339+
UnityEditor.SceneView.RepaintAll();
340+
341+
if (highlightedObject != null)
342+
{
343+
EditorGUIUtility.PingObject(highlightedObject);
344+
}
345+
}
346+
}
347+
343348
}
344349
}

Assets/Editor Toolbox/Editor/ToolboxEditorSceneView.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,26 @@ private static List<GameObject> GetObjectsUnderCursor()
3838

3939
private static void UpdateEventCallback()
4040
{
41-
if (UseToolboxSceneView && !IsEventAttached)
42-
{
43-
UnityEditor.SceneView.duringSceneGui += SceneView_DuringSceneGUI;
44-
IsEventAttached = true;
45-
}
46-
else if (!UseToolboxSceneView && IsEventAttached)
41+
UnityEditor.SceneView.duringSceneGui -= SceneViewDuringSceneGUI;
42+
43+
if (UseToolboxSceneView)
4744
{
48-
UnityEditor.SceneView.duringSceneGui -= SceneView_DuringSceneGUI;
49-
IsEventAttached = false;
45+
UnityEditor.SceneView.duringSceneGui += SceneViewDuringSceneGUI;
5046
}
5147
}
5248

53-
private static void SceneView_DuringSceneGUI(UnityEditor.SceneView sceneView)
49+
private static void SceneViewDuringSceneGUI(UnityEditor.SceneView sceneView)
5450
{
55-
if (Event.current.type == EventType.KeyDown
56-
&& Event.current.keyCode == SelectorKey)
51+
if (Event.current.type != EventType.KeyDown
52+
|| Event.current.keyCode != SelectorKey)
5753
{
58-
List<GameObject> objectsUnderCursor = GetObjectsUnderCursor();
59-
if (objectsUnderCursor.Count > 0)
60-
{
61-
ToolboxEditorSceneViewObjectSelector.Show(objectsUnderCursor, Event.current.mousePosition + sceneView.position.position);
62-
}
54+
return;
55+
}
56+
57+
List<GameObject> objectsUnderCursor = GetObjectsUnderCursor();
58+
if (objectsUnderCursor.Count > 0)
59+
{
60+
ToolboxEditorSceneViewObjectSelector.Show(objectsUnderCursor, Event.current.mousePosition + sceneView.position.position);
6361
}
6462
}
6563

@@ -79,8 +77,6 @@ internal static void UpdateSettings(IToolboxSceneViewSettings settings)
7977
UpdateEventCallback();
8078
}
8179

82-
private static bool IsEventAttached { get; set; } = false;
83-
8480
/// <summary>
8581
/// Should the scene view be used.
8682
/// </summary>
@@ -90,6 +86,5 @@ internal static void UpdateSettings(IToolboxSceneViewSettings settings)
9086
/// Which key should activate the scene view.
9187
/// </summary>
9288
private static KeyCode SelectorKey { get; set; } = KeyCode.LeftControl;
93-
9489
}
9590
}

0 commit comments

Comments
 (0)