Example #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            drawPrefixLabel = false;

            Begin(position, property, label);

            float min = ((SliderAttribute)attribute).min;
            float max = ((SliderAttribute)attribute).max;

            EditorGUI.BeginChangeCheck();

            currentPosition.height = 16;
            object value = property.GetValue();

            if (value is int) {
                property.SetValue(EditorGUI.IntSlider(currentPosition, label, (int)value, (int)min, (int)max));
            }
            else if (value is float) {
                property.SetValue(EditorGUI.Slider(currentPosition, label, (float)value, min, max));
            }
            else if (value is double) {
                property.SetValue(EditorGUI.Slider(currentPosition, label, (float)(double)value, min, max));
            }
            else {
                EditorGUI.HelpBox(currentPosition, "The type of the field must be numerical.", MessageType.Error);
            }

            if (EditorGUI.EndChangeCheck()) {
                property.Clamp(min, max);
            }

            End();
        }
Example #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Begin(position, property, label);

            currentPosition = EditorGUI.PrefixLabel(currentPosition, label);
            currentPosition.x -= 1f;

            BeginIndent(0);
            BeginLabelWidth(27f);

            if (isEditingMin && !EditorGUIUtility.editingTextField)
            {
                property.SetValue("max", Mathf.Max(property.GetValue<float>("max"), property.GetValue<float>("min")));
                isEditingMin = false;
            }
            else if (isEditingMax && !EditorGUIUtility.editingTextField)
            {
                property.SetValue("min", Mathf.Min(property.GetValue<float>("min"), property.GetValue<float>("max")));
                isEditingMax = false;
            }

            EditorGUI.BeginChangeCheck();

            currentPosition.width = currentPosition.width / 2f;
            EditorGUI.PropertyField(currentPosition, property.FindPropertyRelative("min"));

            if (EditorGUI.EndChangeCheck())
            {
                if (EditorGUIUtility.editingTextField)
                    isEditingMin = true;
                else
                    property.SetValue("max", Mathf.Max(property.GetValue<float>("max"), property.GetValue<float>("min")));
            }

            EditorGUI.BeginChangeCheck();

            currentPosition.x += currentPosition.width + 1f;
            EditorGUI.PropertyField(currentPosition, property.FindPropertyRelative("max"));

            if (EditorGUI.EndChangeCheck())
            {
                if (EditorGUIUtility.editingTextField)
                    isEditingMax = true;
                else
                    property.SetValue("min", Mathf.Min(property.GetValue<float>("min"), property.GetValue<float>("max")));
            }

            EndLabelWidth();
            EndIndent();

            End();
        }
Example #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            drawPrefixLabel = false;

            Begin(position, property, label);

            property.SetValue(EditorGUI.LayerField(currentPosition, label, property.GetValue<int>()));

            End();
        }
		public void ToggleButton(SerializedProperty boolProperty, GUIContent trueLabel, GUIContent falseLabel) {
			Rect indentedPosition = EditorGUI.IndentedRect(currentPosition);
			boolProperty.SetValue(EditorGUI.Toggle(indentedPosition, boolProperty.GetValue<bool>(), new GUIStyle("button")));
			
			if (boolProperty.GetValue<bool>()) {
				Rect labelPosition = new Rect(indentedPosition.x + indentedPosition.width / 2 - trueLabel.text.GetWidth(EditorStyles.standardFont) / 2 - 16, indentedPosition.y, indentedPosition.width, indentedPosition.height);
				EditorGUI.LabelField(labelPosition, trueLabel);
			}
			else {
				Rect labelPosition = new Rect(indentedPosition.x + indentedPosition.width / 2 - falseLabel.text.GetWidth(EditorStyles.standardFont) / 2 - 16, indentedPosition.y, indentedPosition.width, indentedPosition.height);
				EditorGUI.LabelField(labelPosition, falseLabel);
			}
			
			currentPosition.y += currentPosition.height + 2;
		}
