Example #1
0
        public static bool DefaultPropertyField(Rect position, RuntimeSerializedProperty property, GUIContent label)
        {
            label = BeginProperty(position, label, property);

            System.Type propertyType = property.PropertyType;

            bool childrenAreExpanded = false;

            // Should we inline? All one-line vars as well as Vector2, Vector3, Rect and Bounds properties are inlined.
            if (!HasVisibleChildFields(property))
            {
                if (propertyType == RuntimeSerializedPropertyType.Bool)
                {
                    EditorGUI.BeginChangeCheck();
                    bool boolValue = EditorGUI.Toggle(position, label, property.BoolValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.BoolValue = boolValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Byte)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.ByteValue);
                    if (intValue >= byte.MaxValue)
                    {
                        intValue = byte.MaxValue;
                    }
                    else if (intValue <= byte.MinValue)
                    {
                        intValue = byte.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ByteValue = (byte)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Char)
                {
                    char[] value = new char[] {
                        property.CharValue
                    };
                    bool changed = GUI.changed;
                    GUI.changed = false;
                    string text = EditorGUI.TextField(position, label, new string(value));
                    if (GUI.changed)
                    {
                        if (text.Length == 1)
                        {
                            property.CharValue = (char)text[0];
                        }
                        else
                        {
                            GUI.changed = false;
                        }
                    }
                    GUI.changed |= changed;
                }
                else if (propertyType == RuntimeSerializedPropertyType.Short)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.ShortValue);
                    if (intValue >= short.MaxValue)
                    {
                        intValue = short.MaxValue;
                    }
                    else if (intValue <= short.MinValue)
                    {
                        intValue = short.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ShortValue = (short)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Integer)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.IntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.IntValue = intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Long)
                {
                    EditorGUI.BeginChangeCheck();
                    long longValue = EditorGUI.LongField(position, label, property.LongValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.LongValue = longValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.sByte)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.sByteValue);
                    if (intValue >= sbyte.MaxValue)
                    {
                        intValue = sbyte.MaxValue;
                    }
                    else if (intValue <= sbyte.MinValue)
                    {
                        intValue = sbyte.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.sByteValue = (sbyte)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.uShort)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.uShortValue);
                    if (intValue >= ushort.MaxValue)
                    {
                        intValue = ushort.MaxValue;
                    }
                    else if (intValue <= ushort.MinValue)
                    {
                        intValue = ushort.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.uShortValue = (ushort)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.uInteger)
                {
                    EditorGUI.BeginChangeCheck();
                    long longValue = EditorGUI.LongField(position, label, property.uIntValue);
                    if (longValue >= uint.MaxValue)
                    {
                        longValue = uint.MaxValue;
                    }
                    else if (longValue <= uint.MinValue)
                    {
                        longValue = uint.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.uIntValue = (uint)longValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.uLong)
                {
                    EditorGUI.BeginChangeCheck();
                    string stringValue = EditorGUI.TextField(position, label, property.uLongValue.ToString());
                    if (EditorGUI.EndChangeCheck())
                    {
                        ulong ulongValue = property.uLongValue;
                        if (ulong.TryParse(stringValue, out ulongValue))
                        {
                            property.uLongValue = ulongValue;
                        }
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Float)
                {
                    EditorGUI.BeginChangeCheck();
                    float floatValue = EditorGUI.FloatField(position, label, property.FloatValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.FloatValue = floatValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Double)
                {
                    EditorGUI.BeginChangeCheck();
                    double doubleValue = EditorGUI.DoubleField(position, label, property.DoubleValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.DoubleValue = doubleValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.String)
                {
                    EditorGUI.BeginChangeCheck();
                    string stringValue = EditorGUI.TextField(position, label, property.StringValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.StringValue = stringValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Color)
                {
                    EditorGUI.BeginChangeCheck();
                    Color colorValue = EditorGUI.ColorField(position, label, property.ColorValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ColorValue = colorValue;
                    }
                }
                else if (propertyType.IsSameOrSubclassOf(RuntimeSerializedPropertyType.Object))
                {
                    GUIStyle style = new GUIStyle();
                    style.richText = true;
                    EditorGUI.LabelField(position, "<color=red>Reference type is not supported!</color>", style);
                }
                else if (propertyType == RuntimeSerializedPropertyType.LayerMask)
                {
                    EditorGUI.BeginChangeCheck();
                    LayerMask layerMaskValue   = property.LayerMaskValue;
                    string[]  displayedOptions = RuntimeSerializedProperty.GetLayerMaskNames(0);
                    int       num = EditorGUI.MaskField(position, label, layerMaskValue.value, displayedOptions);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.LayerMaskValue = num;
                    }
                }
                else if (propertyType.IsEnum)
                {
                    EditorGUI.BeginChangeCheck();
                    System.Enum enumValue = EditorGUI.EnumPopup(position, label, property.EnumValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.EnumValue = enumValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector2)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector2 vector2Value = EditorGUI.Vector2Field(position, label, property.Vector2Value);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector2Value = vector2Value;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector3)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 vector3Value = EditorGUI.Vector3Field(position, label, property.Vector3Value);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector3Value = vector3Value;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector4)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector4 vector4Value = EditorGUI.Vector4Field(position, label, property.Vector4Value);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector4Value = vector4Value;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Rect)
                {
                    EditorGUI.BeginChangeCheck();
                    Rect rectValue = EditorGUI.RectField(position, label, property.RectValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.RectValue = rectValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.ArraySize)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EasyGUI.ArraySizeField(position, label, property.ArraySize, EditorStyles.numberField);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ArraySize = intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.AnimationCurve)
                {
                    EditorGUI.BeginChangeCheck();
                    if (property.AnimationCurveValue == null)
                    {
                        property.AnimationCurveValue = new AnimationCurve(new Keyframe[] {
                            new Keyframe(0, 1),
                            new Keyframe(1, 1)
                        });
                    }
                    AnimationCurve animationCurveValue = EditorGUI.CurveField(position, label, property.AnimationCurveValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.AnimationCurveValue = animationCurveValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Bounds)
                {
                    EditorGUI.BeginChangeCheck();
                    Bounds boundsValue = EditorGUI.BoundsField(position, label, property.BoundsValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.BoundsValue = boundsValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Gradient)
                {
                    EditorGUI.BeginChangeCheck();
                    Gradient gradientValue = EasyGUI.GradientField(label, position, property.GradientValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.GradientValue = gradientValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.FixedBufferSize)
                {
                    EditorGUI.IntField(position, label, property.IntValue);
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector2Int)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector2Int vector2IntValue = EditorGUI.Vector2IntField(position, label, property.Vector2IntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector2IntValue = vector2IntValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector3Int)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3Int vector3IntValue = EditorGUI.Vector3IntField(position, label, property.Vector3IntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector3IntValue = vector3IntValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.RectInt)
                {
                    EditorGUI.BeginChangeCheck();
                    RectInt rectIntValue = EditorGUI.RectIntField(position, label, property.RectIntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.RectIntValue = rectIntValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.BoundsInt)
                {
                    EditorGUI.BeginChangeCheck();
                    BoundsInt boundsIntValue = EditorGUI.BoundsIntField(position, label, property.BoundsIntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.BoundsIntValue = boundsIntValue;
                    }
                }
                else
                {
                    int num = GUIUtility.GetControlID(s_GenericField, FocusType.Keyboard, position);
                    EditorGUI.PrefixLabel(position, num, label);
                }
            }
            // Handle Foldout
            else
            {
                Event tempEvent = new Event(Event.current);

                // Handle the actual foldout first, since that's the one that supports keyboard control.
                // This makes it work more consistent with PrefixLabel.
                childrenAreExpanded = property.IsExpanded;

                bool newChildrenAreExpanded = childrenAreExpanded;
                using (new EditorGUI.DisabledScope(!property.Editable))
                {
                    GUIStyle foldoutStyle = (DragAndDrop.activeControlID != -10) ? EditorStyles.foldout : EditorStyles.foldoutPreDrop;
                    newChildrenAreExpanded = EditorGUI.Foldout(position, childrenAreExpanded, s_PropertyFieldTempContent, true, foldoutStyle);
                }
                if (childrenAreExpanded && property.IsArray && property.ArraySize > property.RuntimeSerializedObject.SerializedObject.maxArraySizeForMultiEditing && property.RuntimeSerializedObject.SerializedObject.isEditingMultipleObjects)
                {
                    Rect boxRect = position;
                    boxRect.xMin += EditorGUIUtility.labelWidth - EasyGUI.Indent;

                    s_ArrayMultiInfoContent.text = s_ArrayMultiInfoContent.tooltip = string.Format(s_ArrayMultiInfoFormatString, property.RuntimeSerializedObject.SerializedObject.maxArraySizeForMultiEditing);
                    EditorGUI.LabelField(boxRect, GUIContent.none, s_ArrayMultiInfoContent, EditorStyles.helpBox);
                }

                if (newChildrenAreExpanded != childrenAreExpanded)
                {
                    // Recursive set expanded
                    if (Event.current.alt)
                    {
                        SetExpandedRecurse(property, newChildrenAreExpanded);
                    }
                    // Expand one element only
                    else
                    {
                        property.IsExpanded = newChildrenAreExpanded;
                    }
                }
                childrenAreExpanded = newChildrenAreExpanded;


                // Check for drag & drop events here, to add objects to an array by dragging to the foldout.
                // The event may have already been used by the Foldout control above, but we want to also use it here,
                // so we use the event copy we made prior to calling the Foldout method.

                // We need to use last s_LastControlID here to ensure we do not break duplicate functionality (fix for case 598389)
                // If we called GetControlID here s_LastControlID would be incremented and would not longer be in sync with GUIUtililty.keyboardFocus that
                // is used for duplicating (See DoPropertyFieldKeyboardHandling)
                int id = EditorGUIUtilityHelper.s_LastControlID;
                switch (tempEvent.type)
                {
                case EventType.DragExited:
                    if (GUI.enabled)
                    {
                        HandleUtility.Repaint();
                    }

                    break;

                case EventType.DragUpdated:
                case EventType.DragPerform:

                    if (position.Contains(tempEvent.mousePosition) && GUI.enabled)
                    {
                        Object[] references = DragAndDrop.objectReferences;

                        // Check each single object, so we can add multiple objects in a single drag.
                        Object[] oArray        = new Object[1];
                        bool     didAcceptDrag = false;
                        foreach (Object o in references)
                        {
                            oArray[0] = o;
                            Object validatedObject = ValidateObjectFieldAssignment(oArray, null, property, EasyGUI.ObjectFieldValidatorOptions.None);
                            if (validatedObject != null)
                            {
                                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                                if (tempEvent.type == EventType.DragPerform)
                                {
                                    property.AppendFoldoutPPtrValue(validatedObject);
                                    didAcceptDrag = true;
                                    DragAndDrop.activeControlID = 0;
                                }
                                else
                                {
                                    DragAndDrop.activeControlID = id;
                                }
                            }
                        }
                        if (didAcceptDrag)
                        {
                            GUI.changed = true;
                            DragAndDrop.AcceptDrag();
                        }
                    }
                    break;
                }
            }

            EndProperty();

            return(childrenAreExpanded);
        }