using System; using UnityEngine; #if FUNCELL_EDITOR namespace PathEditor.Proxy.Editor #else namespace PathEditor.Proxy.Plugin #endif { //显示路径网格的脚本 [ExecuteInEditMode] public class PathItemMonoBehaviour : MonoBehaviour, IPathItemMonoInterface { public Color BlockColor = Color.red; public Color JumpColor = new Color(1, 0.58f, 0); public Color WaterColor = Color.blue; public Color GrassColor = Color.green; public Color StoneColor = Color.gray; public Color SandColor = Color.yellow; public Color MarshColor = Color.cyan; public Color WoodColor = Color.magenta; public Color SnowColor = Color.white; public Color SafeColor = Color.black; public float m_bakeHeightRangeMin = -2.0f; public float m_bakeHeightRangeMax = 50.0f; public int m_weightRadius = 1; public bool m_debugShow = false; public Color m_debugColor = Color.white; public PathGridType[,] m_mergedPathGrid = null; public PathGridType[,] m_BakePathGrid = null; public byte[,] m_mergedWeightPathGrid = null; public short[] m_heightMap = null; public int m_numberOfRows = 10; public int m_numberOfColumns = 10; public float m_cellSize = 1; private bool ShowBlockTile = true; private bool ShowWaterTile = true; private bool ShowJumpTile = true; private bool ShowGrassTile = true; private bool ShowStoneTile = true; private bool ShowSandTile = true; private bool ShowMarshTile = true; private bool ShowWoodTile = true; private bool ShowSnowTile = true; private bool ShowSafeTile = true; private bool OnlyShowBakeTile = true; private bool ShowWeightInfo = false; public bool m_showBlockInfo = true; public float m_maxRadius = 16; void Awake() { } public void DebugMessage(string msg) { } public void setTitle(bool all, bool block, bool water, bool jump, bool grass, bool stone, bool sand, bool marsh, bool wood, bool snow, bool safe, bool onlyBake) { ShowBlockTile = block; ShowWaterTile = water; ShowJumpTile = jump; ShowGrassTile = grass; ShowStoneTile = stone; ShowSandTile = sand; ShowMarshTile = marsh; ShowWoodTile = wood; ShowSnowTile = snow; ShowSafeTile = safe; OnlyShowBakeTile = onlyBake; } public void setMergeGrid(PathGridType[,] mergeGrid) { m_mergedPathGrid = mergeGrid; } public void setBakePathGrid(PathGridType[,] bakePathGrid) { m_BakePathGrid = bakePathGrid; } public void setHeightMap(short[] heightMap) { m_heightMap = heightMap; } public void setRowCol(int rows, int col) { m_numberOfRows = rows; m_numberOfColumns = col; } public void setWeightRadius(int radius) { m_weightRadius = radius; } public void setCellSize(float cellSize) { m_cellSize = cellSize; } public float getbakeHeightRangeMin() { return m_bakeHeightRangeMin; } public float getbakeHeightRangeMax() { return m_bakeHeightRangeMax; } public Color getGizmosColor(int i, int j, out bool canShow) { canShow = false; if (ShowWeightInfo && m_mergedWeightPathGrid != null) { int maxWeight = (m_weightRadius * 2 + 1) * (m_weightRadius * 2 + 1) - 1; var value = m_mergedWeightPathGrid[j, i]; float weight = value / (float)maxWeight; if (weight != 0) { canShow = true; return Color.cyan * weight; } } else { var cellType = m_mergedPathGrid[j, i]; switch (cellType) { case PathGridType.Block: case PathGridType.UserBlock: canShow = ShowBlockTile; return BlockColor; case PathGridType.Jump: canShow = ShowJumpTile; return JumpColor; case PathGridType.Water: canShow = ShowWaterTile; return WaterColor; case PathGridType.Grass: canShow = ShowGrassTile; return GrassColor; case PathGridType.Stone: canShow = ShowStoneTile; return StoneColor; case PathGridType.Sand: canShow = ShowSandTile; return SandColor; case PathGridType.Marsh: canShow = ShowMarshTile; return MarshColor; case PathGridType.Wood: canShow = ShowWoodTile; return WoodColor; case PathGridType.Snow: canShow = ShowSnowTile; return SnowColor; case PathGridType.Safe: canShow = ShowSafeTile; return SafeColor; } } return Color.white;//new Color(0f, 0f, 0f, 0f); } void OnDrawGizmos() { Vector3 start = transform.position; float hsize = m_cellSize * 0.75f; Vector3 blockSize = new Vector3(hsize, hsize / 10.0f, hsize); bool canshowTitle = false; if (Application.isPlaying) { #if !FUNCELL_EDITOR var viewer = Thousandto.Plugins.PathGrid.PathGridSystem.instance.Owner.GetLocalPlayer(); var viewPos = new Vector2(float.MaxValue, float.MaxValue); if (viewer != null && viewer.ModelTransform != null) { viewPos = viewer.Position2d; } float maxRadiusSq = m_maxRadius; if (maxRadiusSq <= 0) { maxRadiusSq = float.MaxValue; } else { maxRadiusSq *= maxRadiusSq; } Vector2 tempPos = Vector2.zero; Gizmos.color = Color.red; for (int j = 0; j < m_numberOfRows; ++j) { for (int i = 0; i < m_numberOfColumns; ++i) { Gizmos.color = getGizmosColor(i, j, out canshowTitle); float height = m_heightMap[j * m_numberOfColumns + i] / 100f; tempPos.x = start.x + i * m_cellSize + m_cellSize / 2f; tempPos.y = start.z + j * m_cellSize + m_cellSize / 2f; if (GetDistanceSq(tempPos, viewPos) < maxRadiusSq) { Vector3 pos = new Vector3(tempPos.x, height, tempPos.y); Gizmos.DrawCube(pos,blockSize); } } } #endif } else { if (m_mergedPathGrid != null) { for (int j = 0; j < m_numberOfRows; ++j) { for (int i = 0; i < m_numberOfColumns; ++i) { Gizmos.color = getGizmosColor(i, j, out canshowTitle); if (canshowTitle) { Vector3 pos = new Vector3( start.x + i * m_cellSize + m_cellSize * 0.5f, m_heightMap[j * m_numberOfColumns + i] / 100f, start.z + j * m_cellSize + m_cellSize * 0.5f); Gizmos.DrawCube(pos, blockSize); } } } } } Gizmos.color = m_debugColor; if (m_debugShow) { DebugDraw(transform.position, m_numberOfRows, m_numberOfColumns, m_cellSize, Gizmos.color); } } private float GetDistanceSq(Vector2 start, Vector2 end) { float a = (end.x - start.x); float b = (end.y - start.y); return a * a + b * b; } void DebugDraw(Vector3 origin, int numRows, int numCols, float cellSize, Color color) { float width = (numCols * cellSize); float height = (numRows * cellSize); Vector3 kZAxis = new Vector3(0.0f, 0.0f, 1.0f); Vector3 kXAxis = new Vector3(1.0f, 0.0f, 0.0f); // Draw the horizontal grid lines for (int i = 0; i < numRows + 1; i++) { Vector3 startPos = origin + i * cellSize * kZAxis; Vector3 endPos = startPos + width * kXAxis; Debug.DrawLine(startPos, endPos, color); } // Draw the vertial grid lines for (int i = 0; i < numCols + 1; i++) { Vector3 startPos = origin + i * cellSize * kXAxis; Vector3 endPos = startPos + height * kZAxis; Debug.DrawLine(startPos, endPos, color); } } } }