Unity3d saving and using Terrain heights

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.