Files
5.24BuYu/Assets/MyGame/aa/UI/Game/ChineseChess/ChessControl/UIChessCell.cs
T
2026-09-13 01:40:32 +08:00

102 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 棋盘格子控件(一个交叉点):
/// 支持两种接入方式:
/// 1. 预制体(拖到 UIChessBoard.prefab_Cell):根节点挂本脚本 + Image + Button,
/// Image 可作点击热区(透明)或格子底图(保留原样显示)
/// 2. 不拖预制体:由 UIChessBoard 代码生成(透明热区)
/// 走子提示(Hint 子物体)没有会自动创建,不拦截射线
/// </summary>
public class UIChessCell : MonoBehaviour
{
/// <summary>逻辑坐标(x 0~8,y 0~9,红方在下方;黑方视角棋盘会整体翻转)</summary>
public Vector2Int gridPos;
/// <summary>点击热区/格子底图(预制体上拖;为空自动 GetComponent)</summary>
public Image img_Click;
/// <summary>走子提示(预制体上拖;为空自动找/建名为 Hint 的子物体)</summary>
public Image img_Hint;
private Button btn_Click;
private UIChessBoard ownerBoard;
private Sprite sprite_Dot;
private Sprite sprite_Ring;
private float dotSize;
private float ringSize;
/// <summary>由 UIChessBoard 生成时调用(cellSize = 网格间距)</summary>
public void Init(UIChessBoard board, Vector2Int pos, float cellSize)
{
ownerBoard = board;
gridPos = pos;
// ---- 点击热区 ----
if (img_Click == null) img_Click = GetComponent<Image>();
if (img_Click == null)
{
img_Click = gameObject.AddComponent<Image>();
img_Click.color = new Color(1f, 1f, 1f, 0f); // 代码补的热区为全透明(预制体自带底图则保留原样)
}
img_Click.raycastTarget = true;
// ---- 走子提示 ----
if (img_Hint == null)
{
var t = transform.Find("Hint");
if (t != null) img_Hint = t.GetComponent<Image>();
if (img_Hint == null) CreateHint();
}
if (img_Hint != null)
{
img_Hint.raycastTarget = false;
img_Hint.gameObject.SetActive(false);
}
// 落子路径点固定用代码绘制的圆点/圆圈(颜色已烘焙)
sprite_Dot = ChessArtFactory.GetHintDotSprite();
sprite_Ring = ChessArtFactory.GetCaptureRingSprite();
dotSize = cellSize * 0.45f;
ringSize = cellSize * 1.12f;
// ---- 按钮 ----
btn_Click = GetComponent<Button>();
if (btn_Click == null)
{
btn_Click = gameObject.AddComponent<Button>();
btn_Click.transition = Selectable.Transition.None;
}
btn_Click.onClick.AddListener(() => ownerBoard.OnCellClicked(this));
}
/// <summary>显示走子提示:isCapture=false 走子点 / true 吃子圈(自定义图不染色)</summary>
public void ShowHint(bool isCapture)
{
if (img_Hint == null) return;
img_Hint.gameObject.SetActive(true);
img_Hint.sprite = isCapture ? sprite_Ring : sprite_Dot;
img_Hint.color = Color.white; // 默认图的绿/红已烘焙在图里;自定义图保持原色
img_Hint.rectTransform.sizeDelta = isCapture
? new Vector2(ringSize, ringSize)
: new Vector2(dotSize, dotSize);
}
public void HideHint()
{
if (img_Hint != null) img_Hint.gameObject.SetActive(false);
}
private void CreateHint()
{
var hgo = new GameObject("Hint", typeof(RectTransform), typeof(Image));
var hrt = (RectTransform)hgo.transform;
hrt.SetParent(transform, false);
hrt.anchorMin = hrt.anchorMax = new Vector2(0.5f, 0.5f);
hrt.anchoredPosition = Vector2.zero;
img_Hint = hgo.GetComponent<Image>();
}
}