CustomAttribute on Class or Method ( EDIT: Button to call methods)

Hey Guys,

EDIT: I think the Headline was a little missleading. I just Wanted simple buttons with method functionality.

I was sick to implement custom editors just for simple buttons that is why I tried myself on an CustomAttribute. This is how far I got:

TestClass:

[System.Serializable]
public class deleteThis : MonoBehaviour {

    [ButtonVoid("Is this button working", "TestButton")]
    public float test = 0;

    public void TestButton()
    {
        Debug.Log("Button Pressed!");
    }
}

The Attribute Class:

public class ButtonVoidAttribute : PropertyAttribute
{

    public string ButtonName = "";
    public string FunctionName = "";


    public ButtonVoidAttribute(string _ButtonName, string _FunctionToCall)
    {
        ButtonName = _ButtonName;
        FunctionName = _FunctionToCall;
	}
}

And my Drawer:

[CustomPropertyDrawer(typeof(ButtonVoidAttribute))]
public class TestDrawer : PropertyDrawer
{

    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {

        // First get the attribute
        ButtonVoidAttribute tFunction = attribute as ButtonVoidAttribute;

        
 
        if(GUI.Button( position ,tFunction.ButtonName))
        {
            //call the function with the tag on
            (property.serializedObject.targetObject as MonoBehaviour).SendMessage(tFunction.FunctionName);
        }
        
    }
}

And it works the problem is I cant set my CustomAttribute on the function I want or above the class instead it works but only if i set it above a value, this is not my desired behaviour. PICTURE:

Also the second parameter is maybe not needed if i could place the attribute above the method and reference it directly somehow, and I obviously cant give parameters to my functions. I am new to property Drawers and Custom attributes.

Any ideas? This could be very handy. ^^

Thanks for your replies @BMayne,

the ContextMenu is realy fast but it is not visible and so others who work on the project cant see the custom functionality. So I worked a little bit and found an awnser. Its like a mixture of PropertyDrawer and DecoratorDrawer.

EDIT: Found an easy Decorator Drawer solution.

  1. It can only be placed on a Field as BMayne told so its not possible to change that

But here is my fast solution and it works quite fine for my purpose. Maybe someone wants an easy and fast way to use custom code without the need to write a lot of CustomEditors. :slight_smile:

Attribute:
using UnityEngine;

[System.AttributeUsageAttribute(System.AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
public class SimpleButtonNewAttribute : PropertyAttribute
{
    private System.Type classType;
    public System.Type ClassType
    {
        get { return classType; }
        set { classType = value; }
    }

    private string buttonName = "";
    public string ButtonName
    {
        get { return buttonName; }
        set { buttonName = value; }
    }

    private string functionName = "";
    public string FunctionName
    {
        get { return functionName; }
        set { functionName = value; }
    }

    private bool above = false;
    public bool Above
    {
        get { return above; }
        set { above = value; }
    }

    public SimpleButtonNewAttribute(string _FunctionToCall, System.Type _Type ,bool _Above = false)
    {
        ButtonName = "Method: " + _FunctionToCall + "()";
        FunctionName = _FunctionToCall;
        Above = _Above;
        ClassType = _Type;
	}
}

DecoratorDrawer

using UnityEditor;
using UnityEngine;
using System;
using System.Reflection;

[CustomPropertyDrawer(typeof(SimpleButtonNewAttribute))]
public class SimpleButtonNewDrawer : DecoratorDrawer
{
    //Unity GUI function for Property Drawer
    public override void OnGUI(Rect _Position)
    {
        // First get the attribute
        SimpleButtonNewAttribute tAttribute = attribute as SimpleButtonNewAttribute;

        UnityEngine.Object theObject = Selection.activeGameObject.GetComponent(tAttribute.ClassType) as UnityEngine.Object;

        if (GUI.Button(_Position, tAttribute.ButtonName))
        {
            //Match found get the function in MethodInfo for Invoke
            //Debug.Log("Found match:  " + TargetObjects*.GetType() + "   " + tFunction.ClassType);*

MethodInfo tMethod = theObject.GetType().GetMethod(tAttribute.FunctionName);

Debug.Log("Method: " + tMethod + " Object: " + theObject + " Type : " + theObject.GetType() + " found Method: " + theObject.GetType().GetMethod(tAttribute.FunctionName));

if (tMethod != null)
{
//Invoke the method if != null Note: It works only of you dont need special parameters! (null)
tMethod.Invoke(theObject, null);
}
}
}

public override float GetHeight()
{
return base.GetHeight() *2f;
}
}

Hey,

The PropertyAttribute is only valid on fields of a class. The whole point of a Property drawers is to override the drawing of a field that is serialized. Since methods are not serialized (that does not even make sense) you can’t have a PropertyAttribute on them.

There is another function that you can use which is ContextMenu. This is what Unity did to get around the issue that you are trying to solve.

Cheers,