using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; // 点击 UI 框外部隐藏 UI 框 public class HideUIOnClickOutside : MonoBehaviour { public GameObject uiPanel; // UI 框的 GameObject private bool first = false; void Start() { uiPanel = gameObject; } void Update() { // 检查点击事件 if (Input.GetMouseButtonDown(0)) // 0是左键点击 { // 判断点击的是不是 UI 框本身或者 UI 框的子对象 if (!EventSystem.current.IsPointerOverGameObject()) { print("点击了外部"); // 如果点击的是 UI 框外部的区域,隐藏 UI 框 // uiPanel.SetActive(false); // 隐藏uiPanel的子对象 foreach (Transform child in uiPanel.transform) //遍历uiPanel的子对象 { child.gameObject.SetActive(false); } } } } }