public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType == SerializedPropertyType.String)
            {
                EditorGUILayout.LabelField(property.displayName);

                EditorGUI.BeginChangeCheck();

                string textAreaValue = EditorGUILayout.TextArea(property.stringValue, GUILayout.MinHeight(EditorGUIUtility.singleLineHeight * 3f));

                if (EditorGUI.EndChangeCheck())
                {
                    property.stringValue = textAreaValue;
                }
            }
            else
            {
                string warning = PropertyUtility.GetAttribute <ResizableTextAreaAttribute>(property).GetType().Name + " can only be used on string fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));

                EditorDrawUtility.DrawPropertyField(property);
            }
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer)
            {
                EditorGUILayout.HelpBox("Field " + property.name + " is not a number", MessageType.Warning);
                return;
            }

            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : String.Format("{0:0.00}", value);

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var   position    = EditorGUILayout.GetControlRect();
            var   maxValue    = progressBarAttribute.MaxValue;
            float lineHight   = EditorGUIUtility.singleLineHeight;
            float padding     = EditorGUIUtility.standardVerticalSpacing;
            var   barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight);

            var fillPercentage = value / maxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;

            var color  = GetColor(progressBarAttribute.Color);
            var color2 = Color.white;

            DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2);
        }
Пример #3
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var attr = PropertyUtility.GetAttribute <PowerSlideAttribute>(property);

            if (attr.BaseRcpLn == 0 | attr.BaseRcpLn == float.NaN)
            {
                EditorDrawUtility.DrawHelpBox($"Invalid Base: {attr.Base}", MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
                return;
            }

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                float value = math.log(property.intValue) * attr.BaseRcpLn;
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PrefixLabel($"{property.displayName}[{property.intValue}]");
                value             = EditorGUILayout.Slider(value, attr.MinPower, attr.MaxPower);
                property.intValue = (int)math.round(math.pow(attr.Base, value));
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                float value = math.log(property.floatValue) * attr.BaseRcpLn;
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PrefixLabel($"{property.displayName}[{property.floatValue}]");
                value = EditorGUILayout.Slider(value, attr.MinPower, attr.MaxPower);
                property.floatValue = math.pow(attr.Base, value);
            }
            else
            {
                string warning = attr.GetType().Name + " can be used only on int or float fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #4
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.isArray)
            {
                var listView = GetListView(property);
                listView.DoLayoutList();
                if (listView.HasKeyboardControl())
                {
                    if (Event.current.type == EventType.KeyDown)
                    {
                        if (Event.current.keyCode == KeyCode.Delete || Event.current.keyCode == KeyCode.KeypadMinus)
                        {
                            if (listView.onCanRemoveCallback(listView))
                            {
                                listView.onRemoveCallback(listView);
                            }
                        }
                        else if (Event.current.keyCode == KeyCode.KeypadPlus)
                        {
                            listView.onAddCallback(listView);
                        }
                    }
                }
            }
            else
            {
                string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));

                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #5
0
        public override void DrawProperty(FieldInfo fieldInfo, SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            Enum targetEnum = (Enum)fieldInfo.GetValue(property.serializedObject.targetObject);
            Enum enumNew    = EditorGUILayout.EnumFlagsField(property.displayName, targetEnum);

            property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
        }
