I have a simple Property Drawer that draws a Toggle and a Label. The toggle just toggles the visibility of the label. This Property Drawer applies to the class Test. I have a test field in a MonoBehaviour and a ScriptableObject.
Instances of my MonoBehaviour work fine the button toggles the labels visibility on and off. But, for instances of my Scriptable Object things are a little odd:
If I have no item in the project window selected and select the ScriptableObject the drawer behaves as I would expect toggling the labels visibility. However if I have any item in the project window selected prior to selecting the ScriptableObject the drawer has no response to toggling.
After debugging it seems like the button’s visibility is toggling. But, the button I have reference to doesn’t seem to be the one shown in the inspector. I cant quite figure this out.
The code is pretty simple I’ve left it below (I assume I’m doing something really stupid haha)
Test Class
[System.Serializable]
public class Test { }
MonoBehaviour Test Container
using UnityEngine;
public class TestHolderMono: MonoBehaviour
{
[SerializeField] Test test;
}
ScriptableObject Test Container
using UnityEngine;
public class TestHolderSO: MonoBehaviour
{
[SerializeField] Test test;
}
Property Drawer:
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[CustomPropertyDrawer(typeof(Test))]
public class TestDrawer : PropertyDrawer
{
bool showLabel = true;
event Action<bool> ShowLabelChanged;
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
// Clear event registration
ShowLabelChanged = null;
// Create Label Toggle
Toggle showLabelToggle = new Toggle("Show Test Buttons");
showLabelToggle.value = showLabel;
showLabelToggle.RegisterValueChangedCallback(
(val) =>
{
showLabel = val.newValue;
ShowLabelChanged?.Invoke(showLabel);
}
);
// Create Label
// Update Display Type when showLabel Changed
Label label = new("Test");
ShowLabelChanged += UpdateButtonDisplay;
void UpdateButtonDisplay(bool show)
{
label.style.display = show ? DisplayStyle.Flex : DisplayStyle.None;
Debug.Log($"Changed DisplayStyle to: {label.style.display}");
}
// Create, populate, and return root
VisualElement root = new();
root.Add(showLabelToggle);
root.Add(label);
return root;
}
}