Unity编辑器中的选择器工具类Selection提供了一些常用的函数来处理场景中的选择和操作。以下是Selection类的常用函数和用法:
- Selection.activeGameObject:获取当前选中的游戏对象。
- Selection.activeTransform:获取当前选中游戏对象的变换组件。
- Selection.gameObjects:获取当前选中的多个游戏对象。
- Selection.transforms:获取当前选中游戏对象的变换组件列表。
- Selection.activeInstanceID:获取当前选中游戏对象的实例ID。
- Selection.assetGUIDs:获取当前选中的资源的GUID列表。
- Selection.activeObject:获取当前选中的单个资源对象。
- Selection.activeContext:获取当前选中游戏对象的上下文信息。
- Selection.activeTerrain:获取当前选中的地形对象。
用法示例:
using UnityEditor;
using UnityEngine;
public class MyEditorScript
{
[MenuItem("MyMenu/Do Something")]
private static void DoSomething()
{
// 获取当前选中的游戏对象
GameObject selectedGameObject = Selection.activeGameObject;
// 获取当前选中的变换组件
Transform selectedTransform = Selection.activeTransform;
// 获取当前选中的多个游戏对象
GameObject[] selectedGameObjects = Selection.gameObjects;
// 获取当前选中的变换组件列表
Transform[] selectedTransforms = Selection.transforms;
// 获取当前选中游戏对象的实例ID
int instanceID = Selection.activeInstanceID;
// 获取当前选中的资源的GUID列表
string[] assetGUIDs = Selection.assetGUIDs;
// 获取当前选中的单个资源对象
Object activeObject = Selection.activeObject;
// 获取当前选中游戏对象的上下文信息
UnityEngine.Object activeContext = Selection.activeContext;
// 获取当前选中的地形对象
Terrain activeTerrain = Selection.activeTerrain;
// 在控制台打印选中的游戏对象名称
if (selectedGameObject != null)
{
Debug.Log("Selected GameObject: " + selectedGameObject.name);
}
else
{
Debug.Log("No GameObject selected.");
}
}
}
通过Selection类,你可以在Unity编辑器中方便地获取和操作当前选中的游戏对象和资源,使得编辑器扩展和工具开发更加便捷。
香港五网CN2网络云服务器链接:www.tsyvps.com
蓝易云香港五网CN2 GIA/GT精品网络服务器。拒绝绕路,拒绝不稳定。