Monday, May 20, 2024
HomeGame Developmentunity - How can I use `UnityEngine.UIElements.PopupWindow` for a custom PropertyDrawer with...

unity – How can I use `UnityEngine.UIElements.PopupWindow` for a custom PropertyDrawer with UI Toolkit?


I’m following the “Structure UI” car and tire example that Unity has in their documentation for UI Toolkit, but I’m trying to modify it in a specific way and I’m coming up short (whether or not it is a “good” way is beyond the scope of the question). Essentially, I’d like to take the editor display for an array and make it accessible by a single button that opens a PopupWindow in the editor, displaying the information for all four tires. I’d also like to make sure that the array is not reorderable, and if possible, I’d like to have it not act like a foldout in the PopupWindow.

I’ve managed to get this so far:

a sort-of functional popup behind a Button that does not display the information from TireSpecs.cs

In Car.cs, I have the following:

using UnityEngine;

public class Car : MonoBehaviour
{
    public string myMake = "Ford";
    public int myYearBuilt = 2001;
    public Color myColor = new Color(0.5725f, 0.525f, 0.3875f, 1.0f);
    
    public TireSpecs[] myTires = new TireSpecs[4];
    public ActualTireDetails tireOptions;
}

In TireSpecs.cs, I have the following:

using System;
using UnityEngine;

[Serializable]
public class TireSpecs
{
    public float myAirPressure = 35.0f;
    public int myProfileDepth = 4;

    public TireSpecs(float airPressure, int profileDepth)
    {
        myAirPressure = airPressure;
        myProfileDepth = profileDepth;
    }
}

And my PropertyDrawer, TirePropertyDrawer.cs, is structured like this:

using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.UIElements;
using System.Collections.Generic;

// Instead, writing [CustomPropertyDrawer(typeof(TireSpecs[]))] 
// will display the information of the tires in a reorderable array,
// but without the button or popup window

[CustomPropertyDrawer(typeof(TireSpecs))]
public class TirePropertyDrawer : PropertyDrawer
{
    public override VisualElement CreatePropertyGUI(SerializedProperty property)
    {
        var container = new VisualElement();

        var button = new Button(() => ShowPopup(container)) { text = "Open Popup" };
        container.Add(button);

        return container;
    }

    void ShowPopup(VisualElement anchor)
    {
        UnityEngine.UIElements.PopupWindow popup = new UnityEngine.UIElements.PopupWindow();
        popup.Add(new Label("This is a Popup"));
        popup.style.width = 200;
        popup.style.height = 100;

        var button = new Button(() => anchor.Remove(popup)) { text = "Close Popup" };
        popup.Add(button);
        anchor.Add(popup);
    }
}

```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments