Beispiel #1
0
    public void DelayedDoubleField()
    {
        EditorGUI.BeginChangeCheck();
        double num = EditorGUI.DelayedDoubleField(rect, guiContent.text, serializedProperty.doubleValue);

        if (EditorGUI.EndChangeCheck())
        {
            serializedProperty.doubleValue = num;
        }
    }
    //private methods--------------------------------------------------------------
    private void InitializeBasicDrawer()
    {
        _fieldDrawer = new Dictionary <string, Func <Rect, string, object, object> >();


        #region int

        //int field-----------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(int).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is int))
            {
                value = 0;
            }
            return(EditorGUI.IntField(pos, fieldName, (int)value));
        });

        //int delayed field---------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(int).Name + DelayedFieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is int))
            {
                value = 0;
            }
            return(EditorGUI.DelayedIntField(pos, fieldName, (int)value));
        });

        //int slider field----------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(int).Name + SliderName,
                         (pos, fieldName, value) =>
        {
            if (!(value is int))
            {
                value = 0;
            }
            return((int)EditorGUI.Slider(pos, new GUIContent(fieldName), (int)value, (int)_sliderMinVal, (int)_sliderMaxVal));
        });


        #endregion

        #region float

        //float field---------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(float).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is float))
            {
                value = 0f;
            }
            return(EditorGUI.FloatField(pos, fieldName, (float)value));
        });

        //float delayed field-------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(float).Name + DelayedFieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is float))
            {
                value = 0f;
            }
            return(EditorGUI.DelayedFloatField(pos, fieldName, (float)value));
        });

        //float slider field--------- -----------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(float).Name + SliderName,
                         (pos, fieldName, value) =>
        {
            if (!(value is float))
            {
                value = 0f;
            }
            return(EditorGUI.Slider(pos, new GUIContent(fieldName), (float)value, (float)_sliderMinVal, (float)_sliderMaxVal));
        });


        #endregion

        #region double

        //double field--------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(double).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is double))
            {
                value = 0d;
            }
            return(EditorGUI.DoubleField(pos, fieldName, (double)value));
        });

        //double delayed field------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(double).Name + DelayedFieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is double))
            {
                value = 0d;
            }
            return(EditorGUI.DelayedDoubleField(pos, fieldName, (double)value));
        });


        #endregion

        #region long

        //long field---------------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(long).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is long))
            {
                value = 0L;
            }
            return(EditorGUI.LongField(pos, fieldName, (long)value));
        });



        #endregion

        #region string

        //string field--------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(string).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is string))
            {
                value = "";
            }
            return(EditorGUI.TextField(pos, fieldName, (string)value));
        });

        //string field--------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(string).Name + TextAreaName,
                         (pos, fieldName, value) =>
        {
            if (!(value is string))
            {
                value = "";
            }
            Rect temp   = pos;
            temp.height = EntryComponent.SingleLineHeight;
            EditorGUI.LabelField(temp, fieldName);
            temp.y     += temp.height;
            temp.height = pos.height - temp.height;
            return(EditorGUI.TextArea(temp, (string)value, EditorStyles.textArea));
        });

        //string password field-----------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(string).Name + PasswordName,
                         (pos, fieldName, value) =>
        {
            if (!(value is string))
            {
                value = "";
            }
            return(EditorGUI.PasswordField(pos, fieldName, (string)value));
        });



        #endregion

        #region char

        //char field----------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(char).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is char))
            {
                value = ' ';
            }
            return(EditorGUI.TextField(pos, fieldName, ((char)(value)).ToString())[0]);
        });

        #endregion

        #region vectors

        //vector2 field----------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(Vector2).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is Vector2))
            {
                value = Vector2.zero;
            }
            return(EditorGUI.Vector2Field(pos, new GUIContent(fieldName), (Vector2)value));
        });

        //vector2 Int field---------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(Vector2Int).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is Vector2Int))
            {
                value = Vector2Int.zero;
            }
            return(EditorGUI.Vector2IntField(pos, fieldName, (Vector2Int)value));
        });

        //vector3 field-------------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(Vector3).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is Vector3))
            {
                value = Vector3.zero;
            }
            return(EditorGUI.Vector3Field(pos, fieldName, (Vector3)value));
        });

        //vector3 Int field---------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(Vector3Int).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is Vector3Int))
            {
                value = Vector3Int.zero;
            }
            return(EditorGUI.Vector3IntField(pos, fieldName, (Vector3Int)value));
        });

        //vector4 field-------------------------------------------------------------------------------------------------
        _fieldDrawer.Add(typeof(Vector4).Name + FieldName,
                         (pos, fieldName, value) =>
        {
            if (!(value is Vector4))
            {
                value = Vector4.zero;
            }
            return(EditorGUI.Vector4Field(pos, fieldName, (Vector4)value));
        });


        #endregion
    }
        // I've seen a lot of ugly methods in Unity source code, but this is just.. OMG
        // The method is identical to the original, only non-delayed fields are replaced with their delayed versions where possible.
        // For SerializedPropertyType.Integer, there is also a ternary expression instead of a single LongField because a version of DelayedLongField doesn't exist.
        public static bool DefaultPropertyFieldDelayed(Rect position, SerializedProperty property, GUIContent label)
        {
            label = EditorGUI.BeginPropertyInternal(position, label, property);

            SerializedPropertyType type = property.propertyType;

            bool childrenAreExpanded = false;

            // Should we inline? All one-line vars as well as Vector2, Vector3, Rect and Bounds properties are inlined.
            if (!EditorGUI.HasVisibleChildFields(property))
            {
                switch (type)
                {
                case SerializedPropertyType.Integer:
                {
                    EditorGUI.BeginChangeCheck();

                    long newValue = property.longValue > Int32.MaxValue
                            ? EditorGUI.LongField(position, label, property.longValue)
                            : EditorGUI.DelayedIntField(position, label, property.intValue);

                    if (EditorGUI.EndChangeCheck())
                    {
                        property.longValue = newValue;
                    }
                    break;
                }

                case SerializedPropertyType.Float:
                {
                    EditorGUI.BeginChangeCheck();

                    // Necessary to check for float type to get correct string formatting for float and double.
                    bool   isFloat  = property.type == "float";
                    double newValue = isFloat ? EditorGUI.DelayedFloatField(position, label, property.floatValue) :
                                      EditorGUI.DelayedDoubleField(position, label, property.doubleValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.doubleValue = newValue;
                    }
                    break;
                }

                case SerializedPropertyType.String:
                {
                    EditorGUI.BeginChangeCheck();
                    string newValue = EditorGUI.DelayedTextField(position, label, property.stringValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.stringValue = newValue;
                    }
                    break;
                }

                case SerializedPropertyType.Boolean:
                {
                    EditorGUI.BeginChangeCheck();
                    bool newValue = EditorGUI.Toggle(position, label, property.boolValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.boolValue = newValue;
                    }
                    break;
                }

                case SerializedPropertyType.Color:
                {
                    EditorGUI.BeginChangeCheck();
                    Color newColor = EditorGUI.ColorField(position, label, property.colorValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.colorValue = newColor;
                    }
                    break;
                }

                case SerializedPropertyType.ArraySize:
                {
                    EditorGUI.BeginChangeCheck();
                    int newValue = EditorGUI.ArraySizeField(position, label, property.intValue, EditorStyles.numberField);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.intValue = newValue;
                    }
                    break;
                }

                case SerializedPropertyType.FixedBufferSize:
                {
                    EditorGUI.DelayedIntField(position, label, property.intValue);
                    break;
                }

                case SerializedPropertyType.Enum:
                {
                    EditorGUI.EnumPopup(position, property, label);
                    break;
                }

                case SerializedPropertyType.ObjectReference:
                {
                    EditorGUI.ObjectFieldInternal(position, property, null, label, EditorStyles.objectField);
                    break;
                }

                case SerializedPropertyType.LayerMask:
                {
                    EditorGUI.LayerMaskField(position, property, label);
                    break;
                }

                case SerializedPropertyType.Character:
                {
                    char[] value = { (char)property.intValue };

                    bool wasChanged = GUI.changed;
                    GUI.changed = false;
                    string newValue = EditorGUI.DelayedTextField(position, label, new string(value));
                    if (GUI.changed)
                    {
                        if (newValue.Length == 1)
                        {
                            property.intValue = newValue[0];
                        }
                        // Value didn't get changed after all
                        else
                        {
                            GUI.changed = false;
                        }
                    }
                    GUI.changed |= wasChanged;
                    break;
                }

                case SerializedPropertyType.AnimationCurve:
                {
                    int id = GUIUtility.GetControlID(EditorGUI.s_CurveHash, FocusType.Keyboard, position);
                    EditorGUI.DoCurveField(EditorGUI.PrefixLabel(position, id, label), id, null, EditorGUI.kCurveColor, new Rect(), property);
                    break;
                }

                case SerializedPropertyType.Gradient:
                {
                    int id = GUIUtility.GetControlID(EditorGUI.s_CurveHash, FocusType.Keyboard, position);
                    EditorGUI.DoGradientField(EditorGUI.PrefixLabel(position, id, label), id, null, property, false, ColorSpace.Gamma);
                    break;
                }

                case SerializedPropertyType.Vector3:
                {
                    EditorGUI.Vector3Field(position, property, label);
                    break;
                }

                case SerializedPropertyType.Vector4:
                {
                    EditorGUI.Vector4Field(position, property, label);
                    break;
                }

                case SerializedPropertyType.Vector2:
                {
                    EditorGUI.Vector2Field(position, property, label);
                    break;
                }

                case SerializedPropertyType.Vector2Int:
                {
                    EditorGUI.Vector2IntField(position, property, label);
                    break;
                }

                case SerializedPropertyType.Vector3Int:
                {
                    EditorGUI.Vector3IntField(position, property, label);
                    break;
                }

                case SerializedPropertyType.Rect:
                {
                    EditorGUI.RectField(position, property, label);
                    break;
                }

                case SerializedPropertyType.RectInt:
                {
                    EditorGUI.RectIntField(position, property, label);
                    break;
                }

                case SerializedPropertyType.Bounds:
                {
                    EditorGUI.BoundsField(position, property, label);
                    break;
                }

                case SerializedPropertyType.BoundsInt:
                {
                    EditorGUI.BoundsIntField(position, property, label);
                    break;
                }

                default:
                {
                    int genericID = GUIUtility.GetControlID(EditorGUI.s_GenericField, FocusType.Keyboard, position);
                    EditorGUI.PrefixLabel(position, genericID, label);
                    break;
                }
                }
            }
            // 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.foldoutPreDrop : EditorStyles.foldout;
                    newChildrenAreExpanded = EditorGUI.Foldout(position, childrenAreExpanded, EditorGUI.s_PropertyFieldTempContent, true, foldoutStyle);
                }


                if (childrenAreExpanded && property.isArray && property.arraySize > property.serializedObject.maxArraySizeForMultiEditing && property.serializedObject.isEditingMultipleObjects)
                {
                    Rect boxRect = position;
                    boxRect.xMin += EditorGUIUtility.labelWidth - EditorGUI.indent;

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

                if (newChildrenAreExpanded != childrenAreExpanded)
                {
                    // Recursive set expanded
                    if (Event.current.alt)
                    {
                        EditorGUI.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 = EditorGUIUtility.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 = EditorGUI.ValidateObjectFieldAssignment(oArray, null, property, EditorGUI.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;
                }
            }

            EditorGUI.EndProperty();

            return(childrenAreExpanded);
        }
        private void _drawValue(ref Rect rect, ValueS valueS)
        {
            rect.size = new Vector2(rect.width - (70 + 30 + 10 + 30) // subtract EditValue and Refresh and Remove Button
                                    , rect.height);

            //todo draw Value
            if (valueS.IsUnity)
            {
                var offset = new Vector2(15, 0);
                rect.position -= offset;         //subtract offset

                rect.size += new Vector2(70, 0); // no Edit Value Button
                Object obj;
                EditorGUI.BeginChangeCheck();
                {
                    obj = EditorGUI.ObjectField(rect, valueS.UValue, valueS.ValueType, false);
                }
                if (EditorGUI.EndChangeCheck())
                {
                    valueS.SetValue(obj);
                    Save();
                }
            }
            else
            {
                //todo non Unity Type

                var value = valueS.GetValue();

                if (value == null)
                {
                    valueS.SetValue(Activator.CreateInstance(valueS.ValueType));
                    Save();
                }

                _drawNonUnityValue <int>(ref rect, valueS, (pos, x) => EditorGUI.DelayedIntField(pos, (int)x.GetValue()));
                _drawNonUnityValue <float>(ref rect, valueS, (pos, x) => EditorGUI.DelayedFloatField(pos, (float)x.GetValue()));
                _drawNonUnityValue <double>(ref rect, valueS, (pos, x) => EditorGUI.DelayedDoubleField(pos, (double)x.GetValue()));
                _drawNonUnityValue <long>(ref rect, valueS, (pos, x) => EditorGUI.LongField(pos, (long)x.GetValue()));
                _drawNonUnityValue <string>(ref rect, valueS, (pos, x) => EditorGUI.DelayedTextField(pos, (string)x.GetValue()));
                _drawNonUnityValue <Vector2>(ref rect, valueS, (pos, x) => EditorGUI.Vector2Field(pos, "", (Vector2)x.GetValue()));
                _drawNonUnityValue <Vector2Int>(ref rect, valueS, (pos, x) => EditorGUI.Vector2IntField(pos, "", (Vector2Int)x.GetValue()));
                _drawNonUnityValue <Vector3>(ref rect, valueS, (pos, x) => EditorGUI.Vector3Field(pos, "", (Vector3)x.GetValue()));
                _drawNonUnityValue <Vector3Int>(ref rect, valueS, (pos, x) => EditorGUI.Vector3IntField(pos, "", (Vector3Int)x.GetValue()));
                _drawNonUnityValue <Vector4>(ref rect, valueS, (pos, x) => EditorGUI.Vector4Field(pos, "", (Vector4)x.GetValue()));
                _drawNonUnityValue <Color>(ref rect, valueS, (pos, x) => EditorGUI.ColorField(pos, (Color)x.GetValue()));
                _drawNonUnityValue <AnimationCurve>(ref rect, valueS, (pos, x) => EditorGUI.CurveField(pos, (AnimationCurve)x.GetValue()));
                _drawNonUnityValue <Bounds>(ref rect, valueS, (pos, x) => EditorGUI.BoundsField(pos, (Bounds)x.GetValue()));
                _drawNonUnityValue <BoundsInt>(ref rect, valueS, (pos, x) => EditorGUI.BoundsIntField(pos, (BoundsInt)x.GetValue()));
                _drawNonUnityValue <Rect>(ref rect, valueS, (pos, x) => EditorGUI.RectField(pos, (Rect)x.GetValue()));
                _drawNonUnityValue <RectInt>(ref rect, valueS, (pos, x) => EditorGUI.RectIntField(pos, (RectInt)x.GetValue()));
                _drawNonUnityValue <Enum>(ref rect, valueS, (pos, x) => EditorGUI.EnumFlagsField(pos, (Enum)x.GetValue()));
                _drawNonUnityValue <Gradient>(ref rect, valueS, (pos, x) => EditorGUI.GradientField(pos, (Gradient)x.GetValue()));

                rect.size = new Vector2(70, 26);

                if (GUI.Button(rect, "Edit Value"))
                {
                    _valueEditPopup.ValueS = valueS;

                    var pos = rect;
                    PopupWindow.Show(pos, _valueEditPopup);
                }
            }
            rect.position += new Vector2(rect.size.x + 10, 0);
        }
Beispiel #5
0
 private static eDouble DelayedDoubleFieldMaster(Rect rect, GUICon guiCon, double value, GUIStyle style) =>
 new eDouble
 {
     Event = new Event(Event.current), Rect = rect, Value = EditorGUI.DelayedDoubleField(rect, guiCon, value, style)
 };