コード例 #1
0
        public override void OnGUI(Rect position, InspectableProperty property, GUIContent label)
        {
            var range       = Attribute as MinMaxRangeAttribute;
            var minProperty = property.FindPropertyRelative(Min);
            var maxProperty = property.FindPropertyRelative(Max);

            if (minProperty != null && maxProperty != null)
            {
                minValue = GetValue(minProperty);
                maxValue = GetValue(maxProperty);

                Rect contentPosition = EditorGUI.PrefixLabel(position, label);
                EditorGUI.BeginChangeCheck();

                int indentLevel = EditorGUI.indentLevel;
                EditorGUI.indentLevel = 0;
                if (range.Min == float.MinValue || range.Max == float.MaxValue)
                {
                    var tempPosition = new Rect(contentPosition);
                    tempPosition.xMax = tempPosition.xMin + 0.2f * contentPosition.width;
                    EditorGUI.LabelField(tempPosition, Min);
                    tempPosition.xMin  = tempPosition.xMax;
                    tempPosition.xMax += 0.3f * contentPosition.width;
                    minValue           = EditorGUI.FloatField(tempPosition, minValue);
                    tempPosition.xMin  = tempPosition.xMax;
                    tempPosition.xMax += 0.2f * contentPosition.width;
                    EditorGUI.LabelField(tempPosition, Max);
                    tempPosition.xMin  = tempPosition.xMax;
                    tempPosition.xMax += 0.3f * contentPosition.width;
                    maxValue           = EditorGUI.FloatField(tempPosition, maxValue);
                }
                else
                {
                    var tempPosition = new Rect(contentPosition);
                    tempPosition.xMax  = tempPosition.xMin + 0.2f * contentPosition.width;
                    minValue           = EditorGUI.FloatField(tempPosition, minValue);
                    tempPosition.xMin  = tempPosition.xMax;
                    tempPosition.xMax += 0.6f * contentPosition.width;
                    EditorGUI.MinMaxSlider(tempPosition, ref minValue, ref maxValue, range.Min, range.Max);
                    tempPosition.xMin  = tempPosition.xMax;
                    tempPosition.xMax += 0.2f * contentPosition.width;
                    maxValue           = EditorGUI.FloatField(tempPosition, maxValue);
                }
                EditorGUI.indentLevel = indentLevel;

                if (EditorGUI.EndChangeCheck())
                {
                    SetValue(minProperty, minValue);
                    SetValue(maxProperty, maxValue);
                }
            }
        }
コード例 #2
0
        public InspectableProperty FindPropertyRelative(string relativePropertyPath)
        {
            InspectableProperty property = null;

            if (!string.IsNullOrEmpty(relativePropertyPath))
            {
                string[] paths = relativePropertyPath.Split(StopChar);
                if (paths.Length > 0 && propertyBase.Properties != null)
                {
                    foreach (var child in propertyBase.Properties)
                    {
                        if (child.Name == paths[0])
                        {
                            property = new InspectableProperty(child);
                            if (paths.Length == 1)
                            {
                                return(property);
                            }
                            else
                            {
                                int    startIndex = paths[0].Length + 1;
                                string path       = relativePropertyPath.Substring(startIndex, relativePropertyPath.Length - startIndex);
                                property = property.FindPropertyRelative(path);
                                if (property != null)
                                {
                                    return(property);
                                }
                            }
                        }
                    }
                }
            }
            return(property);
        }
コード例 #3
0
        public override float GetPropertyHeight(InspectableProperty property, GUIContent label)
        {
            var attr      = Attribute as InspectorDisplayAttribute;
            var fieldName = (attr == null) ? "value" : attr.FieldName;

            var height        = base.GetPropertyHeight(property, label);
            var valueProperty = property.FindPropertyRelative(fieldName);

            if (valueProperty == null)
            {
                return(height);
            }

            if (valueProperty.PropertyType == InspectablePropertyType.Rect || valueProperty.PropertyType == InspectablePropertyType.RectInt)
            {
                return(height * 2);
            }
            if (valueProperty.PropertyType == InspectablePropertyType.Bounds || valueProperty.PropertyType == InspectablePropertyType.BoundsInt)
            {
                return(height * 3);
            }
            if (valueProperty.PropertyType == InspectablePropertyType.String)
            {
                var multilineAttr = GetMultilineAttribute();
                if (multilineAttr != null)
                {
                    return(((!EditorGUIUtility.wideMode) ? 16f : 0f) + 16f + (float)((multilineAttr.Lines - 1) * 13));
                }
                ;
            }

            if (valueProperty.IsExpanded)
            {
                var count = 0;
                var e     = valueProperty.GetEnumerator();
                while (e.MoveNext())
                {
                    count++;
                }
                return(((height + 4) * count) + 6); // (Line = 20 + Padding) ?
            }

            return(height);
        }
コード例 #4
0
        public override void OnGUI(Rect position, InspectableProperty property, GUIContent label)
        {
            string fieldName;
            bool   notifyPropertyChanged;

            {
                var attr = Attribute as InspectorDisplayAttribute;
                fieldName             = (attr == null) ? "value" : attr.FieldName;
                notifyPropertyChanged = (attr == null) ? true : attr.NotifyPropertyChanged;
            }

            if (notifyPropertyChanged)
            {
                EditorGUI.BeginChangeCheck();
            }
            var targetInspectableProperty = property.FindPropertyRelative(fieldName);

            if (targetInspectableProperty == null)
            {
                EditorGUI.LabelField(position, label, new GUIContent()
                {
                    text = "InspectorDisplay can't find target:" + fieldName
                });
                if (notifyPropertyChanged)
                {
                    EditorGUI.EndChangeCheck();
                }
                return;
            }
            else
            {
                EmitPropertyField(position, targetInspectableProperty, label);
            }

            if (notifyPropertyChanged)
            {
                if (EditorGUI.EndChangeCheck())
                {
                    property.InspectableObject.SerializedObject.ApplyModifiedProperties(); // deserialize to field

                    var paths             = property.PropertyPath.Split('.');              // X.Y.Z...
                    var attachedComponent = property.InspectableObject.Object;

                    var targetProp = (paths.Length == 1) ? FieldInfo.GetValue(attachedComponent) : GetValueRecursive(attachedComponent, 0, paths);
                    if (targetProp == null)
                    {
                        return;
                    }
                    var propInfo      = targetProp.GetType().GetProperty(fieldName, BindingFlags.IgnoreCase | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    var modifiedValue = propInfo.GetValue(targetProp, null); // retrieve new value

                    var methodInfo = targetProp.GetType().GetMethod("SetValueAndForceNotify", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(targetProp, new object[] { modifiedValue });
                    }
                }
                else
                {
                    property.InspectableObject.SerializedObject.ApplyModifiedProperties();
                }
            }
        }