Version: 2020.2
public static Object[] GetFiltered (Type type, SelectionMode mode);

参数

type 仅检索此类型的对象。
mode 进一步调整选择的选项。

描述

返回按类型和模式筛选的当前选择。

对于具有多个 type 组件的选定游戏对象,只有第一个将包含在结果中。\ 如果 typeComponentGameObject 的子类,则支持完整的 SelectionMode。\ 如果 type 不是 ComponentGameObject 的子类(如 MeshScriptableObject),则仅支持 SelectionMode.ExcludePrefabSelectionMode.Editable

using UnityEngine;
using UnityEditor;

class ToggleActive : ScriptableObject { [MenuItem("Example/Toggle Active of Selected %i")] static void DoToggle() { Object[] activeGOs = Selection.GetFiltered( typeof(GameObject), SelectionMode.Editable | SelectionMode.TopLevel);

foreach (GameObject obj in activeGOs) { obj.SetActive(!obj.activeSelf); } } }