201 lines
7.1 KiB
C#
201 lines
7.1 KiB
C#
using UnityEngine;
|
||
using System.Collections.Generic;
|
||
|
||
public class HideRootListGUIControl : MonoBehaviour
|
||
{
|
||
// 需控制显隐的节点列表
|
||
public List<GameObject> hideRootList = new List<GameObject>();
|
||
|
||
// GUI滚动位置(处理节点过多时的滚动)
|
||
private Vector2 _scrollPos;
|
||
// GUI窗口基础大小/位置(基于基准分辨率)
|
||
private Rect _baseGuiWindowRect = new Rect(20, 20, 350, 500);
|
||
private Rect _guiWindowRect; // 实际显示的窗口 Rect(自适应+3倍放大后)
|
||
|
||
// 分辨率适配配置
|
||
[Header("分辨率适配配置")]
|
||
public Vector2 baseResolution = new Vector2(1920, 1080); // 基准分辨率(参考)
|
||
[Range(0.5f, 2f)] public float scaleRatio = 1f; // 手动微调缩放比例(可选)
|
||
int baseFontSize = 25; // 基准字体大小(对应基准分辨率)
|
||
[Header("整体放大倍数")]
|
||
public float bigScaleMultiple = 3f; // 额外整体放大3倍(核心新增)
|
||
|
||
// 自适应后的样式
|
||
private GUIStyle _labelStyle;
|
||
private GUIStyle _toggleStyle;
|
||
private GUIStyle _buttonStyle;
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化分辨率自适应+3倍放大逻辑
|
||
/// </summary>
|
||
private void InitResolutionAdaptation()
|
||
{
|
||
// 1. 计算分辨率缩放比例(以宽度为基准)
|
||
float widthScale = Screen.width / baseResolution.x;
|
||
float heightScale = Screen.height / baseResolution.y;
|
||
float finalScale = Mathf.Min(widthScale, heightScale) * scaleRatio;
|
||
|
||
// 核心:叠加3倍放大系数
|
||
finalScale *= bigScaleMultiple;
|
||
|
||
// 2. 适配窗口大小和位置(分辨率适配+3倍放大)
|
||
_guiWindowRect = new Rect(
|
||
_baseGuiWindowRect.x * finalScale,
|
||
_baseGuiWindowRect.y * finalScale,
|
||
_baseGuiWindowRect.width * finalScale,
|
||
_baseGuiWindowRect.height * finalScale
|
||
);
|
||
|
||
// 3. 适配字体大小(分辨率适配+3倍放大)
|
||
int adaptedFontSize = Mathf.RoundToInt(baseFontSize * finalScale);
|
||
InitGUIStyles(adaptedFontSize);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化自适应的GUI样式(核心:适配字体大小+3倍放大)
|
||
/// </summary>
|
||
private void InitGUIStyles(int fontSize)
|
||
{
|
||
// 标签样式(节点名称)
|
||
_labelStyle = new GUIStyle(GUI.skin.label)
|
||
{
|
||
fontSize = fontSize,
|
||
normal = { textColor = Color.white },
|
||
alignment = TextAnchor.MiddleLeft
|
||
};
|
||
|
||
// 开关样式(显隐Toggle)
|
||
_toggleStyle = new GUIStyle(GUI.skin.toggle)
|
||
{
|
||
fontSize = fontSize,
|
||
normal = { textColor = Color.white },
|
||
active = { textColor = Color.yellow },
|
||
alignment = TextAnchor.MiddleCenter
|
||
};
|
||
|
||
// 按钮样式(批量操作)
|
||
_buttonStyle = new GUIStyle(GUI.skin.button)
|
||
{
|
||
fontSize = fontSize + 2, // 按钮字体稍大
|
||
normal = { textColor = Color.white },
|
||
alignment = TextAnchor.MiddleCenter
|
||
};
|
||
}
|
||
|
||
// 写 GUI 方法 控制 hideRootList 里面的节点显隐
|
||
private void OnGUI()
|
||
{
|
||
// 分辨率变化时重新适配(如窗口拉伸、切换全屏)
|
||
if (Screen.width != (int)baseResolution.x || Screen.height != (int)baseResolution.y)
|
||
{
|
||
InitResolutionAdaptation();
|
||
}
|
||
|
||
// 绘制可拖动的GUI窗口(核心容器)
|
||
_guiWindowRect = GUI.Window(0, _guiWindowRect, DrawhideRootListControlWindow, "节点显隐控制器");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绘制窗口内的控制元素(自适应+3倍放大版本)
|
||
/// </summary>
|
||
private void DrawhideRootListControlWindow(int windowId)
|
||
{
|
||
// 计算自适应+3倍放大后的控件尺寸
|
||
float resolutionScale = Mathf.Min(Screen.width / baseResolution.x, Screen.height / baseResolution.y) * scaleRatio;
|
||
float finalControlScale = resolutionScale * bigScaleMultiple;
|
||
|
||
float buttonHeight = 30 * finalControlScale;
|
||
float labelWidth = 200 * finalControlScale;
|
||
float toggleWidth = 100 * finalControlScale;
|
||
float spaceSize = 5 * finalControlScale;
|
||
float lineHeight = 1 * finalControlScale;
|
||
|
||
// 1. 批量操作按钮(顶部)
|
||
GUILayout.BeginHorizontal();
|
||
if (GUILayout.Button("全部显示", _buttonStyle, GUILayout.Height(buttonHeight)))
|
||
{
|
||
SetAllNodesActive(true);
|
||
}
|
||
if (GUILayout.Button("全部隐藏", _buttonStyle, GUILayout.Height(buttonHeight)))
|
||
{
|
||
SetAllNodesActive(false);
|
||
}
|
||
GUILayout.EndHorizontal();
|
||
|
||
// 分隔线
|
||
GUILayout.Space(spaceSize);
|
||
GUI.color = Color.gray;
|
||
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(lineHeight));
|
||
GUI.color = Color.white;
|
||
GUILayout.Space(spaceSize);
|
||
|
||
// 2. 单个节点显隐开关(滚动列表)
|
||
_scrollPos = GUILayout.BeginScrollView(_scrollPos, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
|
||
|
||
// 遍历hideRootList,为每个节点创建开关
|
||
for (int i = 0; i < hideRootList.Count; i++)
|
||
{
|
||
// 跳过空节点(避免空引用)
|
||
if (hideRootList[i] == null)
|
||
{
|
||
GUILayout.Label($"【空节点】索引:{i}", _labelStyle);
|
||
continue;
|
||
}
|
||
|
||
// 单个节点的控制行
|
||
GUILayout.BeginHorizontal();
|
||
|
||
// 节点名称显示(自适应+3倍放大)
|
||
GUILayout.Label($"节点:{hideRootList[i].name}", _labelStyle, GUILayout.Width(labelWidth));
|
||
|
||
// 显隐开关(Toggle)(自适应+3倍放大)
|
||
bool isActive = hideRootList[i].activeSelf;
|
||
bool newActive = GUILayout.Toggle(isActive, isActive ? "显示" : "隐藏", _toggleStyle, GUILayout.Width(toggleWidth));
|
||
|
||
// 开关状态变化时,更新节点显隐
|
||
if (newActive != isActive)
|
||
{
|
||
hideRootList[i].SetActive(newActive);
|
||
}
|
||
|
||
GUILayout.EndHorizontal();
|
||
|
||
// 节点间的间距(自适应+3倍放大)
|
||
GUILayout.Space(spaceSize * 0.6f);
|
||
}
|
||
|
||
GUILayout.EndScrollView();
|
||
|
||
// 3. 窗口可拖动(全屏拖动)
|
||
GUI.DragWindow(new Rect(0, 0, _guiWindowRect.width, _guiWindowRect.height));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量设置所有节点的显隐状态
|
||
/// </summary>
|
||
private void SetAllNodesActive(bool isActive)
|
||
{
|
||
foreach (var node in hideRootList)
|
||
{
|
||
if (node != null)
|
||
{
|
||
node.SetActive(isActive);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 可选:在Scene视图中可视化(方便调试)
|
||
private void OnDrawGizmos()
|
||
{
|
||
foreach (var node in hideRootList)
|
||
{
|
||
if (node != null)
|
||
{
|
||
// 显隐状态不同,Gizmos颜色不同(红色=隐藏,绿色=显示)
|
||
Gizmos.color = node.activeSelf ? Color.green : Color.red;
|
||
Gizmos.DrawWireSphere(node.transform.position, 0.5f);
|
||
}
|
||
}
|
||
}
|
||
} |