[UnityAPI]Selection类

参考链接:

https://blog.csdn.net/qq_33337811/article/details/72858209

0.介绍

Selection这个类是针对Hierarchy视图和Project视图下的选中

1.常用变量 & 方法

a.非过滤模式

单选:Selection.activeTransform、Selection.activeGameObject、Selection.activeObject

多选:Selection.transforms、Selection.gameObjects、Selection.objects

b.过滤模式

Selection.GetFiltered

测试:

 1 using UnityEditor;
 2 using UnityEngine;
 3 
 4 public class TestSelection 
 5 {
 6     [MenuItem("Test/Selection.activeTransform | activeGameObject | activeObject")]
 7     private static void Test()
 8     {
 9         Debug.Log(Selection.activeTransform);//返回当前选中的Hierarchy视图下的物体
10         Debug.Log(Selection.activeGameObject);//返回当前选中的Hierarchy视图下的物体 or Project视图下的预制体
11         Debug.Log(Selection.activeObject);//返回当前选中的Hierarchy视图下的物体 or Project视图下的资源
12     }
13 
14     [MenuItem("Test/Selection.transforms | gameObjects | objects")]
15     private static void Test2()
16     {
17         //Selection.activeTransform的多选版本,注意返回的是top level的
18         Debug.Log("Selection.transforms----------------------------");
19         Transform[] tras = Selection.transforms;
20         for (int i = 0; i < tras.Length; i++)
21         {
22             Debug.Log(tras[i]);
23         }
24 
25         //Selection.activeGameObject的多选版本
26         Debug.Log("Selection.gameObjects----------------------------");
27         GameObject[] goes = Selection.gameObjects;
28         for (int i = 0; i < goes.Length; i++)
29         {
30             Debug.Log(goes[i]);
31         }
32 
33         //Selection.activeObject的多选版本
34         Debug.Log("Selection.objects----------------------------");
35         Object[] objs = Selection.objects;
36         for (int i = 0; i < objs.Length; i++)
37         {
38             Debug.Log(objs[i]);
39         }
40     }
41 
42     [MenuItem("Test/Selection.GetFiltered")]
43     private static void Test3()
44     {
45         //Unfiltered:Return the whole selection.
46         //TopLevel:Only return the topmost selected transform. A selected child of another selected transform will be filtered out.
47         //Deep:Return the selection and all child transforms of the selection.
48         //ExcludePrefab:Excludes any Prefabs from the selection.
49         //Editable:Excludes any objects which shall not be modified.
50 
51         //Assets:Only return objects that are assets in the Asset directory.
52         //(Project视图,只返回Asset文件夹的资源。只返回选中的资源,选中文件夹时不会查找该文件夹下的资源)
53 
54         //DeepAssets:If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy.
55         //(Project视图,如果选择里包含文件夹,则也包括文件夹里的文件和子文件夹。会递归文件夹)
56         Texture[] textures = Selection.GetFiltered<Texture>(SelectionMode.Assets);
57         for (int i = 0; i < textures.Length; i++)
58         {
59             Debug.Log(textures[i]);
60         }
61     }
62 }
原文地址:https://www.cnblogs.com/lyh916/p/13173136.html