playMaker

Author Topic: EZGui Button Invoke  (Read 9109 times)

sP0CkEr

  • Playmaker Newbie
  • *
  • Posts: 25
EZGui Button Invoke
« on: April 11, 2011, 10:23:33 PM »
This is an Action I have found to be useful on my current project. It is a helper to use an EZGui UIButton and captures events when the button has been triggered.

To use it, add the Action to the Start Event of your FSM, and assign the event you would like it to trigger when the button has been invoked. Pretty simple!

Please let me know what you think and how it can be improved.

Code: [Select]

using UnityEngine;
using HutongGames.PlayMaker;

/// <summary>
/// PlayMaker Action for Delegating when a UIButton
/// has been invoked.
/// </summary>
/// <para>
/// This Action should be placed in the Start action of your
/// FSM. It will add itself as a delegate to the EZGui Button
/// to capture events that have been triggered by the
/// button.
/// </para>
/// <remarks>The Owner must contain a UIButton class</remarks>
/// <remarks>See the EZGui Documentation for More information on this Delegate</remarks>

[ActionCategory("EzGui")]
[Tooltip("EzGui Button Click")]

public class EzButtonInvoke : FsmStateAction {

[CheckForComponent(typeof(UIButton))]
public FsmOwnerDefault gameObject;
public FsmEvent invokeEvent;

private GameObject go;
private UIButton _object;

public override void Reset() {
gameObject = null;
invokeEvent = null;
}

public override void OnEnter() {

// get the UIButton attached to this object

go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;
_object = go.GetComponentInChildren<UIButton>();

// set the delegate to capture change event

_object.AddValueChangedDelegate(MyDelegate);

Finish();
}

public override string ErrorCheck() {

// TODO: Missing UIButton Object

return "";
}

void MyDelegate ( IUIObject obj ) {
Fsm.Event(invokeEvent);

}
}


Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: EZGui Button Invoke
« Reply #1 on: April 12, 2011, 03:30:07 AM »
Very clever! Was thinking about ways to integrate EZGui, but this seems like a cool solution. Might be worth extending so it can (optionally) send an event to an FSM on another game object, so you could have one FSM handle events from multiple buttons... but haven't played with EZGui enough yet to know if this makes sense...

sP0CkEr

  • Playmaker Newbie
  • *
  • Posts: 25
Re: EZGui Button Invoke
« Reply #2 on: April 12, 2011, 12:35:11 PM »
great ideas. i was also considering being able to change some of the attributes of the button itself from the action so that you dont need to set it up on the unity interface. thanks for the feedback!

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: EZGui Button Invoke
« Reply #3 on: April 12, 2011, 07:31:21 PM »
great ideas. i was also considering being able to change some of the attributes of the button itself from the action so that you dont need to set it up on the unity interface. thanks for the feedback!

That would be awesome. I hope one day someone does some custom actions for Sprite Manager 2. I'll try to learn how to do it myself and maybe I'll get there before someone else does.

kinetiknz

  • Junior Playmaker
  • **
  • Posts: 85
Re: EZGui Button Invoke
« Reply #4 on: March 13, 2012, 04:19:22 PM »
I just made a simple test scene which has 2 ezgui buttons, and a sphere. Each button has the EZ button invoke action linking to another state that disables/enables the sphere.

It works for the first click on each button, then stops working? am I using this action incorrectly?

Also, specifying another game object doesn't work as you can't select any events outside of the current FSM, global events aren't visible to it.

I get this error in the above setup:
NullReferenceException: Object reference not set to an instance of an object
EzButtonInvoke.OnEnter () (at Assets/PlayMaker/Actions/EZGUI.cs:44)
HutongGames.PlayMaker.FsmState.OnEnter ()
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state)
HutongGames.PlayMaker.Fsm.Update ()
PlayMakerFSM.Update ()

Anyone had a similar issue?
« Last Edit: March 13, 2012, 05:49:36 PM by kinetiknz »