ConditionalField : DrawIf attribute script
#if UNITY_EDITOR #endif // This script uses UnityEditor so it should use preprocessor directives for UNITY_Editor or be placed in an Editor Folder
Script A__________________________________________
using System;
using UnityEngine;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class DrawIfAttribute : PropertyAttribute
{
#region Fields
public string comparePorpertyName { get; private set; }
public object comparedValue { get; private set; }
public DisableType disableType { get; private set; }
public enum DisableType
{
ReadOnly = 2,
DontDraw = 3
}
#endregion
public DrawIfAttribute(string comparedPropertyName, object comparedValue, DisableType disableType = DisableType.DontDraw)
{
this.comparePorpertyName = comparedPropertyName;
this.comparedValue = comparedValue;
this.disableType = disableType;
}
}
Script B_______________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(DrawIfAttribute))]
public class DrawIfPropertyDrawer : PropertyDrawer
{
#region Fields
DrawIfAttribute drawIf;
SerializedProperty comparedField;
#endregion
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (!ShowUI(property) && drawIf.disableType == DrawIfAttribute.DisableType.DontDraw)
{
return -EditorGUIUtility.standardVerticalSpacing;
}
else
{
return EditorGUI.GetPropertyHeight(property, label);
}
}
private bool ShowUI(SerializedProperty property)
{
drawIf = attribute as DrawIfAttribute;
string path = property.propertyPath.Contains(",") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparePorpertyName) : drawIf.comparePorpertyName;
comparedField = property.serializedObject.FindProperty(path);
if (comparedField == null)
{
Debug.LogError("ComparedField error Can't find property : " + path);
return true;
}
switch (comparedField.type)
{
case "bool":
return comparedField.boolValue.Equals(drawIf.comparedValue);
case "Enum":
return comparedField.enumValueIndex.Equals((int)drawIf.comparedValue);
default:
Debug.LogError("Error : " + comparedField.type + " is not supported " + path);
return true;
}
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (ShowUI(property))
{
// A Generic type means a custom class...
if (property.propertyType == SerializedPropertyType.Generic)
{
IEnumerator children = property.GetEnumerator();
Rect offsetPosition = position;
while (children.MoveNext())
{
SerializedProperty child = children.Current as SerializedProperty;
GUIContent childLabel = new GUIContent(child.displayName);
float childHeight = EditorGUI.GetPropertyHeight(child, childLabel);
offsetPosition.height = childHeight;
EditorGUI.PropertyField(offsetPosition, child, childLabel);
offsetPosition.y += childHeight + EditorGUIUtility.standardVerticalSpacing;
}
}
else
{
EditorGUI.PropertyField(position, property, label);
}
} //...check if the disabling type is read only. If it is, draw it disabled
else if (drawIf.disableType == DrawIfAttribute.DisableType.ReadOnly)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label);
GUI.enabled = true;
}
}
}
댓글
댓글 쓰기