Last active
February 7, 2020 06:23
-
-
Save TsubameUnity/e7b53c0c2e66efa4a97161c05985e0a3 to your computer and use it in GitHub Desktop.
GameObjectのヘッダーに情報を追加できるようにするエディタ拡張。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
[CustomEditor(typeof(GameObject))] | |
public class EditorHeaderHideFlags : Editor | |
{ | |
Editor defaultCustomEditor; | |
Type defaultInspectorType; | |
MethodInfo defaultHeaderGUI; | |
GUIStyle inspectorBig; | |
GUIStyle postLargeHeaderBackground; | |
public EditorHeaderHideFlags() | |
{ | |
// GameObjectのヘッダー描写はここが行っている | |
defaultInspectorType = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.GameObjectInspector"); | |
// ヘッダー部分の呼び出しを取得 | |
defaultHeaderGUI = defaultInspectorType.GetMethod("OnHeaderGUI", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); | |
} | |
protected override void OnHeaderGUI() | |
{ | |
inspectorBig = inspectorBig ?? new GUIStyle("In BigTitle"); | |
postLargeHeaderBackground = postLargeHeaderBackground ?? new GUIStyle("IN BigTitle Post"); | |
// カスタムエディター情報を取得 | |
defaultCustomEditor = CreateEditor(target, defaultInspectorType); | |
// デフォルトの表示を描画 | |
defaultHeaderGUI.Invoke(defaultCustomEditor, null); | |
// 空いているスペース分を戻す | |
GUILayout.Space( | |
-1f | |
- inspectorBig.margin.bottom | |
- inspectorBig.padding.bottom | |
- inspectorBig.overflow.bottom | |
); | |
// ヘッダー追加項目を以下に記載 | |
using (new EditorGUILayout.VerticalScope(postLargeHeaderBackground, GUILayout.ExpandWidth(true))) | |
{ | |
var obj = target as GameObject; | |
bool hierarchyMode = (obj.scene.name == UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().name); | |
// PrefabではHideFlagsを編集しても戻るため表示させない | |
if (!PrefabUtility.GetCorrespondingObjectFromOriginalSource(target) && hierarchyMode) | |
{ | |
using (var check = new EditorGUI.ChangeCheckScope()) | |
{ | |
var hideFlags = obj.hideFlags; | |
using (new EditorGUILayout.HorizontalScope()) | |
{ | |
EditorGUIUtility.labelWidth = 60.0f; | |
GUILayout.Space(20); | |
hideFlags = (HideFlags)EditorGUILayout.EnumFlagsField("HideFlags", obj.hideFlags, GUILayout.Width(220.0f)); | |
EditorGUIUtility.labelWidth = 0f; | |
} | |
if (check.changed) | |
{ | |
Undo.RegisterCompleteObjectUndo(target, "Changed HideFlags"); | |
obj.hideFlags = hideFlags; | |
var scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene(); | |
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene); | |
} | |
} | |
} | |
} | |
} | |
public override void OnInspectorGUI() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment