If you have a grid of points to make triangles, you cant “display a number to represent the terrain height”, becuase the triangle can have 3 different heights at its vertex, so im not sure what you want for that
Plus, you’ve added a second post with more details which I haven’t worked into my code
Heres a basic interface that overlays a grid and lets you adjust heights with mouse clicks
The grid isn’t infinate, you’d probably have to use a dictionary to track what cells are modified and assume the rest are blank
//Assets/Editor/MapEdit.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
public class MapEdit : EditorWindow {
#region Init
[MenuItem( "EDITORS/Tile Editor" )]
static void Init () {
MapEdit window = (MapEdit) EditorWindow.GetWindow( typeof( MapEdit ) );
window.Show();
window.position = new Rect( 20, 80, 300, 150 );
}
void OnFocus () {
SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
SceneView.onSceneGUIDelegate += this.OnSceneGUI;
}
void OnDestroy () {
SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
}
#endregion
#region Variables
int[,] _heights;
int[,] heights {
get {
if ( _heights == null ) {
_heights = new int[sizeX, sizeZ];
}
return _heights;
}
set {
_heights = value;
}
}
float cellSize = 1f, heightStep = 0.25f;
int sizeX = 21, sizeZ = 21;
int drawSize;
Plane worldPlane = new Plane( Vector3.up, Vector3.zero );
float rayDistance;
int mouseWorldPosXUnclamped, mouseWorldPosZUnclamped, mouseWorldPosX, mouseWorldPosZ;
#endregion
#region Scene Controls
void OnSceneGUI ( SceneView sceneView ) {
Event e = Event.current;
Ray mouseRay = HandleUtility.GUIPointToWorldRay( e.mousePosition );
if ( ScreenContainsMouse( e.mousePosition ) && worldPlane.GetSide( Camera.current.transform.position ) ) {
if ( worldPlane.Raycast( mouseRay, out rayDistance ) ) {
Vector3 mousePos = mouseRay.GetPoint( rayDistance );
mouseWorldPosXUnclamped = Mathf.RoundToInt( mousePos.x / cellSize );
mouseWorldPosZUnclamped = Mathf.RoundToInt( mousePos.z / cellSize );
mouseWorldPosX = Mathf.Clamp( mouseWorldPosXUnclamped, 0, heights.GetLength( 0 ) - 1 );
mouseWorldPosZ = Mathf.Clamp( mouseWorldPosZUnclamped, 0, heights.GetLength( 1 ) - 1 );
for ( int x = mouseWorldPosX - drawSize; x < mouseWorldPosX + drawSize + 1; x++ ) {
for ( int z = mouseWorldPosZ - drawSize; z < mouseWorldPosZ + drawSize + 1; z++ ) {
if ( x < 0 || x > heights.GetLength( 0 ) - 1 || z < 0 || z > heights.GetLength( 1 ) - 1 )
continue;
Vector3 gridPos = new Vector3( x * cellSize, 0, z * cellSize );
Vector3 terrainPos = new Vector3( x * cellSize, heights[x, z] * heightStep + 1, z * cellSize );
Handles.Label( gridPos, "" + heights[x, z] );
Handles.color = Color.red;
Handles.DrawLine( gridPos, terrainPos );
Handles.color = Color.black;
if ( x < heights.GetLength( 0 ) - 1 && z < heights.GetLength( 1 ) - 1 && x < mouseWorldPosX + drawSize && z < mouseWorldPosZ + drawSize ) {
Vector3 terrainPosPlusX = new Vector3( ( x + 1 ) * cellSize, heights[x + 1, z] * heightStep + 1, z * cellSize );
Vector3 terrainPosPlusZ = new Vector3( x * cellSize, heights[x, z + 1] * heightStep + 1, ( z + 1 ) * cellSize );
Vector3 terrainPosPlusXZ = new Vector3( ( x + 1 ) * cellSize, heights[x + 1, z + 1] * heightStep + 1, ( z + 1 ) * cellSize );
Handles.DrawLine( terrainPos, terrainPosPlusX );
Handles.DrawLine( terrainPosPlusX, terrainPosPlusXZ );
Handles.DrawLine( terrainPosPlusXZ, terrainPosPlusZ );
Handles.DrawLine( terrainPosPlusZ, terrainPos );
}
}
}
if ( GridContainsMouse() ) {
Handles.color = Color.red;
Handles.DrawWireDisc( new Vector3( mouseWorldPosX * cellSize, 0, mouseWorldPosZ * cellSize ), Vector3.up, cellSize * 0.75f );
Handles.color = Color.green;
Handles.DrawWireDisc( new Vector3( mouseWorldPosX * cellSize, heights[mouseWorldPosX, mouseWorldPosZ] * heightStep, mouseWorldPosZ * cellSize ), Vector3.up, cellSize * 0.75f );
}
if ( e.type == EventType.MouseDown && e.button == 0 && GridContainsMouse() ) {
heights[mouseWorldPosX, mouseWorldPosZ]++;
}
if ( e.type == EventType.MouseDown && e.button == 1 && GridContainsMouse() ) {
heights[mouseWorldPosX, mouseWorldPosZ]--;
}
}
}
sceneView.Repaint();
}
#endregion
#region GUI Controls
void OnGUI () {
cellSize = EditorGUILayout.FloatField( "Cell Size", cellSize );
heightStep = EditorGUILayout.FloatField( "Cell Height Step", heightStep );
sizeX = Mathf.Max( 1, EditorGUILayout.IntField( "Size X", sizeX ) );
sizeZ = Mathf.Max( 1, EditorGUILayout.IntField( "Size Z", sizeZ ) );
drawSize = Mathf.Clamp( EditorGUILayout.IntField( "Number Drawn", drawSize ), 1, Mathf.Min( sizeX, sizeZ ) / 2 );
}
#endregion
#region Helper Functions
bool ScreenContainsMouse ( Vector3 v ) {
return v.x > 0 && v.x < Screen.width && v.y > 0 && v.y < Screen.height;
}
bool GridContainsMouse () {
return mouseWorldPosXUnclamped > -1 && mouseWorldPosXUnclamped < heights.GetLength( 0 ) && mouseWorldPosZUnclamped > -1 && mouseWorldPosZUnclamped < heights.GetLength( 1 );
}
#endregion
}