using UnityEditor; using UnityEngine; namespace Game { public class GridCellView : MonoBehaviour { private GridCell _cell; private float _size; private GameObject target; public bool IsOccupied => _cell.IsOccupied; public void Init(GridCell cell, float size) { _cell = cell; _size = size; transform.position = new Vector3((cell.X) * size, 0, (cell.Y) * size); transform.localScale = new Vector3(_size, _size, _size); } public bool Put(GameObject go) { if (target == null) { if (go != null) { target = go; target.transform.position = transform.position; } _cell.Enter(); return true; } return false; } public GameObject Take() { target = null; var go = target; _cell.Exit(); return go; // if (target != null) // { // target = null; // var go = target; // _cell.Exit(); // return go; // } // // return null; } public void DrawGizmos() { if (_cell.IsOccupied) Gizmos.color = Color.red; else Gizmos.color = Color.green; // 获取物体的位置 Vector3 position = transform.position; // 计算方框的四个顶点 Vector3 topLeft = position + new Vector3(-_size / 2, 0f, _size / 2); Vector3 topRight = position + new Vector3(_size / 2, 0f, _size / 2); Vector3 bottomLeft = position + new Vector3(-_size / 2, 0f, -_size / 2); Vector3 bottomRight = position + new Vector3(_size / 2, 0f, -_size / 2); // 绘制方框的四条边 Gizmos.DrawLine(topLeft, topRight); Gizmos.DrawLine(topRight, bottomRight); Gizmos.DrawLine(bottomRight, bottomLeft); Gizmos.DrawLine(bottomLeft, topLeft); #if UNITY_EDITOR if (_cell.IsOccupied) { Handles.Label(transform.position, "有"); } else { Handles.Label(transform.position, "无"); } #endif } } }