Creating Custom Attributes
Udon Toolkit can be extended with your own custom attributes! All you need is inherit from one of the classes provided below
Example
Here is a sample of an attribute that achieves several goals
Changes the background of the element to a yellow color
Adds a header on top
Adds a reset button on the bottom
The final look we want to achieve

Based on that - we need to do a couple of things
Create a new file for your attributes, let's say
MyCustomAttributes.csanywhere in your projectAdd an
#if UNITY_EDITORat the beginning, followed by an empty line and an#endifBetween those you will create your new class Inheriting from the
UTPropertyAttribute
Another very important thing is that we need a separate version of the attribute for the VRChat's build system (otherwise we'll get build errors, and those aren't fun!)
Change an
#endifto#elseAdd a separate version of
MyAttributethat inherits fromAttribute(that is why we neededusing System;earlier)
With that - let's add it to our behaviour code
Create a new UdonBehaviour and make a script
Inside of the script - make a new public variable, for example
public float someVariableAdd the
MyAttributeattribute to it
If you check the Behaviour now - nothing would change! It will just be a normal field.
Now we can override some methods to make it look different! Let's start with the things above or before the field. For this we'll use the BeforeGUI method.
Add a
public override void BeforeGUI(SerializedProperty property)methodInside of it - add our header label
Follow that with a new
GUI.colorassignment. Which will color everything that is rendered below it - with that color
You should now see the field with a header and colored with a shade of yellow!
The only problem is - if you have any other fields bellow the someVariable they will also be colored in this yellow color, as it will override everything it encounters. Try it out: let's add another variable below the someVaribale.

Now that's not really what we want. Let's fix that by adding another method override that would be called below or after the field. This one is called AfterGUI!
Add a
public override void AfterGUI(SerializedProperty property)methodReset the color back to white by assigning to the
GUI.coloragain
Now its fixed and the 2nd property doesn't get colored.

The only thing left to do now is to add a button!
Add a
GUILayout.Button("Reset Value")wrapped in an ifReset the value of our property to
0inside of that if

And we're done! Read below to see what else you can do by overriding methods like OnGUI and GetVisible
One important thing to note here! You might've noticed that we used a property.floatValue to reset our value. That will only work for... well... variables that are of float type. If you'll want to work with a bool or an int or some other property type - you'll need to check the documentation for serializedProperty to learn how to access and change those!
For those who know about serialized properties - do not worry, you do not need to call ApplyModifiedProperties yourself - UdonToolkit's editor will do it for you!
UTPropertyAttribute
The base class for an attribute that would modify the display logic for a particular property.
It has a couple methods that you can override
GetVisible
public virtual bool GetVisible(SerializedProperty property)
This method determines if the property will be visible in the inspector. The attributes will be called from top to bottom, from left to right. As soon as at least one of the attributes returns false in the GetVisible method - the property won't be drawn and the code will proceed to the next one.
BeforeGUI
public virtual void BeforeGUI(SerializedProperty property)
This method will be called right before drawing the field. It is useful for adding headers and other special info right above the actual field.
OnGUI
public virtual void OnGUI(SerializedProperty property)
This method replaces the drawing of the serialized property with the logic provided within it.
AfterGUI
public virtual void AfterGUI(SerializedProperty property)
This method will be called right after drawing the field. You can add extra information, buttons or anything else that is related to the field above in here.
UTVisualAttribute
The base class for purely data-storing attributes that are generally used to alter general looks of the editor UI and not just the single field it is attached too. Things like ListView or TabGroup are inherited from UTVisualAttribute
While inheriting from this attribute does not do anything on its own - it is useful if you are planning to extend the UTEditor class itself.
Internally its just an empty C# attribute
Last updated