Terrainheightprocesser Terrain Height Storage tool
Terrainheightdata Terrain Height Data
Class Terrainheightprocesser
Using unityengine;using system.collections;using system.io;using unityeditor;using system.collections.generic;/* * Terrain Height processor */public class terrainheightprocesser:scriptablewizard{public int mapwdith;public int mapheight;[ MenuItem ("Tools/save Terrain Height File")]public static void OpenDialog () {Displaywizard<terrainheightprocesser > ("Save Terrain Height", "Save", "Cancel"); public void Saveterrainheightfile () {//To emit rays from high altitude at intervals of Vector3 pos = VECTOR3.ZERO;POS.Y = 100; Ray Ray = new Ray (POS,-vector3.up); Vector3 Targetpos = vector3.zero;//Create an object terrainheightdata th = Scriptableobject.createinstance<terrainheightdata > (); th. Mapwidth = mapwdith;th. Mapheight = mapheight;th. List = new list<float> ();//portrait for (short i = 0; i < mapheight; i++) {pos.z = i;//landscape for (short j = 0; J < Mapwdit H J + +) {Targetpos = vector3.zero;//default zeropos.x = J;ray.origin = pos; Raycasthit result;if (Physics.raycast (ray, out result)) {targetpos = Result.point;} Th. List.add (TARGETPOS.Y);}} Calculate File Namestring scenename = unityeditor.editorapplication.currentscene;int start = Scenename.lastindexof ('/') + 1;int count = SceneNa Me. LastIndexOf ('. ') -start;scenename = scenename.substring (start, count); string path = "assets/artasset/terrainheightdata/" + Scenename + ". Asset ";//Save Assetdatabase.createasset (th, path); Assetdatabase.refresh (); string str = string. Empty;str + = "mapheight=" + mapheight.tostring () + ""; str + = "mapwdith=" + mapwdith.tostring (); Editorutility.displaydialog ("SUCCESS", "File:" + path + "\ n" + str, "OK");} void Onwizardcreate () {saveterrainheightfile ();} void Onwizardotherbutton () {Close ();}}
Terrainheightdata
Using unityengine;using system.collections;using system.io;using System.collections.generic;public class terrainheightdata:scriptableobject{[serializefield]public int mapwidth = 0;[ serializefield]public int mapheight = 0; [Serializefield]public list<float> list;public float getterrainheight (float x,float z) {int w = (int) Mathf.floor (x int h = (int) mathf.floor (z); if (W < 0 | | w >= mapwidth | | H < 0 | | h >= mapheight) return 0.0f;int idx = H*ma Pwidth+w;if (idx < 0 | | idx >= mapwidth * mapheight | | idx >= list.count) return 0.0f;return list[idx];}}
Unity3d saving and using Terrain heights