BITKit/Packages/Editor/IntervalUpdateInspector.cs

61 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace BITKit.Editors
{
[CustomPropertyDrawer(typeof(IntervalUpdate))]
public class IntervalUpdateInspector : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
IntervalUpdate intervalUpdate = null;
try
{
intervalUpdate = property.serializedObject
.GetType()
.GetProperty(property.name)?
.GetValue(property.serializedObject) as IntervalUpdate;
}
catch (System.Exception e)
{
Debug.LogException(e);
}
// Create property container element.
var container = new VisualElement();
// Create property fields.
var enable = new Toggle("Enabled");
var updateInterval = new PropertyField(property.FindPropertyRelative(nameof(IntervalUpdate.updateInterval)));
enable.BindProperty(property.FindPropertyRelative(nameof(IntervalUpdate.enable)));
// Add fields to the container.
container.Add(enable);
container.Add(updateInterval);
#if UNITY_EDITOR
enable.RegisterValueChangedCallback(x =>
{
if (UnityEditor.EditorApplication.isPlaying)
{
intervalUpdate?.SetActive(x.newValue);
}
});
#endif
return container;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var enabled = property.FindPropertyRelative("enable");
var updateInterval = property.FindPropertyRelative("updateInterval");
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, enabled, label);
EditorGUI.PropertyField(position, updateInterval, label);
EditorGUI.EndProperty();
}
}
}