How to fire UnityEvent with parameters passed programmatically

Hi!
I’ve created my own selectable with an UnityEvent onValueChange. I programmatically invoke it with parameter but it seems that listeners receive parameter set in inspector rather than the one sent in Invoke call. Is there a way to set which argument should be passed? Or may I somehow get into that value from inspector to change it on the fly?

This is part of my setup:

41966-unityeventoverride.png

Functions SetRightLaneCount(int count) are fired with count set to 0 or whatever is set in that field in inspector.

But I fire the event manually:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using UnityEngine.Events;

public class IncrementalInput : Selectable
{
	[SerializeField]
	private Text text;
	[SerializeField]
	private Button PlusButton;
	[SerializeField]
	private Button MinusButton;
	public int MinValue;
	public int MaxValue;

	private int currentValue = 1;
	public int CurrentValue
	{
		get { return currentValue; }
		set { currentValue = value;
			if( CurrentValue == MaxValue )
				PlusButton.interactable = false;
			else if( CurrentValue == MinValue )
				MinusButton.interactable = false;
			else {
				MinusButton.interactable = true;
				PlusButton.interactable = true;
			}
			onValueChanged.Invoke(currentValue); //Event is fired
			text.text = value.ToString();
		}
	}

	[System.Serializable]
	public class OnValueChanged : UnityEvent<int> { };
	public OnValueChanged onValueChanged;


	public void Add()
	{
		if( CurrentValue < MaxValue )
			CurrentValue++;
	}

	public void Substract()
	{
		if( CurrentValue > MinValue )
			CurrentValue--;
	}


}

So everything works fine: currentValue changes, events are fired, but don’t use correct variable as an argument.

Well, it was after all pretty simple. Functions cannot be static. If they have a form described in the Documentation there will be two functions on the list with the same name! One is in section with dynamic parameters functions and the other in section of static parameters functions. If You want to fire UnityEvent like the one with my code all You have to do is to choose the one from dynamic parameters section and everything will work perfectly. Event will be fired with parameters passed in the Invoke method.

Suppose you declared unityevent as follows

[System.Serializable]
	public class ToggleButtonClickCallBack : UnityEvent<bool>{
	}
	public ToggleButtonClickCallBack OnToggle = new ButtonClickCallBack ();

Now OnToggle Unity Event will be shown in inspector with a bool parameter if you select static parameter method.

If you invoke OnToggle event with dynamic parameter it will only use parameter set in inspector and discard dynamic parameter that we passed at invocation time.

For that I have got a solution……

When you want to call OnToggle event with dynamic parameter use Unityevent class methods as follows to call all the registered methods with dynamic parameter value as you want.

for(int i = 0 ; i < OnToggle.GetPersistentEventCount();i++ ){	
			((MonoBehaviour)OnToggle.GetPersistentTarget(i)).SendMessage(OnToggle.GetPersistentMethodName(i),dynamicValue);
}

Advantage of this is you can set a default value in inspector and call simply invoke to use default value and when you want dynamic parameters you can call as described above.

You can apply some filters also.

UnityEventDyanmicInvoke
Just Select the Dynamic variant of that method and it will get the value which will be passed from the code.