Unity team should provide some examples in the script reference…
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
[ContextMenuItem( "test", "DoIt" )]
public int someInt;
public void DoIt()
{
Debug.Log("Hello");
}
}
Attach this to a gameobject. When you right click on someInt a contextMenu will pop up with the option “test”. When clicked, function DoIt() will execute
This thing isn’t new, it’s been around for aaaaages.
Anyway, element_wsc is almost right:
public class Test : MonoBehaviour
{
[ContextMenu("Test function")]
public void TestFunction()
{
Debug.Log("Hello");
}
}
You attach the attribute to the function you want to execute, and it only takes one parameter.
Woah, that’s a new one for me. Must’ve just come in 4.3. So it actually adds an attribute to a particular field (as opposed to a method)?
Edit: Ah I see, the docs have been updated to 4.5 ![]()
Yes ContextMenuItem goes on a field, like in my example and ContextMenu on a Method like superpig’s example
Ah, my mistake. Got confused by the OP linking to ContextMenuAttribute.
Anyone know the use case for ContextMenuItemAttribute? Seems kinda pointless to me…
Thank you, but, what is the point to attach it to a specific variable then?
I thought I would be able to send that variable as a parameter to function I wanted to call or something like that. I wanted to see a use case so that I wanted to right click on some variable to call a function…
I have to download 4.5 at home…
Anyone wanna bet this attribute is restricted to Field and cannot be applied to Properties? ![]()