playMaker

Author Topic: Send FSM events easily from UGUI button  (Read 2873 times)

naby

  • Playmaker Newbie
  • *
  • Posts: 4
    • Twitter
Send FSM events easily from UGUI button
« on: October 13, 2019, 03:28:21 PM »
This is not an action for FSM, it is a MonoBehaviour component used with UGUI button components

When calling an FSM event from outside the FSM graph, you can send a system event or user custom event from the Button without specifying a string



Code: [Select]

using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using HutongGames.PlayMaker;
using System.Collections.Generic;

[RequireComponent(typeof(Button))]
public class ButtonFsmEvents : MonoBehaviour
{
    public FsmEvent onClick = FsmEvent.Finished;

    private void Awake()
    {
        GetComponent<Button>().onClick.AddListener(() =>
        {
            PlayMakerFSM.BroadcastEvent(onClick);
        });
    }
}


#if UNITY_EDITOR

[CanEditMultipleObjects, CustomEditor(typeof(ButtonFsmEvents))]
public class ButtonFsmEventsEditor : Editor
{
    ButtonFsmEvents _buttonFsmEvents;

    void OnEnable()
    {
        _buttonFsmEvents = target as ButtonFsmEvents;
    }

    public override void OnInspectorGUI()
    {
        using (new EditorGUILayout.HorizontalScope())
        {
            EditorGUILayout.PrefixLabel("OnClick");
            if (GUILayout.Button(_buttonFsmEvents.onClick.Name))
            {
                CreateContextMenu();
            }
        }

    }

    void CreateContextMenu()
    {
        GenericMenu menu = new GenericMenu();

        var systemEvents = new List<FsmEvent>();
        var customEvents = new List<FsmEvent>();

        foreach (var item in FsmEvent.EventList)
        {
            if (item.IsSystemEvent)
            {
                // Ignore Events
                if (item.IsCollision2DEvent || item.IsTrigger2DEvent || item.IsLegacyNetworkEvent)
                {
                    continue;
                }

                systemEvents.Add(item);
            }
            else
            {
                customEvents.Add(item);
            }
        }

        foreach (var item in customEvents)
        {
            menu.AddItem(new GUIContent("Custom/" + item.Name), false, Callback, item);
        }

        menu.AddSeparator("");

        foreach (var item in systemEvents)
        {
            menu.AddItem(new GUIContent("System/" + item.Name), false, Callback, item);
        }

        menu.ShowAsContext();
    }


    void Callback(object fsmEvent)
    {
        _buttonFsmEvents.onClick = (FsmEvent)fsmEvent;
    }
}


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: Send FSM events easily from UGUI button
« Reply #1 on: October 14, 2019, 02:08:51 AM »
Hi,

 cool, thanks for contributing!!

 Have you checked the event proxy generator : https://hutonggames.com/playmakerforum/index.php?topic=11172.msg52732#msg52732

It does the same pretty much, but gives you the flexibility to bind it to any Unity event from any component.

 Bye,

 Jean