I have this generic class which defines a static event Click
using UnityEngine;
using System;
using UnityEngine.EventSystems;
public class ClickButtonGeneric<T>: MonoBehaviour,IPointerClickHandler where T : class
{
public static event Action Click;
public void OnPointerClick(PointerEventData eventData)
{
ClickButton();
}
protected virtual void ClickButton()
{
Click.Invoke();
}
}
This allows me to create various classes that inherit the generic class and have their own static event Click:
public class ClickButtonOptions : ClickButtonGeneric<ClickButtonOptions>{}
public class ClickButtonLogin : ClickButtonGeneric<ClickButtonLogin>{}
I, then can add methods as listeners of the event, like the following example:
using UnityEngine;
public class Options :MonoBehaviour
{
private void Start()
{
ClickButtonOptions.Click += Toggle;
}
private void Toggle()
{
EventsManager.OnTogglePanel(GetComponent<CanvasGroup>());
}
private void OnDisable()
{
ClickButtonOptions.Click -= Toggle;
}
}
I want to make a generic version of the above code where I will be able to define to which static event the Toggle method will be added. Something like the following:
using UnityEngine;
public class ToggleablePanel<T> :MonoBehaviour
{
private void Start()
{
T.Click += Toggle;
}
private void Toggle()
{
EventsManager.OnTogglePanel(GetComponent<CanvasGroup>());
}
private void OnDisable()
{
T.Click -= Toggle;
}
}
Is something like that possible?
Would another approach be better?
I’m not sure what the generic is even adding here, aside from letting you have multiple static events across multiple types. I don’t believe you can do what you’re trying to do as well. You will still need to use a concrete type to access the static Click delegate.
A non-generic, but reusable component that you reference via the inspector would get the same effect without the need for so many different implementations.
I could implement reusable components, but my approach uses csharp events and minimal to none inspector references.
How would the non-generic approach look like? Could I select from inspector the static event I want to use? Or you mean to totally abandon the generic classes approach?
Following spiney199’s suggestions I ended up with the following:
I created a button handler script that I attach to every button I want to invoke click events
using System;
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickButtonHandler : MonoBehaviour,IPointerClickHandler
{
public event Action Click;
public void OnPointerClick(PointerEventData eventData)
{
InvokeEvent();
}
protected virtual void InvokeEvent()
{
Click.Invoke();
}
}
I reference all ClickButtonHandler instances in a singleton EventsManager:
using UnityEngine;
using System;
public class EventsManager : Singleton<EventsManager>
{
#region Button Handlers
public ClickButtonHandler buttonOptionsHandler;
public ClickButtonHandler buttonLoginHandler;
public ClickButtonHandler buttonSignupHandler;
public ClickButtonHandler buttonShowSignupScreenHandler;
#endregion
protected override void OnEnable()
{
if (Instance == null)
{
}
base.OnEnable();
}
}
Then I created a base class for the toggleable panels: