274 lines
9.2 KiB
C#
274 lines
9.2 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.Collections;
|
||
|
||
/// <summary>
|
||
/// 多层Canvas测试工具
|
||
/// 用于测试MainPanel在多层Canvas环境下的BodyAndOrgans可见性控制
|
||
/// </summary>
|
||
public class MultiLayerCanvasTest : MonoBehaviour
|
||
{
|
||
[Header("测试Canvas")]
|
||
public Canvas[] TestCanvases;
|
||
|
||
[Header("控制按钮")]
|
||
public Button CreateOverlayButton;
|
||
public Button RemoveOverlayButton;
|
||
public Button ToggleCanvasOrderButton;
|
||
public Button DebugInfoButton;
|
||
|
||
[Header("信息显示")]
|
||
public Text StatusText;
|
||
public InputField SortingOrderInput;
|
||
|
||
private MainPanel mainPanel;
|
||
private int overlayCounter = 0;
|
||
|
||
void Start()
|
||
{
|
||
// 查找MainPanel
|
||
mainPanel = FindObjectOfType<MainPanel>();
|
||
|
||
// 设置按钮事件
|
||
if (CreateOverlayButton != null)
|
||
CreateOverlayButton.onClick.AddListener(CreateOverlayCanvas);
|
||
|
||
if (RemoveOverlayButton != null)
|
||
RemoveOverlayButton.onClick.AddListener(RemoveLastOverlay);
|
||
|
||
if (ToggleCanvasOrderButton != null)
|
||
ToggleCanvasOrderButton.onClick.AddListener(ToggleCanvasOrder);
|
||
|
||
if (DebugInfoButton != null)
|
||
DebugInfoButton.onClick.AddListener(LogAllCanvasInfo);
|
||
|
||
UpdateStatusDisplay();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建覆盖Canvas
|
||
/// </summary>
|
||
public void CreateOverlayCanvas()
|
||
{
|
||
overlayCounter++;
|
||
|
||
// 创建新的Canvas GameObject
|
||
GameObject overlayObj = new GameObject($"TestOverlay_{overlayCounter}");
|
||
Canvas overlayCanvas = overlayObj.AddComponent<Canvas>();
|
||
overlayObj.AddComponent<CanvasScaler>();
|
||
overlayObj.AddComponent<GraphicRaycaster>();
|
||
|
||
// 设置Canvas属性
|
||
overlayCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||
|
||
// 获取用户输入的sortingOrder,默认比MainPanel高
|
||
int targetOrder = 10;
|
||
if (SortingOrderInput != null && !string.IsNullOrEmpty(SortingOrderInput.text))
|
||
{
|
||
if (int.TryParse(SortingOrderInput.text, out int userOrder))
|
||
{
|
||
targetOrder = userOrder;
|
||
}
|
||
}
|
||
overlayCanvas.sortingOrder = targetOrder;
|
||
|
||
// 添加一个半透明背景来模拟覆盖效果
|
||
GameObject background = new GameObject("Background");
|
||
background.transform.SetParent(overlayObj.transform, false);
|
||
|
||
Image bgImage = background.AddComponent<Image>();
|
||
bgImage.color = new Color(0f, 0f, 0f, 0.3f); // 半透明黑色
|
||
|
||
// 设置背景填满整个屏幕
|
||
RectTransform bgRect = background.GetComponent<RectTransform>();
|
||
bgRect.anchorMin = Vector2.zero;
|
||
bgRect.anchorMax = Vector2.one;
|
||
bgRect.offsetMin = Vector2.zero;
|
||
bgRect.offsetMax = Vector2.zero;
|
||
|
||
// 添加一个文本标签
|
||
GameObject label = new GameObject("Label");
|
||
label.transform.SetParent(overlayObj.transform, false);
|
||
|
||
Text labelText = label.AddComponent<Text>();
|
||
labelText.text = $"测试覆盖层 {overlayCounter}\nSorting Order: {targetOrder}";
|
||
labelText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||
labelText.fontSize = 24;
|
||
labelText.color = Color.white;
|
||
labelText.alignment = TextAnchor.MiddleCenter;
|
||
|
||
RectTransform labelRect = label.GetComponent<RectTransform>();
|
||
labelRect.anchorMin = new Vector2(0.5f, 0.5f);
|
||
labelRect.anchorMax = new Vector2(0.5f, 0.5f);
|
||
labelRect.sizeDelta = new Vector2(300, 100);
|
||
|
||
// 添加关闭按钮
|
||
CreateCloseButton(overlayObj, overlayCanvas);
|
||
|
||
Debug.Log($"创建覆盖Canvas: {overlayObj.name}, SortingOrder: {targetOrder}");
|
||
UpdateStatusDisplay();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 为覆盖Canvas添加关闭按钮
|
||
/// </summary>
|
||
private void CreateCloseButton(GameObject canvasObj, Canvas canvas)
|
||
{
|
||
GameObject buttonObj = new GameObject("CloseButton");
|
||
buttonObj.transform.SetParent(canvasObj.transform, false);
|
||
|
||
Button closeButton = buttonObj.AddComponent<Button>();
|
||
Image buttonImage = buttonObj.AddComponent<Image>();
|
||
buttonImage.color = Color.red;
|
||
|
||
// 设置按钮位置(右上角)
|
||
RectTransform buttonRect = buttonObj.GetComponent<RectTransform>();
|
||
buttonRect.anchorMin = new Vector2(1f, 1f);
|
||
buttonRect.anchorMax = new Vector2(1f, 1f);
|
||
buttonRect.anchoredPosition = new Vector2(-50, -50);
|
||
buttonRect.sizeDelta = new Vector2(80, 40);
|
||
|
||
// 添加按钮文本
|
||
GameObject buttonText = new GameObject("Text");
|
||
buttonText.transform.SetParent(buttonObj.transform, false);
|
||
|
||
Text text = buttonText.AddComponent<Text>();
|
||
text.text = "关闭";
|
||
text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||
text.fontSize = 16;
|
||
text.color = Color.white;
|
||
text.alignment = TextAnchor.MiddleCenter;
|
||
|
||
RectTransform textRect = buttonText.GetComponent<RectTransform>();
|
||
textRect.anchorMin = Vector2.zero;
|
||
textRect.anchorMax = Vector2.one;
|
||
textRect.offsetMin = Vector2.zero;
|
||
textRect.offsetMax = Vector2.zero;
|
||
|
||
// 设置按钮点击事件
|
||
closeButton.onClick.AddListener(() => {
|
||
Debug.Log($"关闭覆盖Canvas: {canvasObj.name}");
|
||
DestroyImmediate(canvasObj);
|
||
UpdateStatusDisplay();
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 移除最后创建的覆盖层
|
||
/// </summary>
|
||
public void RemoveLastOverlay()
|
||
{
|
||
if (overlayCounter > 0)
|
||
{
|
||
GameObject overlay = GameObject.Find($"TestOverlay_{overlayCounter}");
|
||
if (overlay != null)
|
||
{
|
||
Debug.Log($"移除覆盖Canvas: {overlay.name}");
|
||
DestroyImmediate(overlay);
|
||
}
|
||
overlayCounter--;
|
||
UpdateStatusDisplay();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 切换Canvas排序顺序
|
||
/// </summary>
|
||
public void ToggleCanvasOrder()
|
||
{
|
||
Canvas[] allCanvas = FindObjectsOfType<Canvas>();
|
||
|
||
foreach (var canvas in allCanvas)
|
||
{
|
||
if (canvas.name.Contains("TestOverlay"))
|
||
{
|
||
// 随机改变测试Canvas的sortingOrder
|
||
canvas.sortingOrder = Random.Range(-5, 20);
|
||
Debug.Log($"随机调整Canvas {canvas.name} 的SortingOrder为: {canvas.sortingOrder}");
|
||
}
|
||
}
|
||
|
||
UpdateStatusDisplay();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 输出所有Canvas信息
|
||
/// </summary>
|
||
public void LogAllCanvasInfo()
|
||
{
|
||
Debug.Log("=== 多层Canvas测试 - 所有Canvas信息 ===");
|
||
|
||
Canvas[] allCanvas = FindObjectsOfType<Canvas>();
|
||
var sortedCanvas = new System.Collections.Generic.List<Canvas>(allCanvas);
|
||
sortedCanvas.Sort((a, b) => a.sortingOrder.CompareTo(b.sortingOrder));
|
||
|
||
foreach (var canvas in sortedCanvas)
|
||
{
|
||
Debug.Log($"Canvas: {canvas.name}, Order: {canvas.sortingOrder}, Active: {canvas.gameObject.activeInHierarchy}");
|
||
|
||
// 检查是否有MainPanel
|
||
if (canvas.GetComponentInChildren<MainPanel>() != null)
|
||
{
|
||
Debug.Log($" └── 包含MainPanel!");
|
||
}
|
||
|
||
// 检查其他BasePanel
|
||
var panels = canvas.GetComponentsInChildren<BasePanel>();
|
||
foreach (var panel in panels)
|
||
{
|
||
if (panel != null && panel.gameObject.activeInHierarchy)
|
||
{
|
||
Debug.Log($" └── Panel: {panel.GetType().Name}");
|
||
}
|
||
}
|
||
}
|
||
|
||
// 显示BodyAndOrgans当前状态
|
||
if (mainPanel != null && mainPanel.BodyAndOrgans != null)
|
||
{
|
||
Debug.Log($"BodyAndOrgans 当前状态: {(mainPanel.BodyAndOrgans.activeInHierarchy ? "显示" : "隐藏")}");
|
||
}
|
||
|
||
Debug.Log("=== Canvas信息结束 ===");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新状态显示
|
||
/// </summary>
|
||
private void UpdateStatusDisplay()
|
||
{
|
||
if (StatusText == null) return;
|
||
|
||
string status = "多层Canvas测试状态:\n";
|
||
|
||
Canvas[] allCanvas = FindObjectsOfType<Canvas>();
|
||
status += $"总Canvas数量: {allCanvas.Length}\n";
|
||
|
||
int overlayCount = 0;
|
||
foreach (var canvas in allCanvas)
|
||
{
|
||
if (canvas.name.Contains("TestOverlay"))
|
||
{
|
||
overlayCount++;
|
||
}
|
||
}
|
||
status += $"测试覆盖层数量: {overlayCount}\n";
|
||
|
||
if (mainPanel != null && mainPanel.BodyAndOrgans != null)
|
||
{
|
||
bool isVisible = mainPanel.BodyAndOrgans.activeInHierarchy;
|
||
status += $"BodyAndOrgans状态: {(isVisible ? "显示" : "隐藏")}\n";
|
||
}
|
||
|
||
StatusText.text = status;
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
// 每秒更新一次状态
|
||
if (Time.frameCount % 60 == 0)
|
||
{
|
||
UpdateStatusDisplay();
|
||
}
|
||
}
|
||
} |