public static void ObjectField(Rect position, GUIContent label, PersistentProperty property) { Debug.Assert(typeof(Object).IsAssignableFrom(property.type)); EditorGUI.BeginChangeCheck(); var obj = ObjectField(position, label, property.GetValue <Object>(), property.type, property.hasMultipleDifferentValues); if (EditorGUI.EndChangeCheck()) { property.SetValue(obj); } }
public override void OnGUI(Rect position, PersistentProperty property, GUIContent label) { var rect = position; rect.height = EditorGUIUtility.singleLineHeight; var propLength = property.Find("Length"); PersistentGUI.BeginShowMixedValue(propLength.hasMultipleDifferentValues); EditorGUI.BeginChangeCheck(); var length = EditorGUI.DelayedIntField(rect, label, propLength.GetValue <int>()); if (EditorGUI.EndChangeCheck()) { length = Mathf.Max(0, length); property.ResizeArray(length); } PersistentGUI.EndShowMixedValue(); rect.y += EditorGUIUtility.singleLineHeight; rect.y += EditorGUIUtility.standardVerticalSpacing; var elType = property.type.GetElementType(); if (property.length == 1 && PersistentGUI.IsDefaultPropertyType(elType)) { EditorGUI.indentLevel += 1; var array = property.GetValue <Array>(); for (int i = 0; i < array.Length; i++) { EditorGUI.BeginChangeCheck(); var itemLabel = new GUIContent("Element " + i); var itemValue = PersistentGUI.DefaultTypeField(rect, itemLabel, array.GetValue(i)); if (EditorGUI.EndChangeCheck()) { var newArray = Array.CreateInstance(elType, array.Length); Array.Copy(array, newArray, array.Length); newArray.SetValue(itemValue, i); property.SetValue(newArray); } rect.y += PersistentGUI.GetDefaultTypeHeight(itemLabel, elType); rect.y += EditorGUIUtility.standardVerticalSpacing; } EditorGUI.indentLevel -= 1; } else { PersistentGUI.BeginColor(Color.yellow); EditorGUI.LabelField(rect, new GUIContent("Array's element is not editable.")); PersistentGUI.EndColor(); } }
public static void FloatSlider(Rect position, GUIContent label, PersistentProperty property, float leftValue, float rightValue) { Debug.Assert(property.type == typeof(float)); var value = property.GetValue <float>(); BeginShowMixedValue(property.hasMultipleDifferentValues); EditorGUI.BeginChangeCheck(); var newValue = EditorGUI.Slider(position, label, value, leftValue, rightValue); if (EditorGUI.EndChangeCheck()) { property.SetValue(newValue); } EndShowMixedValue(); }
public override float GetHeight(PersistentProperty property, GUIContent label) { var height = EditorGUIUtility.singleLineHeight; var elType = property.type.GetElementType(); if (property.length == 1 && PersistentGUI.IsDefaultPropertyType(elType)) { var array = property.GetValue <Array>(); for (int i = 0; i < array.Length; i++) { var itemLabel = new GUIContent("Element " + i); height += EditorGUIUtility.standardVerticalSpacing; height += PersistentGUI.GetDefaultTypeHeight(itemLabel, elType); } } else { height += EditorGUIUtility.singleLineHeight; } return(height); }
public static void DefaultPropertyField(Rect position, GUIContent label, PersistentProperty property) { var propertyType = property.type; if (propertyType == typeof(Vector2)) { Vector2Field(position, label, property); } else if (propertyType == typeof(Vector3)) { Vector3Field(position, label, property); } else if (propertyType == typeof(Vector4)) { Vector4Field(position, label, property); } else if (propertyType == typeof(Rect)) { RectField(position, label, property); } else if (propertyType == typeof(Bounds)) { BoundsField(position, label, property); } else if (propertyType.IsSubclassOf(typeof(Object))) { ObjectField(position, label, property); } else { object updateValue = null; var propertyValue = property.GetValue <object>(); BeginShowMixedValue(property.hasMultipleDifferentValues); EditorGUI.BeginChangeCheck(); if (propertyType == typeof(bool)) { updateValue = EditorGUI.Toggle(position, label, (bool)propertyValue); } else if (propertyType == typeof(int)) { updateValue = EditorGUI.IntField(position, label, (int)propertyValue); } else if (propertyType == typeof(long)) { updateValue = EditorGUI.LongField(position, label, (long)propertyValue); } else if (propertyType == typeof(float)) { updateValue = EditorGUI.FloatField(position, label, (float)propertyValue); } else if (propertyType == typeof(double)) { updateValue = EditorGUI.DoubleField(position, label, (double)propertyValue); } else if (propertyType == typeof(string)) { updateValue = EditorGUI.TextField(position, label, (string)propertyValue); } else if (propertyType == typeof(Color) || propertyType == typeof(Color32)) { updateValue = EditorGUI.ColorField(position, label, (Color)propertyValue); } else if (propertyType == typeof(UnityEngine.Object) || propertyType.IsSubclassOf(typeof(UnityEngine.Object))) { updateValue = EditorGUI.ObjectField(position, label, (UnityEngine.Object)propertyValue, propertyType, false); } else if (propertyType == typeof(AnimationCurve)) { updateValue = EditorGUI.CurveField(position, label, (AnimationCurve)propertyValue); } else if (propertyType.IsEnum) { updateValue = EditorGUI.EnumPopup(position, label, (Enum)propertyValue); } else { throw new NotSupportedException("Invalide default type: " + propertyType.FullName); } if (EditorGUI.EndChangeCheck()) { property.SetValue(updateValue); } EndShowMixedValue(); } }