using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public class MaxLengthAttribute : PropertyAttribute { public readonly uint Length; public MaxLengthAttribute(uint length) { Length = length; } } #if UNITY_EDITOR [CustomPropertyDrawer(typeof(MaxLengthAttribute))] public class MaxLengthAttributeEditor : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.propertyType == SerializedPropertyType.String) { MaxLengthAttribute attr = (MaxLengthAttribute)attribute; string value = property.stringValue; if (value.Length > attr.Length) { value = value.Substring(0, (int)attr.Length); property.stringValue = value; property.serializedObject.ApplyModifiedProperties(); } } EditorGUI.PropertyField(position, property, label); } } #endif