- Home /
Question by
Michael_Berna · Oct 23, 2021 at 07:08 PM ·
c#functionsevent triggeringcallback
Passing parameters dynamically with Unity Event
I want to create a class that allows me to call functions when a textmesh pro link is clicked and send it the link ID. I want dynamic callback functions that can be added and removed individually so that I can use the same class and just make small inspector changes. I do get the link ID properly in the onpointerclick function, but the problem I am having is that the event always gets an empty string passed. How can I do this properly? I need the events to be a dynamic inspector serialized field and to have the linkID not fixed in the inspector because I have multiple links in a single textmesh and the textmesh text body is dynamically filled, so I don't know what it will be until runtime. Here's my current partially working code:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickableText : MonoBehaviour, IPointerClickHandler
{
[SerializeField]EventWithParams m_OnTextClick;
[System.Serializable]
public class EventWithParams : UnityEvent<string> { }
public void OnPointerClick(PointerEventData eventData)
{
var text = GetComponent<TextMeshProUGUI>();
if(eventData.button == PointerEventData.InputButton.Left)//left mouse click
{
int linkIndex = TMP_TextUtilities.FindIntersectingLink(text, Input.mousePosition, null);
if(linkIndex > -1)
{
TMP_LinkInfo linkInfo = text.textInfo.linkInfo[linkIndex];
string linkID = linkInfo.GetLinkID();
if (m_OnTextClick.GetPersistentEventCount()>0)
{
m_OnTextClick.Invoke(linkID);//the linkID isn't passed properly here even though our class supports a string parameter and it is specified here
}
else //default case for no function assigned
{
Debug.Log("clicked on link: " + linkID + ", but no function assigned, so defaulting to quest journal link function. This may not be desired....");
JournalManager.UpdateJournal(linkID);//the linkID is passed properly here
}
}
}
}
}
Comment