using System; using UnityEditor; using UnityEngine; namespace NBShaderEditor { public class ToggleItem : ShaderGUIItem { private readonly Func _contentProvider; private readonly Func _isVisible; private readonly Action _onValueChanged; public ToggleItem( ShaderGUIRootItem rootItem, ShaderGUIItem parentItem, string propertyName, Func contentProvider, Action onValueChanged = null, Func isVisible = null) : base(rootItem, parentItem) { PropertyName = propertyName; _contentProvider = contentProvider ?? (() => GUIContent.none); _onValueChanged = onValueChanged; _isVisible = isVisible; GuiContent = _contentProvider(); InitTriggerByChild(); } public override void OnGUI() { if (_isVisible != null && !_isVisible()) { return; } base.OnGUI(); } public override void DrawController() { bool value = PropertyInfo.Property.floatValue > 0.5f; value = EditorGUI.Toggle(ControlRect, value); SetFloatIfDifferent(PropertyInfo.Property, value ? 1f : 0f); } public override void OnEndChange() { base.OnEndChange(); _onValueChanged?.Invoke(PropertyInfo.Property.floatValue > 0.5f); } public override void ExecuteReset(bool isCallByParent = false) { base.ExecuteReset(isCallByParent); _onValueChanged?.Invoke(PropertyInfo.Property.floatValue > 0.5f); } } }