Button.onClick.AddListener
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonController : MonoBehaviour {
public Button myButton;
void Awake()
{
myButton = GetComponent<Button>();
myButton.onClick.AddListener( () => {myFunctionForOnClickEvent("stringValue", 4.5f);} );
}
void myFunctionForOnClickEvent(string arg1, float arg2)
{
print(arg1 + ", " + arg2.ToString());
}
}
You can also remove Listener() using below code on Destroy Function
void Destroy()
{
myButton.onClick.RemoveListener(() => {myFunctionForOnClickEvent("stringValue", 4.5f);});
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonController : MonoBehaviour {
public Button myButton;
void Awake()
{
myButton = GetComponent<Button>();
myButton.onClick.AddListener( () => {myFunctionForOnClickEvent("stringValue", 4.5f);} );
}
void myFunctionForOnClickEvent(string arg1, float arg2)
{
print(arg1 + ", " + arg2.ToString());
}
}
You can also remove Listener() using below code on Destroy Function
void Destroy()
{
myButton.onClick.RemoveListener(() => {myFunctionForOnClickEvent("stringValue", 4.5f);});
}
No comments:
Post a Comment