Skip to content

Commit f3b7ab8

Browse files
author
krzysztof biernat
committed
Refactor GameEventEditor
1 parent ccf29a6 commit f3b7ab8

File tree

1 file changed

+124
-57
lines changed

1 file changed

+124
-57
lines changed

Editor/Events/GameEventEditor.cs

Lines changed: 124 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,95 @@
66

77
namespace GTVariable.Editor
88
{
9+
910
[CustomEditor(typeof(GameEvent))]
1011
public class GameEventEditor : UnityEditor.Editor
1112
{
1213
private List<GameEventListener> gameEventListners = new List<GameEventListener>();
13-
private readonly List<bool> foldout = new List<bool>();
14+
private List<bool> problems = new List<bool>();
1415
private GameEvent gameEvent;
16+
private int currentSelected = -1;
17+
private string currentSelectedName = "";
18+
private Vector2 responseScroll;
19+
private Vector2 subscirbersScroll;
20+
private GUIStyle errorStyle;
1521

1622
private void OnEnable()
1723
{
1824
gameEvent = target as GameEvent;
1925
gameEventListners = EditorApplication.isPlaying ? gameEvent.EventListners : FindGameEventListnerInScene();
26+
CheckForResponseProblems();
27+
errorStyle = new GUIStyle();
28+
errorStyle.padding = new RectOffset(0, 0, 3, 0);
29+
errorStyle.imagePosition = ImagePosition.ImageOnly;
2030
}
2131

2232
public override void OnInspectorGUI()
2333
{
24-
DrawOptions();
34+
DrawOptions();
35+
36+
37+
2538
DrawSubscribers();
39+
EditorGUILayout.Space();
40+
if (currentSelected != -1 && currentSelected < gameEventListners.Count)
41+
{
42+
EditorGUILayout.BeginHorizontal();
43+
EditorGUILayout.LabelField($"{gameEventListners[currentSelected].gameObject.name} - { currentSelectedName}");
44+
if (GUILayout.Button("Select"))
45+
{
46+
Selection.activeObject = gameEventListners[currentSelected];
47+
}
48+
if (GUILayout.Button("Ping", EditorStyles.miniButtonMid))
49+
{
50+
EditorGUIUtility.PingObject(gameEventListners[currentSelected]);
51+
}
52+
EditorGUILayout.EndHorizontal();
53+
EditorGUILayout.LabelField("Response", EditorStyles.boldLabel);
54+
DrawResponse(gameEventListners[currentSelected].Response);
55+
}
56+
57+
}
58+
59+
private void DrawResponse(UnityEngine.Events.UnityEvent response)
60+
{
61+
62+
var maxWidth = GUILayout.MaxWidth((EditorGUIUtility.currentViewWidth) / 3 - 18);
63+
EditorGUILayout.BeginHorizontal();
64+
EditorGUILayout.LabelField("Target", maxWidth);
65+
EditorGUILayout.LabelField("Method name", maxWidth);
66+
EditorGUILayout.EndHorizontal();
67+
EditorGUILayout.Space();
68+
69+
var eventCount = response.GetPersistentEventCount();
70+
71+
responseScroll = EditorGUILayout.BeginScrollView(responseScroll,GUILayout.MaxHeight(200));
72+
for (int i = 0; i < eventCount; i++)
73+
{
74+
EditorGUILayout.BeginHorizontal();
75+
var hasTarget = response.GetPersistentTarget(i) != null;
76+
77+
if (hasTarget)
78+
{
79+
EditorGUILayout.LabelField(response.GetPersistentTarget(i).name, EditorStyles.miniLabel, maxWidth);
80+
EditorGUILayout.LabelField(response.GetPersistentMethodName(i), EditorStyles.miniLabel, maxWidth);
81+
if (GUILayout.Button("Select", EditorStyles.miniButtonMid))
82+
{
83+
Selection.activeObject = response.GetPersistentTarget(i);
84+
}
85+
if (GUILayout.Button("Ping", EditorStyles.miniButtonMid))
86+
{
87+
EditorGUIUtility.PingObject(response.GetPersistentTarget(i));
88+
}
89+
}
90+
else
91+
{
92+
EditorGUI.HelpBox(GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth - 50, EditorGUIUtility.singleLineHeight + 5),"NULL",MessageType.Error);
93+
}
94+
95+
EditorGUILayout.EndHorizontal();
96+
}
97+
EditorGUILayout.EndScrollView();
2698
}
2799

28100
private void DrawOptions()
@@ -48,12 +120,39 @@ private void DrawOptions()
48120
if (GUILayout.Button("Find in scene"))
49121
{
50122
gameEventListners = FindGameEventListnerInScene();
123+
CheckForResponseProblems();
51124
}
52125
}
53126

54127
EditorGUILayout.EndHorizontal();
55128
}
56129

