lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  201 随笔 :: 0 文章 :: 12 评论 :: 21万 阅读

参考链接:

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 }
复制代码

 

posted on   艰苦奋斗中  阅读(873)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示