146 lines
4.8 KiB
C#
146 lines
4.8 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 快速Canvas层级测试工具
|
|||
|
|
/// 可以直接添加到场景中进行快速测试
|
|||
|
|
/// </summary>
|
|||
|
|
public class QuickCanvasTest : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Header("快速测试")]
|
|||
|
|
[SerializeField] private Button testButton;
|
|||
|
|
[SerializeField] private Text statusText;
|
|||
|
|
|
|||
|
|
private int testCanvasCount = 0;
|
|||
|
|
private MainPanel mainPanel;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
mainPanel = FindObjectOfType<MainPanel>();
|
|||
|
|
|
|||
|
|
if (testButton == null)
|
|||
|
|
{
|
|||
|
|
CreateTestButton();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (testButton != null)
|
|||
|
|
{
|
|||
|
|
testButton.onClick.AddListener(QuickTest);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
UpdateStatus();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CreateTestButton()
|
|||
|
|
{
|
|||
|
|
// 创建测试按钮
|
|||
|
|
GameObject buttonObj = new GameObject("QuickTestButton");
|
|||
|
|
buttonObj.transform.SetParent(transform, false);
|
|||
|
|
|
|||
|
|
testButton = buttonObj.AddComponent<Button>();
|
|||
|
|
Image buttonImg = buttonObj.AddComponent<Image>();
|
|||
|
|
buttonImg.color = Color.cyan;
|
|||
|
|
|
|||
|
|
RectTransform buttonRect = buttonObj.GetComponent<RectTransform>();
|
|||
|
|
buttonRect.sizeDelta = new Vector2(200, 50);
|
|||
|
|
buttonRect.anchoredPosition = new Vector2(0, 100);
|
|||
|
|
|
|||
|
|
// 添加按钮文字
|
|||
|
|
GameObject textObj = new GameObject("Text");
|
|||
|
|
textObj.transform.SetParent(buttonObj.transform, false);
|
|||
|
|
|
|||
|
|
Text buttonText = textObj.AddComponent<Text>();
|
|||
|
|
buttonText.text = "快速测试覆盖";
|
|||
|
|
buttonText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
|||
|
|
buttonText.fontSize = 16;
|
|||
|
|
buttonText.color = Color.black;
|
|||
|
|
buttonText.alignment = TextAnchor.MiddleCenter;
|
|||
|
|
|
|||
|
|
RectTransform textRect = textObj.GetComponent<RectTransform>();
|
|||
|
|
textRect.anchorMin = Vector2.zero;
|
|||
|
|
textRect.anchorMax = Vector2.one;
|
|||
|
|
textRect.offsetMin = Vector2.zero;
|
|||
|
|
textRect.offsetMax = Vector2.zero;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void QuickTest()
|
|||
|
|
{
|
|||
|
|
testCanvasCount++;
|
|||
|
|
|
|||
|
|
// 创建一个简单的覆盖Canvas
|
|||
|
|
GameObject overlayObj = new GameObject($"QuickTest_{testCanvasCount}");
|
|||
|
|
Canvas overlay = overlayObj.AddComponent<Canvas>();
|
|||
|
|
overlay.renderMode = RenderMode.ScreenSpaceOverlay;
|
|||
|
|
overlay.sortingOrder = 10; // 确保覆盖MainPanel
|
|||
|
|
|
|||
|
|
overlayObj.AddComponent<GraphicRaycaster>();
|
|||
|
|
|
|||
|
|
// 添加半透明背景
|
|||
|
|
GameObject bg = new GameObject("Background");
|
|||
|
|
bg.transform.SetParent(overlayObj.transform, false);
|
|||
|
|
|
|||
|
|
Image bgImg = bg.AddComponent<Image>();
|
|||
|
|
bgImg.color = new Color(1f, 0f, 0f, 0.3f); // 半透明红色
|
|||
|
|
|
|||
|
|
RectTransform bgRect = bg.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 = $"测试覆盖层 {testCanvasCount}\n点击关闭";
|
|||
|
|
labelText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
|||
|
|
labelText.fontSize = 32;
|
|||
|
|
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(400, 200);
|
|||
|
|
|
|||
|
|
// 添加点击关闭功能
|
|||
|
|
Button closeBtn = bg.AddComponent<Button>();
|
|||
|
|
closeBtn.onClick.AddListener(() => {
|
|||
|
|
Debug.Log($"关闭快速测试Canvas: {overlayObj.name}");
|
|||
|
|
Destroy(overlayObj);
|
|||
|
|
UpdateStatus();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
Debug.Log($"创建快速测试Canvas,SortingOrder: {overlay.sortingOrder}");
|
|||
|
|
UpdateStatus();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UpdateStatus()
|
|||
|
|
{
|
|||
|
|
if (statusText != null)
|
|||
|
|
{
|
|||
|
|
string status = "Canvas测试状态:\n";
|
|||
|
|
|
|||
|
|
if (mainPanel != null && mainPanel.BodyAndOrgans != null)
|
|||
|
|
{
|
|||
|
|
bool visible = mainPanel.BodyAndOrgans.activeInHierarchy;
|
|||
|
|
status += $"模型状态: {(visible ? "显示" : "隐藏")}\n";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Canvas[] allCanvas = FindObjectsOfType<Canvas>();
|
|||
|
|
status += $"Canvas总数: {allCanvas.Length}";
|
|||
|
|
|
|||
|
|
statusText.text = status;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
// 每秒更新状态
|
|||
|
|
if (Time.frameCount % 60 == 0)
|
|||
|
|
{
|
|||
|
|
UpdateStatus();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|