Using UnityEventTools.RemovePersistentListener

I am creating an editor script that creates UI buttons and add listeners with arguments. I have completed that. Now, I want to remove the listeners with code.

The unity documentation for this isn’t much help.

Code:

GameObject my_UIPanel;

// Destroy panel and buttons that were created previously.

my_UIPanel = GameObject.Find ("Panel_RoomButtons");
Button[] myBtns = my_UIPanel.GetComponentsInChildren<Button> ();
foreach (Button myBtn in myBtns) {

**//------------------------------------------------------------------------------------##**
**// This is the thing giving me a headache. Rest everything works fine.**
**//------------------------------------------------------------------------------------##**
	UnityEventTools.RemovePersistentListener(myBtn.onClick, myUIManager.DEV_myFunction); 
}
if (my_UIPanel != null) 
	DestroyImmediate (my_UIPanel);
	

// Create new panel and buttons.

my_UIPanel = Instantiate(Pnl_RoomBtnsPrefab) as GameObject;

foreach (GameObject emptyLocation in teleLocations) {
	GameObject myBtnGO = Instantiate (teleBtnPrefab) as GameObject;
	myBtnGO.name = emptyLocation.name + "Btn";
	myBtnGO.transform.SetParent (my_UIPanel.transform, false);
	GameObject currentGO = emptyLocation;
	Button myBtn = myBtnGO.GetComponent <Button> ();
	UnityEventTools.AddObjectPersistentListener(myBtn.onClick, myUIManager.DEV_myFunction, currentGO);
}

If you can help me solve this, I’d be grateful for your help.

Thanks!

@ImFromTheFuture Can you provide a little more information about what exactly is happening? The following example works just fine for me:

using UnityEditor;
using UnityEngine;

public class EventHandlerTest : EditorWindow
{
	[MenuItem("Window/Event Handler Test Window")]
	static void CreateNew()
	{
		EditorWindow.GetWindow<EventHandlerTest>();
	}
	private void OnGUI()
	{
		UnityEngine.UI.Button selectedButton =
			Selection.activeGameObject == null ? null : Selection.activeGameObject.GetComponent<UnityEngine.UI.Button>();
		if (selectedButton == null)
			return;
		EditorGUI.BeginDisabledGroup(selectedButton.onClick.GetPersistentEventCount() == 1);
		if (GUILayout.Button("Register"))
			UnityEditor.Events.UnityEventTools.AddStringPersistentListener(selectedButton.onClick, selectedButton.SendMessage, "SomeString");
		EditorGUI.EndDisabledGroup();
		EditorGUI.BeginDisabledGroup(selectedButton.onClick.GetPersistentEventCount() == 0);
		if (GUILayout.Button("Deregister"))
			UnityEditor.Events.UnityEventTools.RemovePersistentListener(selectedButton.onClick, 0);
		EditorGUI.EndDisabledGroup();
	}
}