27 lines
930 B
C#
27 lines
930 B
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Module.Inspector.Editor
|
|
{
|
|
[CustomPropertyDrawer(typeof(ArrayIndex))]
|
|
internal sealed class DrawerArrayIndex : ValueModifierPropertyDrawer
|
|
{
|
|
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
|
|
{
|
|
var att = (ArrayIndex)attribute;
|
|
|
|
int arraySize = GetSiblingArraySize(property, att);
|
|
int max = arraySize > 0 ? arraySize - 1 : 0;
|
|
int value = Mathf.Clamp(property.intValue, 0, max);
|
|
|
|
if (value != property.intValue)
|
|
property.intValue = value;
|
|
}
|
|
|
|
private static int GetSiblingArraySize(SerializedProperty sp, ArrayIndex att)
|
|
{
|
|
SerializedProperty spSibling = sp.GetSibling(att.fieldName);
|
|
return spSibling != null && spSibling.isArray ? spSibling.arraySize : 0;
|
|
}
|
|
}
|
|
} |