Skip to content

Instantly share code, notes, and snippets.

@AzureFlow
Created June 27, 2016 21:23
Show Gist options
  • Save AzureFlow/2322bf9c80588431ebce00416a965051 to your computer and use it in GitHub Desktop.
Save AzureFlow/2322bf9c80588431ebce00416a965051 to your computer and use it in GitHub Desktop.
Custom Unity PreferencesWindow
using UnityEngine;
using UnityEditor;
public class PreferencesWindow : EditorWindow
{
private const string WINDOW_NAME = "Preferences"; //Text displayed for the menu and window name
private const string SHORTCUT = CTRL + Shift + "W"; //Shortcut to open the window. Shortcut = Ctrl+Shift+W
private const string MENU_LOCATION = "Window/" + WINDOW_NAME + " " + SHORTCUT; //Location and name of menu item
private Vector2 scrollPos = Vector2.zero;
private System.Collections.Generic.List<ITab> tabs = new System.Collections.Generic.List<ITab>();
private int _selectedIndex = 0;
private int SelectedIndex
{
get
{
return this._selectedIndex;
}
set
{
this._selectedIndex = value;
if(this._selectedIndex >= tabs.Count)
{
this._selectedIndex = 0;
}
else
{
if(this._selectedIndex >= 0)
return;
this._selectedIndex = tabs.Count - 1;
}
}
}
private ITab SelectedTab
{
get
{
return tabs[_selectedIndex];
}
}
[MenuItem(MENU_LOCATION)]
public static void Init()
{
//Init window
EditorWindow window = EditorWindow.GetWindow<RPGWindow>(true, WINDOW_NAME, true);
window.minSize = new Vector2(750, 400);
window.position = new Rect(new Vector2(200, 200), window.minSize);
}
public void OnEnable()
{
//Add tabs
tabs.Add(new TabOne());
tabs.Add(new TabTwo());
}
public void OnGUI()
{
//Styles
GUIStyle sectionScrollView = (GUIStyle)"PreferencesSectionBox";
GUIStyle sectionElement = (GUIStyle)"PreferencesSection";
GUIStyle selected = (GUIStyle) "ServerUpdateChangesetOn";
GUIStyle sectionHeader = new GUIStyle(EditorStyles.largeLabel);
sectionHeader.fontStyle = FontStyle.Bold;
sectionHeader.fontSize = 18;
sectionHeader.margin.top = 10;
++sectionHeader.margin.left;
sectionHeader.normal.textColor = EditorGUIUtility.isProSkin ? new Color(0.7f, 0.7f, 0.7f, 1f) : new Color(0.4f, 0.4f, 0.4f, 1f);
EditorGUIUtility.labelWidth = 200;
HandleKeyControl(); //Keyboard navigation (Up/Down)
GUILayout.BeginHorizontal();
{
//Sidebar
scrollPos = GUILayout.BeginScrollView(scrollPos, sectionScrollView, GUILayout.Width(120f));
{
GUILayout.Space(40); //Offset buttons
for(int i = 0; i < tabs.Count; i++) //Display each tab button
{
ITab tab = tabs[i];
bool isActive = i == SelectedIndex;
Rect rect = GUILayoutUtility.GetRect(tab.Title, sectionElement, GUILayout.ExpandWidth(true));
//Selection highlight
if(isActive && Event.current.type == EventType.Repaint)
selected.Draw(rect, false, false, false, false);
//Button
EditorGUI.BeginChangeCheck();
{
if(GUI.Toggle(rect, isActive, tab.Title, sectionElement))
SelectedIndex = i;
}
if(EditorGUI.EndChangeCheck()) GUIUtility.keyboardControl = 0;
//GUILayout.Space(1); //If you want a little extra space
}
}
GUILayout.EndScrollView();
//Content
GUILayout.Space(10);
{
GUILayout.BeginVertical();
{
GUILayout.Label(SelectedTab.Title, sectionHeader); //Header text
//Draw tab's content
GUILayout.Space(10);
{
SelectedTab.OnGUI();
}
GUILayout.Space(5);
}
GUILayout.EndVertical();
}
GUILayout.Space(10);
}
GUILayout.EndHorizontal();
}
private void HandleKeyControl()
{
if(Event.current.type != EventType.KeyDown || GUIUtility.keyboardControl != 0)
return;
switch(Event.current.keyCode)
{
case KeyCode.UpArrow:
--SelectedIndex;
Event.current.Use();
break;
case KeyCode.DownArrow:
++SelectedIndex;
Event.current.Use();
break;
}
}
public interface ITab
{
GUIContent Title { get; }
void OnGUI();
}
public class TabOne : ITab
{
public GUIContent Title
{
get { return new GUIContent("Page #1"); }
//get { return new GUIContent("Page #1", (Texture)EditorGUIUtility.FindTexture("Folder Icon")); }
}
public void OnGUI()
{
GUILayout.Label("Content here");
GUILayout.FlexibleSpace();
if(GUILayout.Button("Use Defaults", GUILayout.Width(120)))
Debug.LogWarning("This button does nothing! You fool!");
}
}
public class TabTwo : ITab
{
public GUIContent Title
{
get { return new GUIContent("Page #2"); }
}
public void OnGUI()
{
GUILayout.Label("Placeholder");
}
}
//KeyMods
public const string CTRL = "%"; //Command key on mac
public const string ALT = "&";
public const string SHIFT = "#";
public const string R_SHIFT = "#RIGHT";
public const string L_SHIFT = "#LEFT";
//Seems to be broken in Unity
public const string NO_MOD = "_"; //NO_MOD + "B" will open the menu when B is pressed
public const string LEFT = "LEFT";
public const string RIGHT = "RIGHT";
public const string UP = "UP";
public const string DOWN = "DOWN";
public const string HOME = "HOME";
public const string END = "END";
public const string PAGE_UP = "PGUP";
public const string PAGE_DOWN = "PGDN";
public const string F1 = "F1";
public const string F2 = "F2";
public const string F3 = "F3";
public const string F4 = "F4";
public const string F5 = "F5";
public const string F6 = "F6";
public const string F7 = "F7";
public const string F8 = "F8";
public const string F9 = "F9";
public const string F10 = "F10";
public const string F11 = "F11";
public const string F12 = "F12";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment