Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by takeaseat · Aug 11, 2013 at 09:12 AM · terraintile

Changing terrain in real time

Hey guys, I'll try and be specific as possible. Currently I'm looking at this extension (http://www.youtube.com/watch?v=yLi-Lp23j7Q), which seems to do exactly what I need, but the problem I have relates to updating the terrain in real time.

My goal is to have a SQL database with a table that stores the X and Y (of the grid) and the value which will correspond to what tile it would be. For example

  • 1 1 5

  • 1 2 10

On runtime, when read from the table, the tile at 1,1 will display tile 5. And then tile at 1,2 will display tile 10. But then if I were to change the the 5 to 20, for example, it will change the terrain in real time on the screen for the user.

I don't necessarily need to implement a SQL table right now. I could just read from a txt file and modify to SQL later. My main issue is just updating the tile in real time with unity.

Amazing community ;)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nastasache · Aug 11, 2013 at 09:41 PM

Hi, tenix

I'am not sure you mean about update an existing tile by some rules or about navigate on a huge number of tiles, so that below example is about create a tile.

The example is from my similar database, for a 3x3 tile:

 1,3,1325
 1,2,1337
 1,1,1335
 2,3,1325
 2,2,1333
 2,1,1329
 3,3,1326
 3,2,1331
 3,1,1327

With these values retrieved as string (see xyzDb variable), you can use a script like (JS):

 #pragma strict
 var unitsStep : float = 30.0;
 var material : Material;
 private var height : int = 3;
 private var width : int = 3;
 private var area : int = width * height;
 private var vertices = new Vector3[height * width];
 
 var xyzDb="1,3,1325\n1,2,1337\n1,1,1335\n2,3,1325\n2,2,1333\n2,1,1329\n3,3,1326\n3,2,1331\n3,1,1327"; // Records from DB
 var xzyValues = new String[area];
 
 function Start() {
 
     xzyValues = xyzDb.Split(char.Parse("\n"));    
 
     gameObject.AddComponent(MeshCollider);
     gameObject.AddComponent(MeshFilter);
     gameObject.AddComponent(MeshRenderer);
     
     MakeTerrain();
     
 }
 
 function MakeTerrain() {
 
     if (material) {
         renderer.material = material;
     } else {
         renderer.material.color = Color.white;
     }
 
     /// Clean up
     var meshCollider : MeshCollider = gameObject.GetComponent(MeshCollider);
     meshCollider.sharedMesh = null;
     
     var terrain : Mesh = GetComponent(MeshFilter).mesh;
     terrain.Clear();
 
     // Build vertices
     var vertices = new Vector3[height * width];
     var uv = new Vector2[height * width];
 
     var uvScale = Vector2 (1/unitsStep,1/unitsStep);
     
     for (var j : int = 0;  j < xzyValues.Length ;  j++){
         
         var lle=xzyValues[j];
         var xyz=lle.Split(char.Parse(","));    
 
         var x : int = parseInt(xyz[0]);
         var z : int = parseInt(xyz[1]);
         var xx = x * unitsStep;
         var zz = z * unitsStep;
 
         var e : int = parseInt(xyz[2]);
         var vertex = Vector3 (xx, e, zz);
         vertices[j] = vertex;
         uv[j] = Vector2.Scale(Vector2 (xx, zz),uvScale);
 
                                                 
     }
     
     // Assign vertices to terrain
     terrain.vertices = vertices;
     terrain.uv = uv;
 
     // Build triangle indices: 3 indices into vertex array for each triangle
     var triangles = new int[(height - 1) * (width - 1) * 6];
     var index = 0;
     for (z=0;z<height-1;z++)
     {
         for (x=0;x<width-1;x++)
         {
             // For each grid cell output two triangles
             triangles[index++] = (z     * width) + x;
             triangles[index++] = ((z+1) * width) + x;
             triangles[index++] = (z     * width) + x + 1;
             
             triangles[index++] = (z     * width) + x + 1;
             
             triangles[index++] = ((z+1) * width) + x;
             triangles[index++] = ((z+1) * width) + x + 1;
             
             
         }
     }
 
     // And assign them to the terrain
     terrain.triangles = triangles;
         
     // Auto-calculate vertex normals from the terrain
     terrain.RecalculateNormals();
     terrain.RecalculateBounds();
     terrain.Optimize();
     
     // Assign terrain mesh as collider mesh
     meshCollider.sharedMesh = terrain; 
     
 }

Note terrain in the script is not about unity Terrains; I use it for meshes. As suggestion regarding DB, for large areas it's better to you use a NoSQL database, recording for example 1_1 as key and 5 as value, 1_2 as key and 10 and so (exploding by x/z coordinates by "_"). Then, retrieve records in needed range.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image takeaseat · Aug 13, 2013 at 03:58 AM 0
Share

I'm sorry and just realized my entire question is stupid and not worded correctly.

I have developed a script that will take in X size, Y size, tile size and texture resolution. So I can define a 100x100 map with scale 1 and texture resolution of 32x32 for example. $$anonymous$$y script is actually very similar to yours.

What I am trying to do now is change a tile after it has been rendered.

So say my database looks like this:

 1,1,5
 1,2,5
 1,3,5
 1,4,5

Which would represent x = 1, y = 1, tileId = 5 and this is loaded on my screen right now, no problem. Now what I want to do is go in and change the 5 to a 6 and it will update the tile on the screen for the user, basically while they are playing. I'm newish to unity and 3d in general. This is something I did years ago in 2d with no issues. I just don't exactly know how to handle it here. Obviously I don't want to do $$anonymous$$akeTerrain() in Update() because that would probably destroy the computer lol.

avatar image nastasache · Aug 13, 2013 at 07:51 PM 0
Share

I see. You mean not about creating a terrain but replacing some tiles. Still it's unclear for me how your maps it's working and what is replacing for. If you looking for a solution to walk in very large area splitting that area in small current tiles/terrains, than I am in the same research stage (also I am not expert in Unity3D) and my approach it's a bit different. $$anonymous$$aybe the links below are useful for you, I am not sure. Of course functions like $$anonymous$$akeTerrain() have not to be called directly in Update() (for example I called it only when set of tiles are changed; meanwhile, I decided anyway not to create terrains during of play but activating/deactivating/loading already existing terrains on the scene or in Resources folder)

Infinite Terrain Free Project Source

9 tile level generation

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Batch Terrain Creation Script? 1 Answer

Tile generation problem 1 Answer

best tile size for terrain? 2 Answers

Tiled terrains - where to put, when to load? 0 Answers

Procedural Texturing on Multiple Terrain Mesh 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges