Writing PropertyDrawers for functions.

Hi all - I’m searching for something that may or may not be possible. You can create custom property drawers for variables, like the one below.

float range = 5;

However, although Unity has the ContextMenu attribute for functions, I’ve never seen a custom propertydrawer for a function.

[ContextMenu("Do whatever")]
void Whatever () {...}

In particular, I’m looking to display a button in the inspector for any functions with a custom [Button] attribute. Is such a thing possible?

Otherwise, how else could you do about creating a button using propertydrawers (rather than writing an editor script, which is just cumbersome)?



I’m personally looking for a better implementation than the one I have but here are my 2 cents on the topic.

As far as I know this is a kinda, I have not been able to make Unity recognize a class method as a property, these seem to be (and I may be completely wrong here) reserved for fields only.

My solution was to create a custom attribute called ButtonUtilityAttribute that is then attached to a field (I normally make them be bytes) and that uses reflection for when the button is hit to call the function.

In code this would look somewhat like this:

[SerializeField, ButtonUtilityAttribute("UseSelected", "Use Selected")]
	private byte useSelectedButton;

	public void UseSelected()
		Debug.Log("Doing something");

Personally I would like to be able to move the attribute to the method rather than having to create a dummy variable in the class.

Anyhow hope this helps for the time being. The drawer implementation is rather straightforward, just use reflection on the object it is running for to find the method call that is specified in the attribute constructor and set the label as the second parameter from the attribute.