Skip to content

Instantly share code, notes, and snippets.

@liveasnotes
Last active August 20, 2020 02:08
Show Gist options
  • Select an option

  • Save liveasnotes/cdbb66b411d5462bcb90827e51708dde to your computer and use it in GitHub Desktop.

Select an option

Save liveasnotes/cdbb66b411d5462bcb90827e51708dde to your computer and use it in GitHub Desktop.
VR4PE_20200223
// (c) 2019 liveasnotes
// https://gist.github.com/liveasnotes/cdbb66b411d5462bcb90827e51708dde
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// ヒエラルキーウィンドウで、選択したオブジェクト(複数選択可)の直下にある子オブジェクトを選択できるスクリプト
/// VSCodeのヒントに従った結果、GameObj->Transform->GameObjという無駄そうな型変換が起きているけど放置
/// <summary>
public static class SelectAllDirectChildren
{
[MenuItem("Edit/Select All Direct Children %&a")]
private static void SelectChildren()
{
if (Selection.activeGameObject)
{
List<GameObject> targetChildren = new List<GameObject>(); // Debug.Log("list created");
foreach (GameObject rootObj in Selection.objects)
{
foreach (Transform obj in rootObj.transform)
{
if (obj != rootObj)
{
targetChildren.Add(obj.gameObject); // Debug.Log("list modified");
}
}
// 以下、LINQを試そうとしたやつの残骸。面倒だったので放置
// ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
// List<GameObject> children = rootObj.GetComponentsInChildren<GameObject>().Where(i => i != rootObj).ToList();
// foreach (GameObject c in children)
// {
// targetChildren.Add(c);
// }
// foreach (GameObject c in rootObj.GetComponentsInChildren<GameObject>().Where(i => i != rootObj).ToList())
// {
// targetChildren.Add(c);
// }
// targetChildren.Concat(rootObj.GetComponentsInChildren<GameObject>().Where(i => i != rootObj).ToList());
}
Selection.objects = targetChildren.ToArray();
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment