- Home /
Finding a method by custom attribute and passing to a UI button
Hey all, I'm trying to put together a right click context menu system. Having an issue however with trying to populate the context menu with entries. I have a custom attribute that I can use to mark methods as being a menu item that takes a single string parameter to denote the text to display on the context button. For instance the code:
[contextMethod("Eat")]
void Eat()
{
Debug.Log("Eaten!");
}
Produces an item in the menu pictured below:
There are a couple of issues here. The first is that to create this I am iterating over the methods as MethodInfo types and creating the menu according to whether or not one of the custom attributes is applied to it. This does not give me access to the parameter of the attribute, which means I can't get the text from the attribute.
The other issue this creates is that I also cannot pass the method to the UI button as the UI button wants a UnityAction type and I only have MethodInfo types.
I am pretty confused by this point, and would appreciate any hints for how to make this work. Since I would be marking these methods pretty regularly an attribute seems like the ideal solution but if all else fails an interface might work too...
Here is the script if anyone wants to take a look: https://gist.github.com/chilisource/c7e1775d0db23eb130edf41d0336a99f
Thanks for any advice.
Answer by yummy81 · Jan 31, 2018 at 11:19 AM
So you have here basically two problems:
How to get access to the parameter of the attribute and get the text from it.
How to pass the method corresponding to the attribute to the UI button onClick event.
The first one: Delete this line of code:
btn.GetComponentInChildren<Text>().text = method.Name;
and paste this instead:
btn.GetComponentInChildren<Text>().text = (attributes[0] as contextMethod).contextTitle;
For the second one: Delete this line of code, because it's unnecessary:
UnityAction action;
Next, replace that one:
btn.GetComponent<Button>().onClick.AddListener(action);
with this:
MethodInfo info = UnityEventBase.GetValidMethodInfo(mono, method.Name, new System.Type[0]);
UnityAction execute = () => info.Invoke(mono, null);
btn.GetComponent<Button>().onClick.AddListener(execute);
and that's it. It should work now. Here you have the whole script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Events;
using System.Reflection;
using System;
public class ContextMenu : MonoBehaviour
{
public GameObject targetObj;
public Transform contentHolder;
public GameObject actionButton;
[SerializeField]
private List<GameObject> contextMenuItems = new List<GameObject>();
private void Awake()
{
MonoBehaviour[] scriptComponents = this.targetObj.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour mono in scriptComponents)
{
var monoType = mono.GetType();
foreach (var method in monoType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
{
var attributes = method.GetCustomAttributes(typeof(contextMethod), true);
if (attributes.Length > 0)
{
GameObject btn = Instantiate(this.actionButton) as GameObject;
contextMenuItems.Add(btn);
btn.transform.SetParent(this.contentHolder);
btn.GetComponentInChildren<Text>().text = (attributes[0] as contextMethod).contextTitle;
MethodInfo info = UnityEventBase.GetValidMethodInfo(mono, method.Name, new System.Type[0]);
UnityAction execute = () => info.Invoke(mono, null);
btn.GetComponent<Button>().onClick.AddListener(execute);
}
}
}
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class contextMethod : Attribute
{
public string contextTitle;
public contextMethod(string menuEntryTitle)
{
this.contextTitle = menuEntryTitle;
}
}