130+
private void CheckForResponseProblems()
131+
{
132+
for (int i = 0; i < gameEventListners.Count; i++)
133+
{
134+
if(i >= problems.Count)
135+
{
136+
problems.Add(CheckGameEventListener(gameEventListners[i]));
137+
}
138+
else
139+
{
140+
problems[i] = CheckGameEventListener(gameEventListners[i]);
141+
}
142+
}
143+
}
144+
145+
private bool CheckGameEventListener(GameEventListener listener)
146+
{
147+
for (int i = 0; i < listener.Response.GetPersistentEventCount(); i++)
148+
{
149+
if (listener.Response.GetPersistentTarget(i) == null)
150+
return true;
151+
}
152+
153+
return false;
154+
}
155+
57156
private List<GameEventListener> FindGameEventListnerInScene()
58157
{
59158
return FindObjectsOfType<GameEventListener>()
@@ -63,69 +162,37 @@ private List<GameEventListener> FindGameEventListnerInScene()
63162

64163
private void DrawSubscribers()
65164
{
66-
GUILayout.Label("Subscirbers:", EditorStyles.boldLabel);
67-
165+
GUILayout.Label("Subscirbers:", EditorStyles.boldLabel);
166+
subscirbersScroll = EditorGUILayout.BeginScrollView(subscirbersScroll);
68167
for (int i = 0; i < gameEventListners.Count; i++)
69168
{
70-
if (foldout.Count <= i)
71-
{
72-
foldout.Add(false);
73-
}
74-
75-
DrawSubscriber(i);
76-
}
77-
78-
//Remove redundace foldout
79-
if (foldout.Count != gameEventListners.Count)
80-
{
81-
foldout.RemoveRange(gameEventListners.Count, foldout.Count - gameEventListners.Count);
169+
if (problems.Count <= i || problems[i] == false)
170+
{
171+
DrawSubscriber(i);
172+
}
173+
else
174+
{
175+
EditorGUILayout.BeginHorizontal();
176+
EditorGUILayout.LabelField(EditorGUIUtility.IconContent("console.erroricon.sml"),errorStyle,GUILayout.MaxHeight(20),GUILayout.MaxWidth(30));
177+
DrawSubscriber(i);
178+
EditorGUILayout.EndHorizontal();
179+
}
82180
}
181+
EditorGUILayout.EndScrollView();
83182
}
84183

85184
private void DrawSubscriber(int id)
86185
{
87-
foldout[id] = EditorGUILayout.BeginFoldoutHeaderGroup(foldout[id], gameEventListners[id].name, null,
88-
(position) =>
89-
{
90-
var menu = new GenericMenu();
91-
menu.AddItem(new GUIContent("Focus"), false, () =>
92-
{
93-
Selection.activeObject = gameEventListners[id];
94-
});
95-
menu.DropDown(position);
96-
});
97-
if (foldout[id])
98-
{
99-
EditorGUILayout.LabelField("Response:");
100-
GUILayout.BeginHorizontal();
101-
EditorGUILayout.LabelField($"Target", EditorStyles.boldLabel, GUILayout.MinWidth(100), GUILayout.MaxWidth(100));
102-
EditorGUILayout.LabelField($"Method", EditorStyles.boldLabel);
103-
GUILayout.EndHorizontal();
104-
for (int i = 0; i < gameEventListners[id].Response.GetPersistentEventCount(); i++)
105-
{
106-
EditorGUILayout.BeginHorizontal();
107-
var eventTarget = gameEventListners[id].Response.GetPersistentTarget(i);
108-
if (eventTarget == null)
109-
{
110-
var bgColor = GUI.color;
111-
GUI.contentColor = Color.red;
112-
EditorGUILayout.LabelField("NULL");
113-
GUI.contentColor = bgColor;
114-
}
115-
else
116-
{
117-
GUILayout.Label($"{eventTarget.name}", GUILayout.MaxWidth(100));
118-
var method = gameEventListners[id].Response.GetPersistentMethodName(i);
119-
GUILayout.Label($"{(method == "" ? "NULL" : method)}");
120-
}
121-
if (GUILayout.Button("Focus target", GUILayout.MaxWidth(50)))
122-
{
123-
Selection.activeObject = gameEventListners[id];
124-
}
125-
EditorGUILayout.EndHorizontal();
126-
}
186+
var name = gameEventListners[id].name;
187+
if (string.IsNullOrEmpty(name)) name = "[No name specify]";
188+
EditorGUILayout.BeginHorizontal();
189+
EditorGUILayout.LabelField(gameEventListners[id].gameObject.name,GUILayout.MaxWidth(120));
190+
if (GUILayout.Button(name))
191+
{
192+
currentSelected = id;
127193
}
128-
EditorGUILayout.EndFoldoutHeaderGroup();
194+
if (id == currentSelected) currentSelectedName = name;
195+
EditorGUILayout.EndHorizontal();
129196
}
130197
}
131198
}

0 commit comments

Comments
 (0)