#if UNITY_EDITOR using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class CmpAreaEditor : MonoBehaviour { public Camera m_Camera; public TGS.TerrainGridSystem m_TerrainGridSystem; public int m_SelectType = 0; public Vector2Int m_CenterPos; public List m_PointList; private bool _lastIsPressedLeft = false; private bool _lastIsPressedRight = false; private int _fakeCenterCellIndex = 0; // Start is called before the first frame update void Start() { var fakeCenterPos = new Vector2Int(8, 20); m_CenterPos = fakeCenterPos; var centerCellIndex = m_TerrainGridSystem.CellGetIndex(fakeCenterPos.x, fakeCenterPos.y); m_TerrainGridSystem.CellSetColor(centerCellIndex, Color.red); _fakeCenterCellIndex = centerCellIndex; } // Update is called once per frame void Update() { { var nowIsPressed = Mouse.current.leftButton.isPressed; if (nowIsPressed && !_lastIsPressedLeft) { // 左键按下 var mousePos = Mouse.current.position.ReadValue(); // 发射射线 var ray = m_Camera.ScreenPointToRay(mousePos); // 获取碰撞点 var hitReults = Physics.RaycastAll(ray.origin, ray.direction); if (hitReults.Length > 0) { var firstHit = hitReults[0]; var cell = m_TerrainGridSystem.CellGetAtPosition(firstHit.point, true); var posInt = new Vector2Int(cell.row, cell.column); if (m_PointList.Contains(posInt)) { m_PointList.Remove(posInt); m_TerrainGridSystem.CellSetColor(cell, new Color(0, 0, 0, 0)); } else { m_PointList.Add(posInt); var cellIndex = m_TerrainGridSystem.CellGetIndex(cell.row, cell.column); if (cellIndex == _fakeCenterCellIndex) { m_TerrainGridSystem.CellSetColor(cell, Color.yellow); } else { m_TerrainGridSystem.CellSetColor(cell, Color.green); } } DebugUtil.LogError("碰撞点格子:" + cell.row + "," + cell.column); } } _lastIsPressedLeft = nowIsPressed; } { var containsCenter = false; var nowIsPressed = Mouse.current.rightButton.isPressed; if (nowIsPressed && !_lastIsPressedRight) { var combieStr = ""; for (var i = 0; i < m_PointList.Count; ++i) { var point = m_PointList[i]; var offset = point - m_CenterPos; var isLast = i == m_PointList.Count - 1; if (isLast) { combieStr += offset.x + "," + offset.y; } else { combieStr += offset.x + "," + offset.y + ","; } if (point == m_CenterPos) { containsCenter = true; } } DebugUtil.Log("偏移数据:" + (containsCenter ? "包含中心点" : "不包含中心点")); DebugUtil.Log(combieStr); } _lastIsPressedRight = nowIsPressed; } } } #endif