Пример #6
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.isArray)
            {
                if (!this.reorderableListsByPropertyName.ContainsKey(property.name))
                {
                    ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true)
                    {
                        drawHeaderCallback = (Rect rect) =>
                        {
                            EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label);
                        },

                        drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                        {
                            var element = property.GetArrayElementAtIndex(index);
                            rect.y += 2f;

                            var key = "";
                            if (element != null && element.objectReferenceValue != null)
                            {
                                key = element.objectReferenceValue.GetType().GetField("Key").GetValue(element.objectReferenceValue) as string;
                            }

                            using (var scope = new EditorGUILayout.HorizontalScope())
                            {
                                EditorGUI.LabelField(new Rect(rect.x, rect.y, 30, EditorGUIUtility.singleLineHeight), "Key");
                                key = EditorGUI.TextField(new Rect(rect.x + 30, rect.y, rect.width * 0.3f, EditorGUIUtility.singleLineHeight), key);

                                if (element != null && element.objectReferenceValue != null)
                                {
                                    element.objectReferenceValue.GetType().GetField("Key").SetValue(element.objectReferenceValue, key);
                                }

                                EditorGUIUtility.labelWidth = 53;
                                EditorGUI.PropertyField(new Rect(rect.x + 30 + rect.width * 0.35f, rect.y, rect.width * 0.65f - 30, EditorGUIUtility.singleLineHeight), element);
                            }
                        }
                    };

                    this.reorderableListsByPropertyName[property.name] = reorderableList;
                }

                this.reorderableListsByPropertyName[property.name].DoLayoutList();
            }
            else
            {
                string warning = typeof(ReorderableKeyListAttribute).Name + " can be used only on arrays or lists";
                EditorGUILayout.HelpBox(warning, MessageType.Warning);
                Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property));

                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #7
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var attr = PropertyUtility.GetAttribute <PrefabOnlyAttribute>(property);
            var type = this.FieldInfo.FieldType;

            if (typeof(GameObject).IsAssignableFrom(type) || typeof(Component).IsAssignableFrom(type))
            {
                property.objectReferenceValue = EditorGUILayout.ObjectField(property.displayName, property.objectReferenceValue, this.FieldInfo.FieldType, false);
            }
            else
            {
                EditorDrawUtility.DrawHelpBox($"Field={this.FieldInfo.Name} Type={this.FieldInfo.FieldType.Name} {nameof(PrefabOnlyAttribute)} can only be used on GameObject or Component fields", MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #8
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.isArray)
            {
                var key = GetPropertyKeyName(property);

                if (!this.reorderableListsByPropertyName.ContainsKey(key))
                {
                    ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true)
                    {
                        drawHeaderCallback = (Rect rect) =>
                        {
                            EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label);
                        },

                        drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                        {
                            var element = property.GetArrayElementAtIndex(index);
                            rect.y     += 1.0f;
                            rect.x     += 10.0f;
                            rect.width -= 10.0f;

                            EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, 0.0f), element, true);
                        },

                        elementHeightCallback = (int index) =>
                        {
                            return(EditorGUI.GetPropertyHeight(property.GetArrayElementAtIndex(index)) + 4.0f);
                        }
                    };

                    this.reorderableListsByPropertyName[key] = reorderableList;
                }

                this.reorderableListsByPropertyName[key].DoLayoutList();
            }
            else
            {
                string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));

                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #9
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var sliderAttribute = PropertyUtility.GetAttribute <SliderAttribute>(property);

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                EditorGUILayout.IntSlider(property, (int)sliderAttribute.MinValue, (int)sliderAttribute.MaxValue);
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                EditorGUILayout.Slider(property, sliderAttribute.MinValue, sliderAttribute.MaxValue);
            }
            else
            {
                var warning = sliderAttribute.GetType().Name + " can be used only on int or float fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #10
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.isArray)
            {
                var key = GetPropertyKeyName(property);

                if (!reorderableListsByPropertyName.ContainsKey(key))
                {
                    var reorderableList =
                        new ReorderableList(property.serializedObject, property, true, true, true, true)
                    {
                        drawHeaderCallback = rect =>
                        {
                            EditorGUI.LabelField(
                                rect, string.Format("{0}: {1}", property.displayName, property.arraySize),
                                EditorStyles.label);
                        },
                        drawElementCallback = (rect, index, isActive, isFocused) =>
                        {
                            var element = property.GetArrayElementAtIndex(index);
                            rect.y += 2f;

                            EditorGUI.PropertyField(
                                new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element);
                        }
                    };

                    reorderableListsByPropertyName[key] = reorderableList;
                }

                reorderableListsByPropertyName[key].DoLayoutList();
            }
            else
            {
                var warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #11
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var attr   = PropertyUtility.GetAttribute <SceneReferenceOnlyAttribute>(property);
            var type   = this.FieldInfo.FieldType;
            var target = property.serializedObject.targetObject;

            if (target is Component)
            {
                var targetGo = (target as Component).gameObject;
                if (typeof(GameObject).IsAssignableFrom(type))
                {
                    var obj = (GameObject)EditorGUILayout.ObjectField(property.displayName, property.objectReferenceValue, this.FieldInfo.FieldType, true);
                    if (obj == null || obj.scene == targetGo.scene)
                    {
                        property.objectReferenceValue = obj;
                    }
                    else
                    {
                        Debug.LogWarning($"{obj.name} is not in the same scene[{targetGo.scene.name}] with {targetGo.name}", obj);
                    }
                    return;
                }
                else if (typeof(Component).IsAssignableFrom(type))
                {
                    var obj = (Component)EditorGUILayout.ObjectField(property.displayName, property.objectReferenceValue, this.FieldInfo.FieldType, true);
                    if (obj == null || obj.gameObject.scene == targetGo.scene)
                    {
                        property.objectReferenceValue = obj;
                    }
                    else
                    {
                        Debug.LogWarning($"{obj.name} is not in the same scene[{targetGo.scene.name}] with {targetGo.name}", obj);
                    }
                    return;
                }
            }
            EditorDrawUtility.DrawHelpBox($"Field={this.FieldInfo.Name} Type={this.FieldInfo.FieldType.Name} {nameof(SceneReferenceOnlyAttribute)} can only be used on GameObject or Component fields of UnityEngine.Component", MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
            EditorDrawUtility.DrawPropertyField(property);
        }
Пример #12
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var attr   = PropertyUtility.GetAttribute <PrefabOnlyAttribute>(property);
            var type   = this.FieldInfo.FieldType;
            var target = property.serializedObject.targetObject;

            if (target is Component)
            {
                var parent = (target as Component).transform;
                if (typeof(GameObject).IsAssignableFrom(type))
                {
                    var obj = (GameObject)EditorGUILayout.ObjectField(property.displayName, property.objectReferenceValue, this.FieldInfo.FieldType, true);
                    if (obj == null || obj.transform.IsChildOf(parent))
                    {
                        property.objectReferenceValue = obj;
                    }
                    else
                    {
                        Debug.LogWarning($"{obj.name} is not child of {parent.name} thus can not be used", obj);
                    }
                    return;
                }
                else if (typeof(Component).IsAssignableFrom(type))
                {
                    var obj = (Component)EditorGUILayout.ObjectField(property.displayName, property.objectReferenceValue, this.FieldInfo.FieldType, true);
                    if (obj == null || obj.transform.IsChildOf(parent))
                    {
                        property.objectReferenceValue = obj;
                    }
                    else
                    {
                        Debug.LogWarning($"{obj.name} is not child of {parent.name} thus can not be used", obj);
                    }
                    return;
                }
            }
            EditorDrawUtility.DrawHelpBox($"Field={this.FieldInfo.Name} Type={this.FieldInfo.FieldType.Name} {nameof(TranformChildOnlyAttribute)} can only be used on GameObject or Component fields of UnityEngine.Component", MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
            EditorDrawUtility.DrawPropertyField(property);
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            if (property.isArray)
            {
                var targetObject = property.serializedObject.targetObject;
                var propInfo     = targetObject.GetType().GetField(property.name);
                var listObj      = (IList)propInfo.GetValue(targetObject);

                if (!this.reorderableListsByPropertyName.ContainsKey(property.name))
                {
                    ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true)
                    {
                        drawHeaderCallback = (Rect rect) =>
                        {
                            EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label);
                        },
                        drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                        {
                            rect.y += 2f;
                            var   elementObj = listObj[index];
                            var   element    = property.GetArrayElementAtIndex(index);
                            float y          = rect.y;

                            var allElementFileds = elementObj.GetType().GetFields();
                            for (int i = 0; i < allElementFileds.Length; i++)
                            {
                                var elementChildProp = element.FindPropertyRelative(allElementFileds[i].Name);
                                if (elementChildProp == null)
                                {
                                    continue;
                                }
                                EditorGUI.PropertyField(new Rect(rect.x, y, rect.width, EditorGUIUtility.singleLineHeight), elementChildProp);
                                y     += 20;
                                rect.y = y;
                            }
                        }
                    };
                    reorderableList.elementHeightCallback = (int index) =>
                    {
                        var element          = property.GetArrayElementAtIndex(index);
                        var elementObj       = listObj[index];
                        var allElementFileds = elementObj.GetType().GetFields();
                        int y = 0;
                        for (int i = 0; i < allElementFileds.Length; i++)
                        {
                            var elementChildProp = element.FindPropertyRelative(allElementFileds[i].Name);
                            if (elementChildProp == null)
                            {
                                continue;
                            }
                            y += 22;
                        }
                        return(y);
                    };
                    this.reorderableListsByPropertyName[property.name] = reorderableList;
                }
                this.reorderableListsByPropertyName[property.name].DoLayoutList();
            }
            else
            {
                string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
                EditorGUILayout.HelpBox(warning, MessageType.Warning);
                Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #14
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var minMaxSliderAttribute = PropertyUtility.GetAttribute <MinMaxSliderAttribute>(property);

            if (property.propertyType == SerializedPropertyType.Vector2)
            {
                var controlRect     = EditorGUILayout.GetControlRect();
                var labelWidth      = EditorGUIUtility.labelWidth;
                var floatFieldWidth = EditorGUIUtility.fieldWidth;
                var sliderWidth     = controlRect.width - labelWidth - 2f * floatFieldWidth;
                var sliderPadding   = 5f;

                var labelRect = new Rect(
                    controlRect.x,
                    controlRect.y,
                    labelWidth,
                    controlRect.height);

                var sliderRect = new Rect(
                    controlRect.x + labelWidth + floatFieldWidth + sliderPadding,
                    controlRect.y,
                    sliderWidth - 2f * sliderPadding,
                    controlRect.height);

                var minFloatFieldRect = new Rect(
                    controlRect.x + labelWidth,
                    controlRect.y,
                    floatFieldWidth,
                    controlRect.height);

                var maxFloatFieldRect = new Rect(
                    controlRect.x + labelWidth + floatFieldWidth + sliderWidth,
                    controlRect.y,
                    floatFieldWidth,
                    controlRect.height);

                // Draw the label
                EditorGUI.LabelField(labelRect, property.displayName);

                // Draw the slider
                EditorGUI.BeginChangeCheck();
                var sliderValue = property.vector2Value;

                EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue,
                                       minMaxSliderAttribute.MaxValue);

                sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x);

                sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue,
                                            Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y));

                sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);

                sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x),
                                            minMaxSliderAttribute.MaxValue);

                if (EditorGUI.EndChangeCheck())
                {
                    property.vector2Value = sliderValue;
                }
            }
            else
            {
                var warning = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            DropdownAttribute dropdownAttribute = PropertyUtility.GetAttribute <DropdownAttribute>(property);

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            FieldInfo fieldInfo       = ReflectionUtility.GetField(target, property.name);
            FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, dropdownAttribute.ValuesFieldName);

            if (valuesFieldInfo == null)
            {
                this.DrawWarningBox(string.Format("{0} cannot find a values field with name \"{1}\"", dropdownAttribute.GetType().Name, dropdownAttribute.ValuesFieldName));
                EditorGUILayout.PropertyField(property, true);
            }
            else if (valuesFieldInfo.GetValue(target) is IList &&
                     fieldInfo.FieldType == this.GetElementType(valuesFieldInfo))
            {
                // Selected value
                object selectedValue = fieldInfo.GetValue(target);

                // Values and display options
                IList    valuesList     = (IList)valuesFieldInfo.GetValue(target);
                object[] values         = new object[valuesList.Count];
                string[] displayOptions = new string[valuesList.Count];

                for (int i = 0; i < values.Length; i++)
                {
                    object value = valuesList[i];
                    values[i]         = value;
                    displayOptions[i] = value.ToString();
                }

                // Selected value index
                int selectedValueIndex = Array.IndexOf(values, selectedValue);
                if (selectedValueIndex < 0)
                {
                    selectedValueIndex = 0;
                }

                // Draw the dropdown
                this.DrawDropdown(target, fieldInfo, property.displayName, selectedValueIndex, values, displayOptions);
            }
            else if (valuesFieldInfo.GetValue(target) is IDropdownList)
            {
                // Current value
                object selectedValue = fieldInfo.GetValue(target);

                // Current value index, values and display options
                IDropdownList dropdown = (IDropdownList)valuesFieldInfo.GetValue(target);
                IEnumerator <KeyValuePair <string, object> > dropdownEnumerator = dropdown.GetEnumerator();

                int           index = -1;
                int           selectedValueIndex = -1;
                List <object> values             = new List <object>();
                List <string> displayOptions     = new List <string>();

                while (dropdownEnumerator.MoveNext())
                {
                    index++;

                    KeyValuePair <string, object> current = dropdownEnumerator.Current;
                    if (current.Value.Equals(selectedValue))
                    {
                        selectedValueIndex = index;
                    }

                    values.Add(current.Value);
                    displayOptions.Add(current.Key);
                }

                if (selectedValueIndex < 0)
                {
                    selectedValueIndex = 0;
                }

                // Draw the dropdown
                this.DrawDropdown(target, fieldInfo, property.displayName, selectedValueIndex, values.ToArray(), displayOptions.ToArray());
            }
            else
            {
                this.DrawWarningBox(typeof(DropdownAttribute).Name + " works only when the type of the field is equal to the element type of the array");
                EditorGUILayout.PropertyField(property, true);
            }
        }
