Skip to content

Instantly share code, notes, and snippets.

@a1678991
Created September 9, 2021 11:26
Show Gist options
  • Save a1678991/e620e4d49d8353d621403904a9bfeabc to your computer and use it in GitHub Desktop.
Save a1678991/e620e4d49d8353d621403904a9bfeabc to your computer and use it in GitHub Desktop.
選択されたComponentと同一のComponentを持つ子GameObjectを選択するやつ
/*
* © @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