Example #5
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            drawPrefixLabel = false;

            Begin(position, property, label);

            string arrayName = ((PopupAttribute)attribute).arrayName;
            string onChangeCallback = ((PopupAttribute)attribute).onChangeCallback;
            SerializedProperty array = property.serializedObject.FindProperty(arrayName);
            int selectedIndex = 0;

            List<string> displayedOptions = new List<string>();
            if (array != null && property.GetValue() != null) {
                for (int i = 0; i < array.arraySize; i++) {
                    object value = array.GetArrayElementAtIndex(i).GetValue();

                    if (property.GetValue().Equals(value)) {
                        selectedIndex = i;
                    }

                    if (value != null) {
                        if (value as Object != null) {
                            displayedOptions.Add(string.Format("{0} [{1}]", value.GetType().Name, i));
                        }
                        else {
                            displayedOptions.Add(string.Format("{0}", value));
                        }
                    }
                    else {
                        displayedOptions.Add(" ");
                    }
                }
            }

            EditorGUI.BeginChangeCheck();
            selectedIndex = Mathf.Clamp(EditorGUI.Popup(_currentPosition, label, selectedIndex, displayedOptions.ToGUIContents()), 0, array.arraySize - 1);

            if (array != null && array.arraySize != 0 && array.arraySize > selectedIndex) {
                property.SetValue(array.GetArrayElementAtIndex(selectedIndex).GetValue());
            }

            if (EditorGUI.EndChangeCheck()) {
                if (!string.IsNullOrEmpty(onChangeCallback)) ((MonoBehaviour)property.serializedObject.targetObject).Invoke(onChangeCallback, 0);
            }

            End();
        }
Example #6
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            drawPrefixLabel = false;

            Begin(position, property, label);

            EditorGUI.BeginChangeCheck();

            Array enumValues = Enum.GetValues(fieldInfo.FieldType);
            int value = (int)enumValues.GetValue(property.GetValue<int>());
            string[] options = GetDisplayOptions();
            value = EditorGUI.MaskField(currentPosition, label, value, options);

            if (EditorGUI.EndChangeCheck()) {
                object enumValue = value == -1 ? Array.IndexOf(enumValues, Enum.ToObject(fieldInfo.FieldType, SumOptions(options))) : Array.IndexOf(enumValues, Enum.ToObject(fieldInfo.FieldType, value));
                property.SetValue(enumValue);
            }

            End();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            drawPrefixLabel = false;

            Begin(position, property, label);

            var types = ((TypePopupAttribute)attribute).Types;
            var typeNames = types.Convert(type => type.Name);
            var typeName = property.GetValue<string>();
            var typeIndex = Array.IndexOf(types, Type.GetType(typeName));

            EditorGUI.BeginChangeCheck();
            BeginIndent(0);

            typeIndex = EditorGUI.Popup(currentPosition, typeIndex, typeNames);

            EndIndent();
            if (EditorGUI.EndChangeCheck())
                property.SetValue(types[typeIndex].AssemblyQualifiedName);

            End();
        }
Example #8
0
        void ShowPolar2D(SerializedProperty property)
        {
            var xProperty = property.FindPropertyRelative("x");
            var yProperty = property.FindPropertyRelative("y");
            var vector = property.GetValue<Vector2>().ToPolar().Round(0.0001f);
            currentPosition.width = currentPosition.width / 2f - 1f;

            EditorGUI.BeginChangeCheck();
            EditorGUI.BeginProperty(currentPosition, xProperty.ToGUIContent(), xProperty);

            vector.x = EditorGUI.FloatField(currentPosition, "R", vector.x);
            currentPosition.x += currentPosition.width + 2f;

            EditorGUI.EndProperty();

            EditorGUI.BeginProperty(currentPosition, yProperty.ToGUIContent(), yProperty);

            vector.y = EditorGUI.FloatField(currentPosition, "ϴ", vector.y);

            EditorGUI.EndProperty();
            if (EditorGUI.EndChangeCheck())
                property.SetValue(vector.ToCartesian());
        }