Пример #16
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.isArray)
            {
                if (!this.reorderableListsByPropertyName.ContainsKey(property.name))
                {
                    ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true)
                    {
                        drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                        {
                            var element = property.GetArrayElementAtIndex(index);
                            rect.y += 2f;
                            EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element);
                        }
                    };

                    reorderableList.drawHeaderCallback = (Rect rect) => {
                        property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, string.Format("{0} [{1}]", property.displayName, property.arraySize));
                        Rect rect2 = rect;
                        if (reorderableList.count == 0)
                        {
                            rect2.height = (rect2.height * 3f);
                        }
                        Event     current = Event.current;
                        EventType type    = current.type;
                        if (type != EventType.DragExited)
                        {
                            if (type == EventType.DragUpdated || type == EventType.DragPerform)
                            {
                                if (rect2.Contains(current.mousePosition))
                                {
                                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                                    if (current.type == EventType.DragPerform)
                                    {
                                        DragAndDrop.AcceptDrag();
                                        Object[] objectReferences = DragAndDrop.objectReferences;
                                        for (int i = 0; i < objectReferences.Length; i++)
                                        {
                                            Object obj = objectReferences[i];
                                            AddElement(property, obj);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            DragAndDrop.PrepareStartDrag();
                        }
//                            EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label);
                    };

                    this.reorderableListsByPropertyName[property.name] = reorderableList;
                }
                if (property.isExpanded)
                {
                    this.reorderableListsByPropertyName[property.name].DoLayoutList();
                }
                else
                {
                    property.isExpanded = (EditorGUILayout.Foldout(property.isExpanded, new GUIContent(string.Format("{0} [{1}]", property.displayName, property.arraySize), property.tooltip)));
                }
            }
            else
            {
                string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));

                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Пример #17
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer)
            {
                EditorGUILayout.HelpBox("Field " + property.name + " is not a number", MessageType.Warning);
                return;
            }

            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : String.Format("{0:0.##}", value);

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var position = EditorGUILayout.GetControlRect();
            var maxValue = progressBarAttribute.MaxValue;

            float lineHight   = EditorGUIUtility.singleLineHeight;
            float padding     = EditorGUIUtility.standardVerticalSpacing;
            var   barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight);

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            // maxValueVar - if found so override maxValue
            var maxValueVar = progressBarAttribute.maxValueVar;

            if (maxValueVar.Trim().Length > 0)
            {
                // try to get field first
                FieldInfo maxValueFieldInfo = ReflectionUtility.GetField(target, maxValueVar);
                if (maxValueFieldInfo != null)
                {
                    if (maxValueFieldInfo.FieldType == typeof(int))
                    {
                        maxValue = (int)maxValueFieldInfo.GetValue(target);
                    }

                    if (maxValueFieldInfo.FieldType == typeof(float))
                    {
                        maxValue = (float)maxValueFieldInfo.GetValue(target);
                    }
                }
                else
                {
                    // if not get the property
                    PropertyInfo maxValuePropertyInfo = ReflectionUtility.GetProperty(target, maxValueVar);
                    if (maxValuePropertyInfo != null)
                    {
                        if (maxValuePropertyInfo.PropertyType == typeof(int))
                        {
                            maxValue = (int)maxValuePropertyInfo.GetValue(target);
                        }

                        if (maxValuePropertyInfo.PropertyType == typeof(float))
                        {
                            maxValue = (float)maxValuePropertyInfo.GetValue(target);
                        }
                    }
                }
            }

            var fillPercentage = value / maxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;

            var color  = GetColor(progressBarAttribute.Color);
            var color2 = Color.white;

            DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2);
        }