DevSettings Pattern

Table of Contents

About

A (maybe) useful template for development settings. This template provides a functionality that configures some values by EditorWindow, keep its values with PlayerPrefs, and referencing it at runtime while developing.

Make less duplicating by using C# Reflection.

DevSettings.cs

using System.Reflection;
using UnityEngine;

#if UNITY_EDITOR
namespace Dotge
{
  public static class DevSettings
  {
      public static bool Invincible
      {
          get { return PlayerPrefs.GetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4)) != 0; }
          set { PlayerPrefs.SetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4), value ? 1 : 0); }
      }

      public static bool SkipPressAnyKey
      {
          get { return PlayerPrefs.GetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4)) != 0; }
          set { PlayerPrefs.SetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4), value ? 1 : 0); }
      }

      public static bool SkipDeadAnim
      {
          get { return PlayerPrefs.GetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4)) != 0; }
          set { PlayerPrefs.SetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4), value ? 1 : 0); }
      }

      public static bool SkipAfterDead
      {
          get { return PlayerPrefs.GetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4)) != 0; }
          set { PlayerPrefs.SetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4), value ? 1 : 0); }
      }

      public static bool SkipHighscore
      {
          get { return PlayerPrefs.GetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4)) != 0; }
          set { PlayerPrefs.SetInt("editor." + MethodBase.GetCurrentMethod().Name.Substring(4), value ? 1 : 0); }
      }
  }
}
#endif

DevSettingsWindow.cs

using System.Reflection;
using UnityEditor;

namespace Dotge
{
    public class DevSettingsWindow : EditorWindow
    {
        [MenuItem("Custom/DevSettings")]
        public static void Open()
        {
            GetWindow<DevSettingsWindow>();
        }

        void OnGUI()
        {
            var ps = typeof(DevSettings).GetProperties();
            foreach (var p in ps)
            {
                UIForProperty(p);
            }
        }

        void UIForProperty(PropertyInfo p)
        {
            if (p.PropertyType == typeof(bool))
            {
                ToggleForProperty(p);
            }
            else if (p.PropertyType == typeof(int))
            {
                IntFieldForProperty(p);
            }
            else if (p.PropertyType == typeof(string))
            {
                TextFieldForProperty(p);
            }
        }

        void ToggleForProperty(PropertyInfo p)
        {
            bool value = (bool)p.GetValue(null, null);
            bool updated = EditorGUILayout.Toggle(p.Name, value);
            p.SetValue(null, updated, null);
        }

        void IntFieldForProperty(PropertyInfo p)
        {
            int value = (int)p.GetValue(null, null);
            int updated = EditorGUILayout.IntField(p.Name, value);
            p.SetValue(null, updated, null);
        }

        void TextFieldForProperty(PropertyInfo p)
        {
            string value = (string)p.GetValue(null, null);
            string updated = EditorGUILayout.TextField(p.Name, value);
            p.SetValue(null, updated, null);
        }
    }
}