_xiaofang/xiaofang/Assets/Res/gsj/scripts/figurestate.cs
2024-12-20 10:10:19 +08:00

67 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class figurestate : MonoBehaviour
{
public bool isAI;
public delegate void FigureStateChanged(bool newState, figurestate sender);
public static event FigureStateChanged OnFigureStateChanged;
private Button button; // 按钮组件
private GameObject image;
void Start()
{
// 获取当前挂载物体的 Button 组件
button = GetComponent<Button>();
image=transform.Find("Image").gameObject;
//.SetActive(false);
if (button != null)
{
button.onClick.AddListener(OnButtonClicked);
}
else
{
Debug.LogError($"{gameObject.name} 上缺少 Button 组件!");
}
}
void Update()
{
// 检测是否点击了其他地方,隐藏高光栏
//if (Input.GetMouseButtonDown(0))
//{
// // 如果点击不是在按钮上,隐藏高光栏
// if (!IsMouseOverButton())
// {
// ToggleHighlightBar(false);
// }
//}
}
// 判断鼠标是否点击了按钮
bool IsMouseOverButton()
{
RectTransform rectTransform = button.GetComponent<RectTransform>();
return rectTransform.rect.Contains(Input.mousePosition);
}
// 当按钮被点击时
void OnButtonClicked()
{
// 当按钮被点击时,显示高光栏
//ToggleHighlightBar(true);
// 触发事件,通知监听者状态发生变化
OnFigureStateChanged?.Invoke(isAI, this);
Debug.Log($"{gameObject.name} clicked. isAI = {isAI}");
}
// 切换高光栏显示状态
void ToggleHighlightBar(bool show)
{
image.SetActive(show);
}
}