Example #9
0
        void OnEnumFlagSelected(FlagsOption option, SerializedProperty property)
        {
            var flags = property.GetValue<IEnumFlag>();

            switch (option.Type)
            {
                case FlagsOption.OptionTypes.Everything:
                    foreach (IEnumFlag value in enumValues)
                        flags = flags.Add(value);
                    break;
                case FlagsOption.OptionTypes.Nothing:
                    foreach (IEnumFlag value in enumValues)
                        flags = flags.Remove(value);
                    break;
                case FlagsOption.OptionTypes.Custom:
                    if (option.IsSelected)
                        flags = flags.Remove((IEnumFlag)option.Value);
                    else
                        flags = flags.Add((IEnumFlag)option.Value);
                    break;
            }

            property.SetValue(flags);
        }
        void ShowCallbacks()
        {
            callbacksProperty = serializedObject.FindProperty("callbacks");

            EditorGUI.BeginDisabledGroup(Application.isPlaying);
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.PrefixLabel("Callbacks");
            int callerMask = EditorGUILayout.MaskField(callbacksProperty.GetValue<int>(), callbacks);
            callbacksProperty.SetValue(callerMask);

            EditorGUILayout.EndHorizontal();
            EditorGUI.EndDisabledGroup();

            List<StateMachineCaller> callers = new List<StateMachineCaller>();

            for (int i = 0; i < callbackTypes.Length; i++) {
                if ((callerMask & 1 << i) != 0) {
                    StateMachineCaller caller = stateMachine.GetOrAddComponent(callbackTypes[i]) as StateMachineCaller;

                    caller.machine = stateMachine;
                    caller.hideFlags = HideFlags.HideInInspector;
                    callers.Add(caller);
                }
            }

            for (int i = existingCallers.Length - 1; i >= 0; i--) {
                StateMachineCaller caller = existingCallers[i];

                if (caller != null && !callers.Contains(caller)) {
                    callers.Remove(caller);
                    caller.Remove();
                }
            }
        }
 /// <summary>
 /// Displays the property field in the center of the window.
 /// This method distinguishes between certain properties.
 /// The GameObject tag, for example, shouldn't be displayed with a regular string field.
 /// </summary>
 /// <param name="p">The SerializedProerty to display</param>
 /// <param name="width">The width of the whole thing in the ui</param>
 private void PropertyField(SerializedProperty p, float width = 170)
 {
     if(p.IsRealArray())
     {
         DisplayArrayProperty(p, width);
     }
     else
     {
         var oldValue = p.GetValue();
         if(fieldname == "GameObject.TagString")
         {
             var oldTag = oldValue as string;
             var newTag = EditorGUILayout.TagField("", oldTag, GUILayout.Width(width));
             if(newTag != oldTag)
             {
                 p.SetValue(newTag);
             }
         }
         else if(fieldname == "GameObject.StaticEditorFlags")
         {
             DisplayStaticFlagChooser(p, width);
         }
         else
         {
             EditorGUILayout.PropertyField(p, new GUIContent(""), GUILayout.Width(width));
         }
         if(!object.Equals(p.GetValue(), oldValue))
         {
             p.serializedObject.ApplyModifiedProperties();
             UsedNew();
         }
     }
 }
Example #12
0
 public static void SetSerializedValue <T>(this UnityEditor.SerializedProperty property, T value)
 {
     property.SetValue(value);
 }
        public void ToggleButton(SerializedProperty boolProperty, GUIContent trueLabel, GUIContent falseLabel)
        {
            Rect indentedPosition = EditorGUI.IndentedRect(_currentPosition);
            boolProperty.SetValue(ToggleButton(indentedPosition, boolProperty.GetValue<bool>(), trueLabel, falseLabel));

            _currentPosition.y += _currentPosition.height + 2;
        }
        void OnEnumFlagSelected(FlagsOption option, SerializedProperty property)
        {
            var enumValue = property.GetValue<int>();

            switch (option.Type)
            {
                case FlagsOption.OptionTypes.Everything:
                    enumValue = -1;
                    break;
                case FlagsOption.OptionTypes.Nothing:
                    enumValue = 0;
                    break;
                case FlagsOption.OptionTypes.Custom:
                    var value = (int)option.Value;

                    if ((enumValue & value) == value)
                        enumValue &= ~value;
                    else
                        enumValue |= value;
                    break;
            }

            property.SetValue(enumValue);
        }
        void OnByteFlagSelected(FlagsOption option, SerializedProperty property)
        {
            var byteFlag = property.GetValue<ByteFlag>();

            switch (option.Type)
            {
                case FlagsOption.OptionTypes.Everything:
                    byteFlag = new ByteFlag(enumValues.Convert((object v) => Convert.ToByte(v)));
                    break;
                case FlagsOption.OptionTypes.Nothing:
                    byteFlag = ByteFlag.Nothing;
                    break;
                case FlagsOption.OptionTypes.Custom:
                    byte value = (byte)option.Value;
                    byteFlag = byteFlag[value] ? byteFlag - value : byteFlag + value;
                    break;
            }

            property.SetValue(byteFlag);
        }