-
-
Save urushisora/fb1dd31a39793128f1f6f1434e637992 to your computer and use it in GitHub Desktop.
[unity][cluster] Item が入れ子になっているとき Hierarchy に ! を表示する
This file contains 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
// 内容: ClusterVR.CreatorKit.Item が入れ子になっているとき Hierarchy に ! を表示する Editor 拡張 | |
// 制作環境: Unity2019.4.11f1 | |
// 使い方: Editor ディレクトリ配下に置くだけ | |
// 例: Assets/Editor/Hierarchy/NestedItemDetector.cs | |
using System.Linq; | |
using ClusterVR.CreatorKit.Item.Implements; | |
using UnityEditor; | |
using UnityEngine; | |
public static class NestedItemDetector | |
{ | |
const int WIDTH = 16; | |
const bool itemOnly = true; // Item コンポーネントがついていないオブジェクトにも ! を表示したいときは false にする | |
[InitializeOnLoadMethod] | |
static void Initialize() | |
{ | |
EditorApplication.hierarchyWindowItemOnGUI += OnGUI; | |
} | |
static void OnGUI(int instanceID, Rect selectionRect) | |
{ | |
var go = EditorUtility.InstanceIDToObject(instanceID) as GameObject; | |
if (go == null) | |
{ | |
return; | |
} | |
// 自分が Item なら子に Item が1つでもあればネストしている | |
var item = go.GetComponent<Item>(); | |
if (item != null && go.GetComponentsInChildren<Item>(includeInactive).Count() > 1) | |
{ | |
ShowLabel(selectionRect); | |
return; | |
} | |
// 自分が Item なら、親に Item が1つでもあればネストしている | |
if (item != null && ParentHaveItem(item)) | |
{ | |
ShowLabel(selectionRect); | |
return; | |
} | |
if (itemOnly) | |
{ | |
return; | |
} | |
// 自分が Item でなくても子がネストしてたら ! を表示する | |
var childItems = go.GetComponentsInChildren<Item>(includeInactive); | |
foreach (var childItem in childItems) | |
{ | |
if (ParentHaveItem(childItem)) | |
{ | |
ShowLabel(selectionRect); | |
return; | |
} | |
} | |
} | |
static bool ParentHaveItem(Item item) | |
{ | |
var parent = item.transform.parent; | |
return parent != null && parent.GetComponentInParent<Item>() != null; | |
} | |
static void ShowLabel(Rect selectionRect) | |
{ | |
var pos = selectionRect; | |
pos.x = pos.xMax - WIDTH; | |
pos.width = WIDTH; | |
GUI.Label(pos, "!"); | |
} | |
/// <summary> | |
/// 試験的な機能でまだ未対応 | |
/// </summary> | |
const bool includeInactive = true; // todo: inactive なものでも入れ子になっていたらアップロードでエラーが出るので true 前提で実装しているが, 検出対象として inactive を無視したい需要もありそう | |
/// <summary> | |
/// 試験的な機能でまだ未対応 | |
/// </summary> | |
} |
自分が Item じゃなくても、子の方で入れ子になってたら ! 表示した方がわかりやすいか。
Item じゃないオブジェクトにも ! を表示する機能を追加しましたが、! が多くなりすぎてどこが Item かわかりづらく使いづらい気がしたので、デフォルトは Item だけに表示して、それ以外のオブジェクトについてはフラグで切り替えるようにしておきました
https://gist.github.com/urushisora/fb1dd31a39793128f1f6f1434e637992#file-nesteditemdetector-cs-L14
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
表示はこんな感じです