Last active
May 13, 2021 10:54
-
-
Save kankikuchi/0a13099d1c5db624814543aff69e6d95 to your computer and use it in GitHub Desktop.
Hierarchy上で非アクティブなオブジェクトをエディタ上で検索する拡張【Unity】【エディタ拡張】
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
// InactiveGameObjectSearcher.cs | |
// http://kan-kikuchi.hatenablog.com/entry/InactiveGameObjectSearcher | |
// | |
// Created by kan.kikuchi on 2021.02.03. | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// 非アクティブなオブジェクトを検索するクラス | |
/// </summary> | |
public class InactiveGameObjectSearcher : EditorWindow { | |
//子も検索対象に含むか(自身は非アクティブではないが親が非アクティブなために表示されてないオブジェクトも検索するか) | |
private bool _isContainChildren = false; | |
//スクロール位置 | |
private Vector2 _scrollPosition = Vector2.zero; | |
//検索結果 | |
private List<GameObject> _inactiveGameObjects = new List<GameObject>(); | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
//メニューからウィンドウを表示 | |
[MenuItem("Tools/Open/Inactive GameObject Searcher")] | |
public static void Open (){ | |
InactiveGameObjectSearcher.GetWindow (typeof(InactiveGameObjectSearcher)); | |
} | |
//================================================================================= | |
//表示するGUIの設定 | |
//================================================================================= | |
private void OnGUI(){ | |
EditorGUILayout.HelpBox("非アクティブなオブジェクトを検索するクラス", MessageType.Info); | |
EditorGUILayout.Space(); | |
//子も検索対象に含むか | |
_isContainChildren = EditorGUILayout.ToggleLeft("子も検索対象に含むか", _isContainChildren); | |
//検索開始ボタン | |
if (GUILayout.Button("検索")) { | |
//Hierarchy上の非アクティブな物だけ取得 | |
_inactiveGameObjects = Resources.FindObjectsOfTypeAll<GameObject>() | |
.Where(gameObject => ((_isContainChildren && !gameObject.activeInHierarchy) || (!_isContainChildren && !gameObject.activeSelf)) | |
&& AssetDatabase.GetAssetOrScenePath(gameObject).Contains(".unity")) | |
.ToList(); | |
} | |
EditorGUILayout.Space(); | |
//描画範囲が足りなければスクロール出来るように | |
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUI.skin.scrollView); | |
//検索結果表示 | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
EditorGUILayout.LabelField("検索結果"); | |
foreach (var inactiveGameObject in _inactiveGameObjects.Where(inactiveGameObject => inactiveGameObject != null)) { | |
//非アクティブなオブジェクトの名前でボタン作成、押したら選択するように | |
if (GUILayout.Button(inactiveGameObject.name)) { | |
Selection.activeGameObject = inactiveGameObject; | |
} | |
} | |
EditorGUILayout.EndVertical(); | |
//スクロール箇所終了 | |
EditorGUILayout.EndScrollView(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment