Skip to content

Instantly share code, notes, and snippets.

@hecres
Created March 14, 2018 12:44
Show Gist options
  • Select an option

  • Save hecres/7fa224ee30a54adbe8a62d1661ae420d to your computer and use it in GitHub Desktop.

Select an option

Save hecres/7fa224ee30a54adbe8a62d1661ae420d to your computer and use it in GitHub Desktop.
プロジェクト設定、Lighting設定へのショートカットをつくるエディタ拡張
using System;
using System.Collections.Generic;
using System.Linq;
using Hecres.EditorShortcuts.Editor.ProjectSettings;
using Hecres.EditorShortcuts.Editor.RenderSettings;
using UnityEditor;
using UnityEngine;
namespace Hecres.EditorShortcuts.Editor
{
public class ShortcutMenu : UnityEditor.Editor
{
// ReSharper disable once UnusedMember.Local
[MenuItem("Hecres/Shortcuts/Setting Shortcut")]
private static void ShowSettingShortcutWindow()
{
const string title = "Shortcut";
EditorWindow.GetWindow(typeof(SettingShortcutWindow), false, title);
}
}
}
namespace Hecres.EditorShortcuts.Editor.ProjectSettings
{
public enum ProjectSettingTypes
{
Input,
TagsAndLayers,
Audio,
Time,
Player,
Physics,
Physics2D,
Quality,
Graphics,
Network,
Editor,
ScriptExecutionOrder
}
}
namespace Hecres.EditorShortcuts.Editor.ProjectSettings
{
public static class ProjectSettingNames
{
private static readonly Dictionary<ProjectSettingTypes, string> Table
= new Dictionary<ProjectSettingTypes, string>
{
{ProjectSettingTypes.Input, "Input"},
{ProjectSettingTypes.TagsAndLayers, "Tags and Layers"},
{ProjectSettingTypes.Audio, "Audio"},
{ProjectSettingTypes.Time, "Time"},
{ProjectSettingTypes.Player, "Player"},
{ProjectSettingTypes.Physics, "Physics"},
{ProjectSettingTypes.Physics2D, "Physics 2D"},
{ProjectSettingTypes.Quality, "Quality"},
{ProjectSettingTypes.Graphics, "Graphics"},
{ProjectSettingTypes.Network, "Network"},
{ProjectSettingTypes.Editor, "Editor"},
{ProjectSettingTypes.ScriptExecutionOrder, "Script Execution Order"}
};
public static string GetValue(ProjectSettingTypes key)
{
return Table[key];
}
public static IEnumerable<string> ToValueList()
{
return new List<string>(Table.Values);
}
}
}
namespace Hecres.EditorShortcuts.Editor.RenderSettings
{
public enum RenderSettingTypes
{
Lighting
}
}
namespace Hecres.EditorShortcuts.Editor.RenderSettings
{
public static class RenderSettingNames
{
private static readonly Dictionary<RenderSettingTypes, string> Table
= new Dictionary<RenderSettingTypes, string>
{
{RenderSettingTypes.Lighting, "Lighting"}
};
public static string GetValue(RenderSettingTypes key)
{
return Table[key];
}
public static IEnumerable<string> ToValueList()
{
return new List<string>(Table.Values);
}
}
}
namespace Hecres.EditorShortcuts.Editor
{
public class SettingShortcutWindow : EditorWindow
{
private const float ButtonHeight = 24f;
private readonly string[] projectSettingTexts;
private readonly string[] renderSettingTexts;
public SettingShortcutWindow()
{
projectSettingTexts = ProjectSettingNames.ToValueList().ToArray();
renderSettingTexts = RenderSettingNames.ToValueList().ToArray();
InitializeSize();
}
private void InitializeSize()
{
// デフォルト設定ではWindowをあまり縮小出来ないため上書きします。
minSize = new Vector2(minSize.x, ButtonHeight);
maxSize = new Vector2(maxSize.x, ButtonHeight);
var tempPosition = position;
tempPosition.size = new Vector2(position.size.x, minSize.y);
position = tempPosition;
}
// ReSharper disable once InconsistentNaming
// ReSharper disable once UnusedMember.Local
private void OnGUI()
{
GUILayout.BeginHorizontal();
DrawProjectSettingShortcuts();
DrawRenderSettingShortcuts();
GUILayout.EndHorizontal();
}
private void DrawProjectSettingShortcuts()
{
var toolbarWidth = CalculateToolbarCellWidth() * projectSettingTexts.Length;
var selected = GUILayout.Toolbar(-1, projectSettingTexts, GUILayout.MaxWidth(toolbarWidth));
if (selected < 0 || selected >= projectSettingTexts.Length)
{
return;
}
EditorApplication.ExecuteMenuItem($"Edit/Project Settings/{projectSettingTexts[selected]}");
}
private void DrawRenderSettingShortcuts()
{
var toolbarWidth = CalculateToolbarCellWidth() * renderSettingTexts.Length;
var selected = GUILayout.Toolbar(-1, renderSettingTexts, GUILayout.MaxWidth(toolbarWidth));
if (selected < 0 || selected >= renderSettingTexts.Length)
{
return;
}
// RenderSettingsはメニュー項目の位置がばらけています。
var selectedType = (RenderSettingTypes)selected;
switch (selectedType)
{
case RenderSettingTypes.Lighting:
EditorApplication.ExecuteMenuItem("Window/Lighting/Settings");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private float CalculateToolbarCellWidth()
{
return position.width / (projectSettingTexts.Length + renderSettingTexts.Length);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment