Skip to content

Commit e48f3e1

Browse files
committed
Custom inspector for EventListener implemented
1 parent ec517bf commit e48f3e1

7 files changed

Lines changed: 102 additions & 21 deletions

Editor/EspidiGames.SOEventSystem.Editor.asmdef

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"name": "EspidiGames.SOEventSystem.Editor",
33
"rootNamespace": "",
44
"references": [
5-
"GUID:478a2357cc57436488a56e564b08d223"
5+
"GUID:478a2357cc57436488a56e564b08d223",
6+
"GUID:2c34241bc039bf74eab5e91e0d89b976"
7+
],
8+
"includePlatforms": [
9+
"Editor"
610
],
7-
"includePlatforms": [],
811
"excludePlatforms": [],
912
"allowUnsafeCode": false,
1013
"overrideReferences": false,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using UnityEditor;
2+
using SOBaseEvents.Refactor;
3+
using System;
4+
using UnityEngine;
5+
6+
namespace EG.ScriptableObjectSystem.Editor
7+
{
8+
[CustomEditor(typeof(EventListener<,>), true)]
9+
[CanEditMultipleObjects]
10+
public class EventListenerCustomInspector: UnityEditor.Editor
11+
{
12+
private const string EventSOVariableName = "_baseEvent";
13+
14+
SerializedProperty _baseEventProp;
15+
Type _tArg;
16+
17+
private void OnEnable()
18+
{
19+
_baseEventProp = serializedObject.FindProperty(EventSOVariableName);
20+
var listenerType = target.GetType();
21+
22+
var baseType = listenerType;
23+
while (baseType != null
24+
&& (!baseType.IsGenericType
25+
|| baseType.GetGenericTypeDefinition() != typeof(EventListener<,>)))
26+
{
27+
baseType = baseType.BaseType;
28+
}
29+
30+
if (baseType != null)
31+
{
32+
_tArg = baseType.GetGenericArguments()[1];
33+
}
34+
}
35+
36+
public override void OnInspectorGUI()
37+
{
38+
serializedObject.Update();
39+
40+
EditorGUILayout.Space();
41+
EditorGUILayout.LabelField("Configuración del Evento", EditorStyles.boldLabel);
42+
43+
EditorGUI.BeginChangeCheck();
44+
45+
var newObj = EditorGUILayout.ObjectField(
46+
new GUIContent("Event Asset", $"Must implement ISOEventRegistry<{_tArg.Name}>"),
47+
_baseEventProp.objectReferenceValue,
48+
typeof(ScriptableObject),
49+
false
50+
);
51+
52+
if (EditorGUI.EndChangeCheck())
53+
{
54+
if (newObj == null)
55+
{
56+
_baseEventProp.objectReferenceValue = null;
57+
}
58+
else
59+
{
60+
var implementsBase = newObj is ISOEventBase;
61+
62+
var registryInterface = typeof(ISOEventRegistry<>).MakeGenericType(_tArg);
63+
var implementsRegistry = registryInterface.IsAssignableFrom(newObj.GetType());
64+
65+
if (implementsBase && implementsRegistry)
66+
{
67+
_baseEventProp.objectReferenceValue = newObj;
68+
}
69+
else
70+
{
71+
var errorMsg = $"Asset '{newObj.name}' is not valid.\n\n";
72+
if (!implementsBase)
73+
{
74+
errorMsg += "- Does not implement ISOEventBase.\n";
75+
}
76+
77+
if (!implementsRegistry)
78+
{
79+
errorMsg += $"- Does not implement ISOEventRegistry<{_tArg.Name}>.\n";
80+
}
81+
82+
EditorUtility.DisplayDialog("Compatibility Error", errorMsg, "Ok");
83+
}
84+
}
85+
}
86+
87+
serializedObject.ApplyModifiedProperties();
88+
}
89+
}
90+
}

Editor/EventListenerCustomInspector.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/SOBaseEvents/Refactor/EventListener.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
2-
using SOBaseEvents.Refactor.Impl;
32
using UnityEngine;
43

54
namespace SOBaseEvents.Refactor
65
{
7-
public abstract class EventListener<TEvent, TArg> : EventListenerBase where TEvent : EventSOBase<TArg>
6+
public abstract class EventListener<TEvent, TArg> : MonoBehaviour where TEvent : class, ISOEventRegistry<TArg>
87
{
98
public event Action<TArg> OnEventRaised;
109

10+
[SerializeField] protected ScriptableObject _baseEvent;
11+
1112
protected TEvent _typedEvent => _baseEvent as TEvent;
1213

1314
private void OnEnable()

Runtime/SOBaseEvents/Refactor/Impl/BoolEventListener.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using UnityEngine;
2-
3-
namespace SOBaseEvents.Refactor.Impl
1+
namespace SOBaseEvents.Refactor.Impl
42
{
53
public class BoolEventListener : EventListener<BoolEventSO, bool>
64
{

Runtime/SOBaseEvents/Refactor/Impl/EventListenerBase.cs

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

Runtime/SOBaseEvents/Refactor/Impl/EventListenerBase.cs.meta

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

0 commit comments

Comments
 (0)