Sp0ggy
March 2, 2020, 2:34pm
1
Struggling for a while with this, hoping someone can help. The code below works fine with a button component (but) for the onClick event.
MethodInfo inf = UnityEventBase.GetValidMethodInfo(script, "Click", new System.Type[] { });
if (inf != null)
{
UnityAction execute = () => inf.Invoke(script, null);
but.onClick.AddListener(execute);
}
What i’m trying to work out is how to implement this for a scrollbar component with on value changed, as this involves a float parameter. Any help appreciated.
MethodInfo inf = UnityEventBase.GetValidMethodInfo(this, "ValueChanged", new Type[] { typeof(float) });
if (inf != null)
{
UnityAction<float> execute = (newValue) => inf.Invoke(this, new object[] { newValue });
scrollbar.onValueChanged.AddListener(execute);
}
...
public void ValueChanged(float newValue)
{
Debug.Log(newValue);
}
1 Like
Sp0ggy
March 2, 2020, 3:58pm
3
Hikiko66:
MethodInfo inf = UnityEventBase.GetValidMethodInfo(this, "ValueChanged", new Type[] { typeof(float) });
if (inf != null)
{
UnityAction<float> execute = (newValue) => inf.Invoke(this, new object[] { newValue });
scrollbar.onValueChanged.AddListener(execute);
}
...
public void ValueChanged(float newValue)
{
Debug.Log(newValue);
}
Thank you a million times over for this. Just tried and it works perfectly