Custom GUI

Custom editor GUI

Databrain supports custom UI for your DataObject, made with the new Unity UIElements. You can create quite complex UIs with it which is also how we have created the node editor for the Logic and Techtree addons, for example.

Since version 1.1.0 it is now possible to also use Odin Inspector attributes to create custom editor GUI's. Read more

To create a custom UI with UIToolkit, simply override from EditorGUI in your DataObject class. See the following example:

using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor.UIElements;
#endif

[DataObjectHideAllFields]
public class EnemyData : DataObject
{
    public int health;
    public float speed;
    
    #if UNITY_EDITOR
    // Override CustomGUI to create custom GUI for this DataObject
    public override VisualElement EditorGUI(SerializedObject _serializedObject, DatabrainEditorWindow _editorWindow)
    {
        // Create a root container
        var _root = new VisualElement();
        
        // Lets add a label
        var _label = new Label();
        _label.text = "Enemy Data";
        _label.style.fontSize = 14;
        
        // Add label to the root
        _root.Add(_label);
        
        // Add both properties
        var _healthProp = new PropertyField();
        _healthProp.BindProperty(_serializedObject.FindProperty(nameof(health)));
        
        var _speedProp = new PropertyField();
        _speedProp.BindProperty(_serializedObject.FindProperty(nameof(speed)));
        
        _root.Add(_healthProp);
        _root.Add(_speedProp);
        
        // Return the main container
        return _root;
    }
    #endif
}

Some notes regarding this script:

  • If you're using editor specific methods (like BindProperty), make sure to encapsulate the EditorGUI method in to the #if UNITY_EDITOR directive. The same goes for the UnityEditor namespace.

  • You usually don't want to show the default inspector when using a custom GUI. You can hide all fields by using the DataObjectHideAllFields attribute on the class

Custom runtime GUI

It is also possible to create a custom runtime GUI for a DataObject class. Simply override from RuntimeGUI in your DataObject class.

Here's a simple example:

You will then have to use it in a custom Monobehaviour script which adds the RuntimeGUI to a UIDocument like this:

Add this script to an empty GameObject in the scene and make sure to assign a UIDocument to the document variable.

Last updated