Skip to content

Instantly share code, notes, and snippets.

@Kattoor
Created May 16, 2016 13:50
Show Gist options
  • Save Kattoor/ffe99e410691cd0d6a2862ab13bae0b5 to your computer and use it in GitHub Desktop.
Save Kattoor/ffe99e410691cd0d6a2862ab13bae0b5 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.IO;
using System.Text;
using System;
public class TerrainHeightParser : MonoBehaviour {
private Terrain terrain;
private int hmWidth;
private int hmHeight;
private string ReadFile(string fileName)
{
try
{
StreamReader reader = new StreamReader(fileName, Encoding.Default);
return reader.ReadToEnd();
}
catch (Exception e)
{
return e.Message;
}
}
void Start()
{
terrain = Terrain.activeTerrain;
hmWidth = terrain.terrainData.heightmapWidth;
hmHeight = terrain.terrainData.heightmapHeight;
float[,] terrainHeights = terrain.terrainData.GetHeights(0, 0, hmWidth, hmHeight);
string text = ReadFile("Assets/heightmap.txt");
string[] lines = text.Split('\n');
for (int y = 0; y < lines.Length; y++)
{
string[] heights = lines[y].Split(',');
Debug.Log(heights.Length);
for (int x = 0; x < heights.Length; x++)
{
int h = 0;
try
{
h = int.Parse(heights[x]);
} catch (Exception ex)
{
Debug.Log(ex.Message + ": " + h);
}
terrainHeights[x, 63 - y] = (h != 0 ? (-h / 10000F) : 0);
}
}
terrain.terrainData.SetHeights(0, 0, terrainHeights);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment