Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit c4b7ee1

Browse files
Fixed event debug buttons not supporting non-unity objects
1 parent 274bafa commit c4b7ee1

File tree

7 files changed

+106
-261
lines changed

7 files changed

+106
-261
lines changed

Assets/SO Architecture/Editor/Inspectors/TypedGameEventEditor.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
using System.Reflection;
22
using UnityEditor;
33
using UnityEngine;
4+
using Type = System.Type;
45

56
[CustomEditor(typeof(GameEventBase<>), true)]
67
public class TypedGameEventEditor : BaseGameEventEditor
78
{
8-
private System.Type GenericType { get { return target.GetType().BaseType.GetGenericArguments()[0]; } }
9+
private Type GenericType { get { return target.GetType().BaseType.GetGenericArguments()[0]; } }
910

1011
private MethodInfo _raiseMethod;
11-
private TypedRaiseButton _raiseButton;
1212

1313
protected override void OnEnable()
1414
{
1515
base.OnEnable();
1616

1717
_raiseMethod = target.GetType().BaseType.GetMethod("Raise");
18-
_raiseButton = new TypedRaiseButton(GenericType, CallMethod);
1918
}
2019
protected override void DrawRaiseButton()
2120
{
22-
_raiseButton.Draw();
21+
SerializedProperty property = serializedObject.FindProperty("_debugValue");
22+
23+
EditorGUILayout.PropertyField(property);
24+
25+
if(GUILayout.Button("Raise"))
26+
{
27+
CallMethod(GetDebugValue(property));
28+
}
29+
}
30+
private object GetDebugValue(SerializedProperty property)
31+
{
32+
object parent = SerializedPropertyHelper.GetParent(property);
33+
Type parentType = parent.GetType();
34+
35+
FieldInfo targetField = parentType.GetField("_debugValue", BindingFlags.Instance | BindingFlags.NonPublic);
36+
37+
return targetField.GetValue(parent);
2338
}
2439
private void CallMethod(object value)
2540
{

Assets/SO Architecture/Editor/Inspectors/TypedRaiseButton.cs

Lines changed: 0 additions & 252 deletions
This file was deleted.

Assets/SO Architecture/Editor/Inspectors/TypesGameEventListenerEditor.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
using System.Reflection;
22
using UnityEditor;
3+
using UnityEngine;
4+
using Type = System.Type;
35

46
[CustomEditor(typeof(BaseGameEventListener<,,>), true)]
57
public class TypesGameEventListenerEditor : BaseGameEventListenerEditor
68
{
79
private System.Type GenericType { get { return target.GetType().BaseType.GetGenericArguments()[0]; } }
810

911
private MethodInfo _raiseMethod;
10-
private TypedRaiseButton _raiseButton;
1112

1213
protected override void OnEnable()
1314
{
1415
base.OnEnable();
1516

1617
_raiseMethod = target.GetType().BaseType.GetMethod("OnEventRaised");
17-
_raiseButton = new TypedRaiseButton(GenericType, CallMethod);
1818
}
1919
protected override void DrawRaiseButton()
2020
{
21-
_raiseButton.Draw();
21+
SerializedProperty property = serializedObject.FindProperty("_debugValue");
22+
23+
EditorGUILayout.PropertyField(property);
24+
25+
if (GUILayout.Button("Raise"))
26+
{
27+
CallMethod(GetDebugValue(property));
28+
}
29+
}
30+
private object GetDebugValue(SerializedProperty property)
31+
{
32+
object parent = SerializedPropertyHelper.GetParent(property);
33+
Type parentType = parent.GetType();
34+
35+
FieldInfo targetField = parentType.GetField("_debugValue", BindingFlags.Instance | BindingFlags.NonPublic);
36+
37+
return targetField.GetValue(parent);
2238
}
2339
private void CallMethod(object value)
2440
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using UnityEditor;
4+
using System.Linq;
5+
using System;
6+
using System.Reflection;
7+
8+
public static class SerializedPropertyHelper
9+
{
10+
public static object GetParent(SerializedProperty prop)
11+
{
12+
var path = prop.propertyPath.Replace(".Array.data[", "[");
13+
object obj = prop.serializedObject.targetObject;
14+
var elements = path.Split('.');
15+
foreach (var element in elements.Take(elements.Length - 1))
16+
{
17+
if (element.Contains("["))
18+
{
19+
var elementName = element.Substring(0, element.IndexOf("["));
20+
var index = Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
21+
obj = GetValue(obj, elementName, index);
22+
}
23+
else
24+
{
25+
obj = GetValue(obj, element);
26+
}
27+
}
28+
return obj;
29+
}
30+
31+
public static object GetValue(object source, string name)
32+
{
33+
if (source == null)
34+
return null;
35+
var type = source.GetType();
36+
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
37+
if (f == null)
38+
{
39+
var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
40+
if (p == null)
41+
return null;
42+
return p.GetValue(source, null);
43+
}
44+
return f.GetValue(source);
45+
}
46+
47+
public static object GetValue(object source, string name, int index)
48+
{
49+
var enumerable = GetValue(source, name) as IEnumerable;
50+
var enm = enumerable.GetEnumerator();
51+
while (index-- >= 0)
52+
enm.MoveNext();
53+
return enm.Current;
54+
}
55+
}

0 commit comments

Comments
 (0)