143 lines
5.3 KiB
C#

using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Setting))]
public class SettingEditor : Editor
{
private SerializedProperty typeProperty;
private SerializedProperty idProperty;
private SerializedProperty aliasProperty;
private SerializedProperty descriptionProperty;
// Bool fields
private SerializedProperty boolValueProperty;
// Int fields
private SerializedProperty intValueProperty;
private SerializedProperty intMinProperty;
private SerializedProperty intMaxProperty;
private SerializedProperty intStepProperty;
// Float fields
private SerializedProperty floatValueProperty;
private SerializedProperty floatMinProperty;
private SerializedProperty floatMaxProperty;
private SerializedProperty floatStepProperty;
// String fields
private SerializedProperty stringValueProperty;
private SerializedProperty allowedValuesProperty;
//Signal fields
private SerializedProperty signalProperty;
private void OnEnable()
{
// General fields
typeProperty = serializedObject.FindProperty("Type");
idProperty = serializedObject.FindProperty("Id");
aliasProperty = serializedObject.FindProperty("Alias");
descriptionProperty = serializedObject.FindProperty("Description");
// Bool fields
boolValueProperty = serializedObject.FindProperty("BoolValue");
// Int fields
intValueProperty = serializedObject.FindProperty("IntValue");
intMinProperty = serializedObject.FindProperty("IntMin");
intMaxProperty = serializedObject.FindProperty("IntMax");
intStepProperty = serializedObject.FindProperty("IntStep");
// Float fields
floatValueProperty = serializedObject.FindProperty("FloatValue");
floatMinProperty = serializedObject.FindProperty("FloatMin");
floatMaxProperty = serializedObject.FindProperty("FloatMax");
floatStepProperty = serializedObject.FindProperty("FloatStep");
// String fields
stringValueProperty = serializedObject.FindProperty("StringValue");
allowedValuesProperty = serializedObject.FindProperty("AllowedValues");
//Signal fields
signalProperty = serializedObject.FindProperty("SignalValue");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
// Draw General fields (always visible)
EditorGUILayout.LabelField("General", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(typeProperty);
EditorGUILayout.PropertyField(idProperty);
EditorGUILayout.PropertyField(aliasProperty);
EditorGUILayout.PropertyField(descriptionProperty);
EditorGUILayout.Space(5);
// Draw type-specific fields
Setting.SettingType currentType = (Setting.SettingType)typeProperty.enumValueIndex;
switch (currentType)
{
case Setting.SettingType.Bool:
EditorGUILayout.LabelField("Bool Settings", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(boolValueProperty);
break;
case Setting.SettingType.Int:
EditorGUILayout.LabelField("Int Settings", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(intValueProperty);
EditorGUILayout.PropertyField(intMinProperty);
EditorGUILayout.PropertyField(intMaxProperty);
EditorGUILayout.PropertyField(intStepProperty);
break;
case Setting.SettingType.Float:
EditorGUILayout.LabelField("Float Settings", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(floatValueProperty);
EditorGUILayout.PropertyField(floatMinProperty);
EditorGUILayout.PropertyField(floatMaxProperty);
EditorGUILayout.PropertyField(floatStepProperty);
break;
case Setting.SettingType.String:
EditorGUILayout.LabelField("String Settings", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(stringValueProperty);
EditorGUILayout.PropertyField(allowedValuesProperty);
break;
case Setting.SettingType.Signal:
EditorGUILayout.LabelField("Signal Settings", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(signalProperty);
break;
}
serializedObject.ApplyModifiedProperties();
// Add validation for Int and Float values
ValidateValues();
}
private void ValidateValues()
{
Setting setting = (Setting)target;
if (setting.Type == Setting.SettingType.Int)
{
// Clamp IntValue between min and max
if (setting.IntValue < setting.IntMin)
setting.IntValue = setting.IntMin;
if (setting.IntValue > setting.IntMax)
setting.IntValue = setting.IntMax;
}
else if (setting.Type == Setting.SettingType.Float)
{
// Clamp FloatValue between min and max
if (setting.FloatValue < setting.FloatMin)
setting.FloatValue = setting.FloatMin;
if (setting.FloatValue > setting.FloatMax)
setting.FloatValue = setting.FloatMax;
}
}
}