Game Engine/유니티(Unity)
Custom Attribue: Public 변수 인스펙터상 ReadOnly로 만들기
3영2
2023. 6. 13. 11:44
반응형
Public을 인스펙터에서 수정하지 못하게 하고 싶을때
사용 예시:
1 2 | [ReadOnly] public AnimationState currentCarAnimationState = AnimationState.Idle; [ReadOnly] public AnimationState currentCharAnimationState = AnimationState.Idle; | cs |

커스텀 Attribute
소스 코드:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using UnityEngine; using System; #if UNITY_EDITOR namespace UnityEditor { [CustomPropertyDrawer(typeof(ReadOnlyAttribute), true)] public class ReadOnlyAttributeDrawer : PropertyDrawer { // Necessary since some properties tend to collapse smaller than their content public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return EditorGUI.GetPropertyHeight(property, label, true); } // Draw a disabled property field public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { GUI.enabled = !Application.isPlaying && ((ReadOnlyAttribute)attribute).runtimeOnly; EditorGUI.PropertyField(position, property, label, true); GUI.enabled = true; } } } #endif [AttributeUsage(AttributeTargets.Field)] public class ReadOnlyAttribute : PropertyAttribute { public readonly bool runtimeOnly; public ReadOnlyAttribute(bool runtimeOnly = false) { this.runtimeOnly = runtimeOnly; } } | cs |
반응형