AlicizaX/Client/Packages/com.alicizax.uxtool/Editor/UXGUI/Utils/UXSelectionUtil.cs
陈思海 5862fb2af1 add
1.新增手柄相关适配
2.新增设备重映射绑定
3.新增设备映射图标更新
2025-12-09 20:31:44 +08:00

91 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace AlicizaX.UXTool
{
public static class UXSelectionUtil
{
public static void AddSelectionChangedEvent(Action e)
{
Selection.selectionChanged += e;
}
public static void RemoveSelectionChangedEvent(Action e)
{
Selection.selectionChanged -= e;
}
// 存储选中物体的缓存列表
private static List<GameObject> gameObjectsCache = new List<GameObject>();
/// <summary>
/// 获取当前选中的所有物体排除包含StageEngine脚本的物体
/// </summary>
public static GameObject[] gameObjects
{
get
{
gameObjectsCache.Clear(); // 清空缓存
GameObject[] selectedObjects = Selection.gameObjects;
foreach (var obj in selectedObjects)
{
if (obj.GetComponent<StageEngine>() == null &&obj != PrefabStageUtils.StageRoot.gameObject) // 排除StageEngine脚本的物体
{
gameObjectsCache.Add(obj);
}
}
return gameObjectsCache.ToArray();
}
set => Selection.objects = value;
}
/// <summary>
/// 获取当前选中的第一个物体排除包含StageEngine脚本的物体
/// </summary>
public static GameObject activeGameObject
{
get
{
var selectedObjects = gameObjects;
return selectedObjects.Length > 0 ? selectedObjects[0] : null;
}
set { Selection.activeGameObject = value; }
}
/// <summary>
/// 获取当前选中的所有物体排除StageEngine脚本的物体并清除掉包含StageEngine脚本的物体。
/// </summary>
private static void ClearStageEngineSelection()
{
GameObject[] selectedObjects = Selection.gameObjects;
List<GameObject> validSelections = new List<GameObject>();
// 遍历选中的物体排除包含StageEngine脚本的物体
foreach (var obj in selectedObjects)
{
if (obj.GetComponent<StageEngine>() == null) // 如果没有StageEngine脚本则保留
{
validSelections.Add(obj);
}
}
// 清除所有选中物体
Selection.objects = validSelections.ToArray();
}
/// <summary>
/// 获取当前选中的物体并清除选中列表中包含StageEngine脚本的物体。
/// </summary>
public static GameObject[] clearedGameObjects
{
get
{
ClearStageEngineSelection(); // 清理包含 StageEngine 脚本的物体
return gameObjects; // 返回已经清理过的选中物体
}
}
}
}