``` docs(readme): 更新文档说明 - 添加项目使用指南 - 完善API接口说明 - 修正错误的配置示例 ``` 注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 部署格位高亮:拖动卡牌时在目标格位显示一块发光地板。
|
|
/// 绿色 = 可以放置;红色 = 该格已被占用 / 不可放置。
|
|
/// 全场只需要一个,由 DefenseGameFlowController 在布阵阶段创建。
|
|
/// </summary>
|
|
public class DeployHighlight : MonoBehaviour
|
|
{
|
|
public static DeployHighlight Instance { get; private set; }
|
|
|
|
private static readonly Color FreeColor = new Color(0.25f, 1f, 0.45f, 0.55f);
|
|
private static readonly Color BlockedColor = new Color(1f, 0.28f, 0.28f, 0.55f);
|
|
|
|
private GameObject plate;
|
|
private Material plateMaterial;
|
|
private int shownCell = -1;
|
|
|
|
/// <summary>高亮当前是否可见。</summary>
|
|
public bool IsVisible => plate != null && plate.activeSelf;
|
|
|
|
/// <summary>当前显示的是否是"不可放置"(红色)。</summary>
|
|
public bool IsBlocked { get; private set; }
|
|
|
|
/// <summary>当前高亮所在的格位,-1 表示没有。</summary>
|
|
public int ShownCell => shownCell;
|
|
|
|
public static DeployHighlight Ensure()
|
|
{
|
|
if (Instance != null) return Instance;
|
|
|
|
var go = new GameObject("DeployHighlight");
|
|
Instance = go.AddComponent<DeployHighlight>();
|
|
Instance.Build();
|
|
return Instance;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
Build();
|
|
}
|
|
}
|
|
|
|
private void Build()
|
|
{
|
|
if (plate != null) return;
|
|
|
|
plate = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
|
plate.name = "CellPlate";
|
|
plate.transform.SetParent(transform, false);
|
|
plate.transform.localRotation = Quaternion.identity;
|
|
plate.transform.localScale = new Vector3(DeployGrid.CellWidth * 0.96f, 0.02f, DeployGrid.CellDepth * 0.96f);
|
|
|
|
// 高亮只是视觉提示,不需要碰撞
|
|
var collider = plate.GetComponent<Collider>();
|
|
if (collider != null)
|
|
{
|
|
Destroy(collider);
|
|
}
|
|
|
|
var shader = Shader.Find("Sprites/Default") ?? Shader.Find("Unlit/Color");
|
|
plateMaterial = new Material(shader);
|
|
plateMaterial.name = "DeployHighlightMat";
|
|
plateMaterial.color = FreeColor;
|
|
|
|
var renderer = plate.GetComponent<Renderer>();
|
|
renderer.sharedMaterial = plateMaterial;
|
|
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
renderer.receiveShadows = false;
|
|
|
|
plate.SetActive(false);
|
|
}
|
|
|
|
/// <summary>在指定格位显示高亮。blocked = true 显示红色。</summary>
|
|
public void Show(int cellIndex, bool blocked)
|
|
{
|
|
if (plate == null) Build();
|
|
if (!DeployGrid.IsValidIndex(cellIndex))
|
|
{
|
|
Hide();
|
|
return;
|
|
}
|
|
|
|
var pos = DeployGrid.CellPosition(cellIndex);
|
|
plate.transform.position = new Vector3(pos.x, DeployGrid.GridY + 0.05f, pos.z);
|
|
plateMaterial.color = blocked ? BlockedColor : FreeColor;
|
|
IsBlocked = blocked;
|
|
|
|
if (shownCell != cellIndex || !plate.activeSelf)
|
|
{
|
|
plate.SetActive(true);
|
|
shownCell = cellIndex;
|
|
}
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
if (plate != null && plate.activeSelf)
|
|
{
|
|
plate.SetActive(false);
|
|
}
|
|
shownCell = -1;
|
|
IsBlocked = false;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (Instance == this) Instance = null;
|
|
}
|
|
}
|