Ejemplo n.º 1
0
 protected override void OnReset()
 {
     foreach (Setting setting in GetSettings())
     {
         if (typeof(PartSettings).IsAssignableFrom(setting.type))
         {
             PartSettings partSettings = (PartSettings)setting.field.GetValue(setting.obj);
             partSettings.Reset();
             partSettings.OnChange();
         }
     }
 }
Ejemplo n.º 2
0
        public static void SettingField(Setting setting)
        {
            if (setting == null)
            {
                return;
            }
            HeaderAttribute headerAttr = setting.field.GetCustomAttribute <HeaderAttribute>();

            if (headerAttr != null)
            {
                Header(headerAttr.header);
            }
            bool delayed = setting.field.GetCustomAttribute <DelayedAttribute>() != null;

            if (setting.type == typeof(string))
            {
                string stringValue = (string)setting.field.GetValue(setting.obj);
                stringValue = !delayed?
                              EditorGUILayout.TextField(setting.label, stringValue) :
                                  EditorGUILayout.DelayedTextField(setting.label, stringValue);

                setting.field.SetValue(setting.obj, stringValue);
            }
            else
            if (setting.type == typeof(int))
            {
                int            intValue  = (int)setting.field.GetValue(setting.obj);
                RangeAttribute rangeAttr = setting.field.GetCustomAttribute <RangeAttribute>();
                if (rangeAttr != null)
                {
                    intValue = EditorGUILayout.IntSlider(setting.label, intValue,
                                                         (int)rangeAttr.min, (int)rangeAttr.max);
                }
                else
                {
                    intValue = !delayed?
                               EditorGUILayout.IntField(setting.label, intValue) :
                                   EditorGUILayout.DelayedIntField(setting.label, intValue);

                    MinAttribute minAttr = setting.field.GetCustomAttribute <MinAttribute>();
                    if (minAttr != null)
                    {
                        intValue = intValue < (int)minAttr.min ? (int)minAttr.min : intValue;
                    }
                }
                setting.field.SetValue(setting.obj, intValue);
            }
            else
            if (setting.type == typeof(float))
            {
                float          floatValue = (float)setting.field.GetValue(setting.obj);
                RangeAttribute rangeAttr  = setting.field.GetCustomAttribute <RangeAttribute>();
                if (rangeAttr != null)
                {
                    floatValue = EditorGUILayout.Slider(setting.label, floatValue, rangeAttr.min,
                                                        rangeAttr.max);
                }
                else
                {
                    floatValue = !delayed?
                                 EditorGUILayout.FloatField(setting.label, floatValue) :
                                     EditorGUILayout.DelayedFloatField(setting.label, floatValue);

                    MinAttribute minAttr = setting.field.GetCustomAttribute <MinAttribute>();
                    if (minAttr != null)
                    {
                        floatValue = floatValue < minAttr.min ? minAttr.min : floatValue;
                    }
                }
                setting.field.SetValue(setting.obj, floatValue);
            }
            else
            if (setting.type == typeof(bool))
            {
                bool boolValue = (bool)setting.field.GetValue(setting.obj);
                boolValue = EditorGUILayout.Toggle(setting.label, boolValue);
                setting.field.SetValue(setting.obj, boolValue);
            }
            else
            if (setting.type == typeof(Color))
            {
                Color colorValue = (Color)setting.field.GetValue(setting.obj);
                colorValue = EditorGUILayout.ColorField(setting.label, colorValue);
                setting.field.SetValue(setting.obj, colorValue);
            }
            else
            if (setting.type.IsEnum)
            {
                Enum     enumValue   = (Enum)setting.field.GetValue(setting.obj);
                int      intValue    = Convert.ToInt32(enumValue);
                string[] enumOptions = EnumCache.GetOptions(setting.type);
                Array    enumValues  = EnumCache.GetValues(setting.type);
                if (!setting.toggle)
                {
                    intValue = EditorGUILayout.IntPopup(setting.label, intValue, enumOptions,
                                                        (int[])enumValues);
                    setting.field.SetValue(setting.obj, intValue);
                }
                else
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel(setting.label);
                    int i = ArrayUtility.IndexOf <string>(enumOptions, enumValue.ToString());
                    i = GUILayout.Toolbar(i, enumOptions);
                    EditorGUILayout.EndHorizontal();
                    setting.field.SetValue(setting.obj, i);
                }
            }
            else
            if (setting.type.IsGenericList())
            {
                SettingList(setting);
            }
            else
            if (typeof(PartSettings).IsAssignableFrom(setting.type))
            {
                PartSettings partSettings = (PartSettings)setting.field.GetValue(setting.obj);
                EditorGUI.BeginChangeCheck();
                partSettings.OnGUI(setting);
                if (EditorGUI.EndChangeCheck())
                {
                    partSettings.OnChange();
                }
            }
            firstElement = false;
        }