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
    1.
    Create a new file for your attributes, let's say MyCustomAttributes.cs anywhere in your project
    2.
    Add an #if UNITY_EDITOR at the beginning, followed by an empty line and an #endif
    3.
    Between those you will create your new class Inheriting from the UTPropertyAttribute
1
using UnityEditor;
2
using UnityEngine;
3
using System; // This in particular is very important
4
// As it will be used down th eline
5
6
#if UNITY_EDITOR
7
public class MyAttribute : UTPropertyAttribute {
8
}
9
#endif
Copied!
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!)
    1.
    Change an #endif to #else
    2.
    Add a separate version of MyAttribute that inherits from Attribute (that is why we needed using System; earlier)
1
using UnityEditor;
2
using UnityEngine;
3
using System; // This in particular is very important
4
// As it will be used down th eline
5
6
#if UNITY_EDITOR
7
public class MyAttribute : UTPropertyAttribute {
8
}
9
#else
10
// this just tells unity to use this attribute on fields
11
[AttributeUsage(AttributeTargets.Field)]
12
public class MyAttribute : Attribute {
13
public MyAttribute() { // since we do not have any parameters
14
// we create an empty constructor
15
}
16
}
17
#endif
Copied!
The above process is required, because VRChat will need some version of the attribute to build the code. In this case we are using a dummy empty attribute as nothing we're doing here is important for the final build.
You can check the last third of the UTAttributes file to see how to handle attributes with parameters in a similar fashion
With that - let's add it to our behaviour code
    1.
    Create a new UdonBehaviour and make a script
    2.
    Inside of the script - make a new public variable, for example public float someVariable
    3.
    Add the MyAttribute attribute to it
1
public class CustomAttributeTest : UdonSharpBehaviour {
2
[MyAttribute] public float someVariable;
3
private void Start() {
4
}
5
}
Copied!
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.
    1.
    Add a public override void BeforeGUI(SerializedProperty property) method
    2.
    Inside of it - add our header label
    3.
    Follow that with a new GUI.color assignment. Which will color everything that is rendered below it - with that color
1
#if UNITY_EDITOR
2
public class MyAttribute : UTPropertyAttribute {
3
public override void BeforeGUI(SerializedProperty property) {
4
EditorGUILayout.LabelField("HEADER TEXT", EditorStyles.largeLabel);
5
GUI.color = new Color(0.98f, 0.92f, 0.35f);
6
}
7
}
8
#endif
Copied!
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.
1
public class CustomAttributeTest : UdonSharpBehaviour {
2
[MyAttribute] public float someVariable;
3
4
public bool anotherVariable;
5
6
private void Start() {
7
}
8
}
Copied!
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!
    1.
    Add a public override void AfterGUI(SerializedProperty property) method
    2.
    Reset the color back to white by assigning to the GUI.color again
1
#if UNITY_EDITOR
2
public class MyAttribute : UTPropertyAttribute {
3
public override void BeforeGUI(SerializedProperty property) {
4
EditorGUILayout.LabelField("HEADER TEXT", EditorStyles.largeLabel);
5
GUI.color = new Color(0.98f, 0.92f, 0.35f);
6
}
7
8
public override void AfterGUI(SerializedProperty property) {
9
GUI.color = Color.white;
10
}
11
}
12
#endif
Copied!
Now its fixed and the 2nd property doesn't get colored.
The only thing left to do now is to add a button!
    1.
    Add a GUILayout.Button("Reset Value") wrapped in an if
    2.
    Reset the value of our property to 0 inside of that if
1
#if UNITY_EDITOR
2
public class MyAttribute : UTPropertyAttribute {
3
public override void BeforeGUI(SerializedProperty property) {
4
EditorGUILayout.LabelField("HEADER TEXT", EditorStyles.largeLabel);
5
GUI.color = new Color(0.98f, 0.92f, 0.35f);
6
}
7
8
public override void AfterGUI(SerializedProperty property) {
9
GUI.color = Color.white;
10
if (GUILayout.Button("Reset Value")) {
11
property.floatValue = 0;
12
}
13
}
14
}
15
#endif
Copied!
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!
The final code is provided in the Demo/CustomAttributeSample folder, so feel free to use it as a starting point!

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.
1
// HideIf attribute implementation
2
public override bool GetVisible(SerializedProperty property) {
3
isVisible = UTUtils.GetVisibleThroughAttribute(property, methodName, true);
4
return isVisible;
5
}
Copied!

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.
1
// SectionHeader attribute implementation
2
public override void BeforeGUI(SerializedProperty property) {
3
UTStyles.RenderSectionHeader(text);
4
}
Copied!

OnGUI

public virtual void OnGUI(SerializedProperty property)
This method replaces the drawing of the serialized property with the logic provided within it.
UT will always use the first provided OnGUI method. So if the property has multiple attributes that override OnGUI - only the first one will be called
1
// Toggle attribute implementation
2
public override void OnGUI(SerializedProperty property) {
3
var text = String.IsNullOrWhiteSpace(label) ? property.displayName : label;
4
property.boolValue = GUILayout.Toggle(property.boolValue, text, "Button");
5
}
Copied!

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.
1
// HelpBox attribute implementation
2
public override void AfterGUI(SerializedProperty property) {
3
UTStyles.RenderNote(text);
4
}
Copied!

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
1
/// <summary>
2
/// These attributes are used to pass dat to custom logic in the UTEditor
3
/// </summary>
4
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
5
public class UTVisualAttribute : Attribute {
6
}
Copied!
Last modified 6mo ago