Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I make a button call everything suscribed to the "onEndEdit" event of an InputField?

Discussion in 'Scripting' started by gillemp, Aug 17, 2019.

  1. gillemp

    gillemp

    Joined:
    Nov 23, 2015
    Posts:
    81
    How do I make a method (that is going to be called every time that a button is pressed) to call everything subscribed to the "onEndEdit" event of an InputField?

    I expected something like this:
    Code (CSharp):
    1. public class DoneButtonInputField : MonoBehaviour
    2. {
    3.     [SerializeField] private InputField inputField;
    4.  
    5.     public void ClickDoneButton()
    6.     {
    7.         inputField.onEndEdit();
    8.     }
    9. }
    But it does not work and I do not know how to search for the answer (probably by vocabulary lack).
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Try
    inputField.onEndEdit.Invoke();


    This is kind of a dirty thing to do, though. Events are conceptually meant to represent...well...a particular event occurring. Invoking the listeners when the represented event hasn't actually happened is a violation of the abstraction.

    If this were a regular C# event (rather than a UnityEvent) then you simply wouldn't be allowed to invoke it from outside that class at all.

    Consider refactoring.
     
  3. gillemp

    gillemp

    Joined:
    Nov 23, 2015
    Posts:
    81
    Thanks, this works as I expected:

    Code (CSharp):
    1. inputField.onEndEdit.Invoke(inputField.text);
    And I'm trying to do it because the InputField component does not allow you to have a button to finish the editing of the text, so I'm adding it. That's why I want to call the event when the button is pressed, which means that the edit is finished.
     
    Last edited: Aug 17, 2019