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

Set Button OnClick Events Programmatically

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Aug 4, 2017.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am looking for a way to set up button onClick events in the editor, I know, that's what the OnClick event editor is for, but I am looking to do it in the form of an array, so I set these things up, then the buttons get instantiated with the correct events built-in.

    I was hoping that the UnityAction namespace would help with this, but this is not visible in the editor.

    It's probably easier to see what I am attempting to do once you look at the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Events;
    6.  
    7. [System.Serializable]
    8. public class FunctionButtons
    9. {
    10.     public string buttonText;
    11.     public UnityAction buttonActions;
    12.     public bool closesWindow;
    13. }
    14.  
    15. [System.Serializable]
    16. public class Functions
    17. {
    18.     public string name;
    19.     public enum Type { door };
    20.     public string device;
    21.     public enum Network { local, global };
    22.     [SerializeField]
    23.     public FunctionButtons[] actions;
    24. }
    25.  
    26. public class ConsoleFunctions : MonoBehaviour
    27. {
    28.     public GameObject functionTemplate;
    29.  
    30.     [SerializeField]
    31.     public Functions[] functions;
    So, I am attempting to set up an array in which I can set the amount of something, then set up the amount of buttons on it, and the events that will be hooked on to that buttons.

    Is there a way I can set the same thing in the editor in the same way that the onClick editor works, then set the button to it?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    After digging through decompiled Unity DLL's (this shouldn't be this difficult), I thought a setup like this would work fine, but even this, doesn't seem to get the button to do anything, nothing in the onClick events on the button, and nothing like a hidden listener, it just doesn't do anything.

    Code (CSharp):
    1. public Button.ButtonClickedEvent buttonFunction;
    2.  
    3. void Start()
    4. {
    5. button.onClick = buttonFunction as UnityEngine.UI.Button.ButtonClickedEvent;
    6. }
     
    Last edited: Aug 4, 2017
  4. MysterieCatGames

    MysterieCatGames

    Joined:
    Oct 8, 2012
    Posts:
    5
    Code (CSharp):
    1. button.onClick.AddListener(delegate { buttonfunction(); });
     
  5. demonpants

    demonpants

    Joined:
    Oct 3, 2008
    Posts:
    82
    Code (CSharp):
    1. button.onClick.AddListener(MyFunction);