Unity3d Dynamic change of terrain

Source: Internet
Author: User

Get Terraindata directly to make changes

usingSystem.Collections;usingUnityengine;usingUnityeditor; Public classdynamiccreateterrain:monobehaviour{ PublicTerraindata Terraindata; Private float[,] heightsbackups; voidStart () {//var terrain = Createterrain ();modifyterraindataheight (Terraindata); //restore terrain after 5 secondsStartcoroutine (Disable ()); }    //Dynamically Create Terrain     PublicTerrain Createterrain () {terraindata terraindata=NewTerraindata (); Terraindata.heightmapresolution=513; Terraindata.basemapresolution=513; Terraindata.size=NewVector3 ( -, -, -); Terraindata.alphamapresolution= +; Terraindata.setdetailresolution ( +,8); Gameobject obj=Terrain.createterraingameobject (Terraindata); Assetdatabase.createasset (Terraindata,"Assets/terrain_modifyheight.asset");        Assetdatabase.saveassets (); returnObj. Getcomponent<terrain>(); }    //dynamic change of terrain     Public voidmodifyterraindataheight (Terraindata terraindata) {intwidth =Terraindata.heightmapwidth; intHeight =Terraindata.heightmapheight; float[,] array =New float[width, height]; Print ("Width:"+ width +"Height:"+height);  for(inti =0; I < width; i++)             for(intj =0; J < height; J + +)            {                floatF1 =i; floatF2 =width; floatF3 =J; floatF4 =height; floatBasev = (f1/f2 + f3/f4)/2*1; Array[i, J]= Basev *Basev; }        //Backup Height MapHeightsbackups = Terraindata.getheights (0,0, width, height); //Set Height mapTerraindata.setheights (0,0, array); } IEnumerator Disable () {yield return NewWaitforseconds (5); Debug.Log ("recove Terrain."); Terraindata.setheights (0,0, heightsbackups); }}

It is important to note that this will directly modify the Terraindata height map data, directly to the data on the hard disk also changed, if only the game effect needs to be changed in the need to backup, the end of the game to restore data.

This approach can be used to dynamically alter the terrain to achieve some effects such as digging holes.

Dynamically changing the terrain of the plugin:


Unity3d Dynamic change of terrain

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.