156 lines
3.9 KiB
C#
156 lines
3.9 KiB
C#
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
|
||
public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||
{
|
||
private Image maskImage;
|
||
private Image selectImage;
|
||
private GameObject selectedObject;
|
||
private Button button;
|
||
|
||
|
||
private bool isSelected = false;
|
||
|
||
private Color highlightColor = new Color32(0x88, 0x88, 0x88, 255); // #888888
|
||
private Color defaultColor = new Color32(0x2A, 0xFF, 0x00, 255); // #2AFF00
|
||
private byte normalAlpha = 150;
|
||
private byte hoverAlpha = 200;
|
||
private byte selectedAlpha = 255;
|
||
|
||
public bool IsLocked;
|
||
|
||
void Awake()
|
||
{
|
||
// 自动获取 UI 组件
|
||
maskImage = transform.Find("TribeIconMask/Mask")?.GetComponent<Image>();
|
||
selectImage = transform.Find("TribeIconMask/Select")?.GetComponent<Image>();
|
||
selectedObject = transform.Find("TribeIconMask/Select/Selected")?.gameObject;
|
||
|
||
// 获取同节点的 Button
|
||
button = GetComponent<Button>();
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
// 自动选中同级中第一个拥有 TribeHoverEffect 的对象
|
||
if (IsFirstTribeInParent())
|
||
{
|
||
OnClicked(); // 默认选中
|
||
}
|
||
else
|
||
{
|
||
if (!isSelected)
|
||
{
|
||
ApplyNormalState();
|
||
if (selectedObject != null)
|
||
selectedObject.SetActive(false);
|
||
}
|
||
}
|
||
|
||
// 如果有 Button,则通过代码绑定点击事件
|
||
if (button != null)
|
||
{
|
||
button.onClick.AddListener(HandleButtonClick);
|
||
}
|
||
}
|
||
|
||
private bool IsFirstTribeInParent()
|
||
{
|
||
Transform parent = transform.parent;
|
||
if (parent == null) return false;
|
||
|
||
foreach (Transform child in parent)
|
||
{
|
||
var effect = child.GetComponent<TribeHoverEffect>();
|
||
if (effect != null)
|
||
{
|
||
return child == transform; // 第一个找到的就是自己
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
// 供 Button 调用的封装
|
||
public void HandleButtonClick()
|
||
{
|
||
if(!IsLocked)
|
||
OnClicked();
|
||
}
|
||
|
||
private void OnClicked()
|
||
{
|
||
// 取消兄弟节点选中
|
||
Transform parent = transform.parent;
|
||
if (parent != null)
|
||
{
|
||
foreach (Transform child in parent)
|
||
{
|
||
if (child == transform) continue;
|
||
|
||
var effect = child.GetComponent<TribeHoverEffect>();
|
||
if (effect != null)
|
||
{
|
||
effect.Deselect();
|
||
}
|
||
}
|
||
}
|
||
|
||
// 自身设为选中
|
||
isSelected = true;
|
||
selectImage.color = highlightColor;
|
||
SetAlpha(maskImage, selectedAlpha);
|
||
if (selectedObject != null)
|
||
selectedObject.SetActive(true);
|
||
}
|
||
|
||
public void OnPointerEnter(PointerEventData eventData)
|
||
{
|
||
if (IsLocked) return;
|
||
if (!isSelected)
|
||
{
|
||
selectImage.color = highlightColor;
|
||
SetAlpha(maskImage, hoverAlpha);
|
||
}
|
||
}
|
||
|
||
public void OnPointerExit(PointerEventData eventData)
|
||
{
|
||
if (IsLocked) return;
|
||
if (!isSelected)
|
||
{
|
||
selectImage.color = defaultColor;
|
||
SetAlpha(maskImage, normalAlpha);
|
||
}
|
||
}
|
||
|
||
private void ApplyNormalState()
|
||
{
|
||
if (selectImage != null)
|
||
selectImage.color = defaultColor;
|
||
|
||
SetAlpha(maskImage, normalAlpha);
|
||
}
|
||
|
||
private void SetAlpha(Image image, byte alpha)
|
||
{
|
||
if (image == null) return;
|
||
Color color = image.color;
|
||
color.a = alpha / 255f;
|
||
image.color = color;
|
||
}
|
||
|
||
public void Deselect()
|
||
{
|
||
isSelected = false;
|
||
ApplyNormalState();
|
||
if (selectedObject != null)
|
||
selectedObject.SetActive(false);
|
||
}
|
||
|
||
public bool CheckIsSelected()
|
||
{
|
||
return isSelected;
|
||
}
|
||
}
|