91 lines
3.0 KiB
C#
91 lines
3.0 KiB
C#
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; // 返回已经清理过的选中物体
|
||
}
|
||
}
|
||
}
|
||
} |