Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

EventDelegate in inspector (for ButtonPressed script)

Discussion in 'UGUI & TextMesh Pro' started by elnico, Aug 21, 2015.

  1. elnico

    elnico

    Joined:
    Aug 2, 2014
    Posts:
    7
    Hello,

    Does somebody know which namespace is needed to use EventDelegate ?

    I write a C# script ButtonPressed.cs to attach to a GameObject contening a Button in order to trigger function when the button is pressed (not clicked but pressed).

    A simplified version of the script is attached.

    I connect OnPointerExit, OnPointerDown, OnPointerUp to ButtonPressed.redirectExit ... Down and Up using an EventTrigger.

    It works and calls ButtonPressed.CallPressed() when the button is pressed.

    void CallPressed()
    {
    Debug.Log( "PRESSED" );
    }

    But I want to add in my ButtonPressed an attribute to call another method from another script inside CallPressed() and I need to be able to change the method in the inspector.

    So my idea is to add an EventDelegate in my script.

    But when I add the EvenDelegate attribute:

    public class ButtonPressed : MonoBehaviour
    {
    public EventDelegate myCallback;

    ...
    }

    I have a "EventDelegate not found" error message but I have no idea which namespace I should use ?

    Any idea or comment about my approach is very welcome.

    If there are other or better ways, I appreciate to know them. But I am very interested to be able to use an EventDelegate in the inspector with a custom script for other purposes as well. How to do it please ?

    Thanks,
    Nicolas.
     

    Attached Files:

  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Kiwasi likes this.
  3. elnico

    elnico

    Joined:
    Aug 2, 2014
    Posts:
    7
    Thank you so much rakkarage, it works :)

    And thank you for the very interesting link.

    For those interested, EventDelegate was a mistake. In fact I should have looked for UnityEvent.

    I should have written :

    using UnityEngine.Events;

    public class ButtonPressed : MonoBehaviour
    {
    public UnityEvent myCallback;

    ...

    void CallPressed()
    {
    myCallback.Invoke();
    }
    }

    I will read the reast of linked thread to hopefully see if I can generalize it to add an arbitrary parameter (int, string, ...) like it is done in a button.
     
  4. elnico

    elnico

    Joined:
    Aug 2, 2014
    Posts:
    7
    For those like me who did not know about UnityEvent :

    To link to a method with a parameter, simply connect to a method with a parameter, the inspector will update accordingly and let you insert you parameter value.