WX-Game1/Assets/Scripts/Component/AvatarComponent.cs

76 lines
1.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems; // 添加EventSystems命名空间
/// <summary>
/// 头像组件
/// </summary>
public class AvatarComponent : MonoBehaviour
{
public Button avatarButton;
private bool isClicked = false;
private void Awake()
{
avatarButton.onClick.AddListener(OpenAvatarPage);
}
private void Update()
{
// 检测空白处点击事件
HandleEmptyAreaClick();
}
/// <summary>
/// 处理空白处点击事件
/// </summary>
private void HandleEmptyAreaClick()
{
// 检测鼠标左键点击
if (Input.GetMouseButtonDown(0))
{
if (!isClicked)
return;
// 点击了空白处,触发空白处点击事件
OnEmptyAreaClick();
}
// 检测触摸点击
if (Input.touchCount > 0)
{
// 获取第一个触摸点
Touch touch = Input.GetTouch(0);
// 检查触摸阶段是否为触摸开始
if (touch.phase == TouchPhase.Began)
{
if (!isClicked)
return;
// 触摸了空白处,触发空白处点击事件
OnEmptyAreaClick();
// 输出触摸调试信息
Debug.Log($"检测到触摸空白处!触摸位置: {touch.position}, 触摸ID: {touch.fingerId}");
}
}
}
/// <summary>
/// 空白处点击事件处理
/// </summary>
private void OnEmptyAreaClick()
{
isClicked = false;
UIManager.Instance.HideUI("首页-头像");
}
private void OpenAvatarPage()
{
isClicked = true;
UIManager.Instance.ShowPage("首页-头像");
}
}