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:
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…
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;
}
}