using UnityEngine; using UnityEditor; using UnityEngine.UIElements; namespace EditorAttributes.Editor { [CustomPropertyDrawer(typeof(ToggleGroupAttribute))] public class ToggleGroupDrawer : PropertyDrawerBase { public override VisualElement CreatePropertyGUI(SerializedProperty property) { var toggleGroup = attribute as ToggleGroupAttribute; var foldoutSaveKey = CreatePropertySaveKey(property, "IsToggleGroupFolded"); var toggleSaveKey = CreatePropertySaveKey(property, "IsToggleGroupToggled"); var root = new VisualElement(); var foldout = new Foldout { text = toggleGroup.GroupName, tooltip = property.tooltip, style = { unityFontStyleAndWeight = FontStyle.Bold }, value = EditorPrefs.GetBool(foldoutSaveKey) }; var toggleBox = new Toggle() { text = "", style = { marginRight = 10f }, value = property.propertyType == SerializedPropertyType.Boolean ? property.boolValue : EditorPrefs.GetBool(toggleSaveKey) }; foldout.contentContainer.SetEnabled(toggleBox.value); if (toggleGroup.DrawInBox) ApplyBoxStyle(foldout.contentContainer); root.Add(toggleBox); foreach (string variableName in toggleGroup.FieldsToGroup) { var propertyField = CreateField(variableName, property, root); foldout.Add(propertyField); } toggleBox.RegisterValueChangedCallback((callback) => { if (property.propertyType == SerializedPropertyType.Boolean) { property.boolValue = callback.newValue; property.serializedObject.ApplyModifiedProperties(); } else { EditorPrefs.SetBool(toggleSaveKey, callback.newValue); // The value is already serialized via the property, there is no point in saving it. } foldout.contentContainer.SetEnabled(callback.newValue); }); root.Add(foldout); ExecuteLater(foldout, () => { var toggle = foldout.Q(); toggle.style.backgroundColor = CanApplyGlobalColor ? EditorExtension.GLOBAL_COLOR / 3f : new Color(0.1f, 0.1f, 0.1f, 0.2f); var parentElement = foldout.Q