Created
September 9, 2021 11:26
-
-
Save a1678991/e620e4d49d8353d621403904a9bfeabc to your computer and use it in GitHub Desktop.
選択されたComponentと同一のComponentを持つ子GameObjectを選択するやつ
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
/* | |
* © @a1678991 | |
*/ | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
public class GameObjectSelector : MonoBehaviour | |
{ | |
private static void SelectGameObjectsInChildren(GameObject root, Component selectedComponent) | |
{ | |
var childComponents = root.GetComponentsInChildren(selectedComponent.GetType(), true); | |
var gameObjects = childComponents.Select(c => c.gameObject.GetInstanceID()).ToArray(); | |
Selection.instanceIDs = gameObjects; | |
} | |
[MenuItem("CONTEXT/Component/Select GameObject with this component in children")] | |
private static void SelectGameObjectsInChildren(MenuCommand menuCommand) | |
{ | |
var selectedComponent = menuCommand.context as Component; | |
if (selectedComponent == null) | |
{ | |
Debug.LogError("Selected component is null"); | |
return; | |
} | |
var selectedRoot = selectedComponent.gameObject; | |
if (selectedRoot == null) | |
{ | |
Debug.LogError("Selected component's gameObject is null"); | |
return; | |
} | |
SelectGameObjectsInChildren(selectedRoot, selectedComponent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment