Author Topic: Fun with EventDelegates  (Read 3236 times)

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Fun with EventDelegates
« on: August 30, 2015, 09:16:28 PM »
I just want to share some of the fun that can be had with the extremely powerful EventDelegate system in NGUI and propose a few tweaks to help it along.

The power of EventDelegate events in NGUI comes from the ease created by simply wiring events in the editor versus having to handle them via code.  I heartily recommend using the various events exposed on the NGUI stock controls. 

I find that I create many simple public void MethodName() {...} methods that can be the target of these events.  Simple additions, like SetUnchecked() and SetChecked() can then be easy to wire up to events.  What would make this more powerful would be a way to put static parameter values in the editor (see the EventDelegate Wireup.png attached image).  PlayMaker does something similar with the ability to add references and static values.

The next level was reached when I started creating my own List<EventDelegate> events from my own classes.  This then opens up a very easy method to add my own events to my game and even extend NGUI's base classes.

EDIT: The below already exists as EventDelegate.Execute(List<EventDelegate>).
My first recommendation is to add to EventDelegate.cs a static Process() function.  I created my own pseudo-extension method for the same and it means not writing the processing code for events ever again.

EventDelegateProcessor.cs

using System.Collections.Generic;

public static class EventDelegateProcessor
{
    public static void Process(List<EventDelegate> eventDelegate)
    {
        List<EventDelegate> mTemp = null;

        if (eventDelegate != null)
        {
            mTemp = eventDelegate;
            eventDelegate = new List<EventDelegate>();

            // Notify the listener delegates
            EventDelegate.Execute(mTemp);

            // Re-add the previous persistent delegates
            for (int i = 0; i < mTemp.Count; ++i)
            {
                EventDelegate ed = mTemp;
                if (ed != null && !ed.oneShot) EventDelegate.Add(eventDelegate, ed, ed.oneShot);
            }
            mTemp = null;
        }
    }
}

As an example that illustrates the above, here are some simple classes of mine that decorate UIToggle with both types of extra functionality: granular void event target methods and custom events.  (Freyja is the dev name for my project.)

FreyjaToggle.cs
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class FreyjaToggle : MonoBehaviour
  5. {
  6.     private UIToggle _uiToggle;
  7.  
  8.     [HideInInspector]
  9.     public List<EventDelegate> onToggleTrue = new List<EventDelegate>();
  10.  
  11.     [HideInInspector]
  12.     public List<EventDelegate> onToggleFalse = new List<EventDelegate>();
  13.  
  14.  
  15.     public void Start()
  16.     {
  17.         _uiToggle = gameObject.GetComponent<UIToggle>();
  18.         EventDelegate.Add(_uiToggle.onChange, OnToggleChanged, false);
  19.     }
  20.  
  21.     private void OnToggleChanged()
  22.     {
  23.         if (_uiToggle.value)
  24.             EventDelegateProcessor.Process(onToggleTrue);
  25.         else
  26.             EventDelegateProcessor.Process(onToggleFalse);
  27.     }
  28.  
  29.     public void SetValue(bool isChecked)
  30.     { _uiToggle.value = isChecked; }
  31.  
  32.     public void SetChecked()
  33.     { SetValue(true); }
  34.  
  35.     public void SetUnchecked()
  36.     { SetValue(false); }
  37.  
  38.     public void ToggleState()
  39.     { _uiToggle.value = !_uiToggle.value; }
  40. }
  41.  

FreyjaToggleEditor.cs: located in an \Editor folder
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomEditor(typeof(FreyjaToggle))]
  5. public class FreyjaToggleEditor : Editor
  6. {
  7.     public override void OnInspectorGUI()
  8.     {
  9.         base.OnInspectorGUI();
  10.  
  11.         FreyjaToggle ft = target as FreyjaToggle;
  12.  
  13.         NGUIEditorTools.DrawEvents("On Toggle True", ft, ft.onToggleTrue);
  14.         NGUIEditorTools.DrawEvents("On Toggle False", ft, ft.onToggleFalse);
  15.     }
  16. }
  17.  

I hope this helps somebody!!   ;D
« Last Edit: September 06, 2015, 06:10:30 PM by Zyxil »

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Re: Fun with EventDelegates
« Reply #1 on: September 05, 2015, 09:52:09 AM »
No love for the EventDelegate?   :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fun with EventDelegates
« Reply #2 on: September 06, 2015, 05:48:08 PM »
I don't understand the reason behind the "Process" function. All it does is calls Execute, so why not just call Execute as-is?

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Re: Fun with EventDelegates
« Reply #3 on: September 06, 2015, 05:56:58 PM »
I have had this written for awhile. I remember Execute(List<EventDelegate>) not being available when I wrote this.  I'm pretty sure it was handled internally in EventDelegate.cs before.  Nice that it's there now, I can clean up my code.

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Re: Fun with EventDelegates
« Reply #4 on: September 09, 2015, 09:31:13 AM »
How about extending EventDelegate to add static parameter values?  I think this would be very powerful.  As it is now, this can be approximated by having SetChecked() and SetUnchecked() methods or exposing simple values as member variables: public bool bTrue = true; public bFalse = false;.  This example is just for bool variables, others for strings or enums would be even more powerful.

Thoughts?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fun with EventDelegates
« Reply #5 on: September 13, 2015, 05:09:37 AM »
Static value parameter support has been one of those "want to add" features on my list for some time, but I never seem to get around to actually doing it...