Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nerdares · Mar 08, 2016 at 09:05 AM · cameraterrainloadingdynamicdynamic loading

Dynamic terrain loading on one scene

Ok so I am trying to dynamically load the terrain based off where the camera is supposed to be.

In my scene, I have a 4x4 terrain, resulting in 16 tiles. It is important to note that my following code works for any terrain as long as:

the square root of the total amount of terrain tiles, divided by 4 leaves no remainder. Here is the code below that stores the terrains (placed in order) from a 1d array list, into a 2d array. Then this code sets the neighbor for each terrain.

 using UnityEngine;
 using System.Collections;
 
 public class TerrainLoader : MonoBehaviour
 {
     [HideInInspector]
     public Terrain[] TerrainList;
 
     [HideInInspector]
     public Terrain[,] TerrainGrid;
 
 
 
 
     void Start()
     {
         TerrainList = GetComponentsInChildren<Terrain>();
         if (Mathf.Sqrt(TerrainList.Length) % 4 == 0 || TerrainList.Length == 4)
         {
             TerrainGrid = new Terrain[(int)Mathf.Sqrt(TerrainList.Length), (int)Mathf.Sqrt(TerrainList.Length)];
             print("Will proceed -> amount of terrain tiles are divisiable by 4 and are square roots");
             AssignTerrains();
         }
         else
         {
             print("Cannot proceed. The amount of terrain's entered are not a multiple of 4 and is a squared number");
 
         }
 
     }
 
     void AssignTerrains()
     {
         int count = 0;
         for (int row = 0; row < TerrainGrid.GetLength(0); row++)
         {
 
             for (int col = 0; col < TerrainGrid.GetLength(1); col++)
             {
 
                 TerrainGrid[row, col] = TerrainList[count];
                 #region print statement
                 /*
                 print("On row " + row);
                 print("On column " + col);
                 print("We put in " + TerrainGrid[row, col].name);
                 print("--------------------------------");
                 */
                 #endregion
                 count++;
             }
         }
 
         SetNeighbors();
 
     }
 
     void SetNeighbors()
     {
 
         for (int row = 0; row < TerrainGrid.GetLength(0); row++)
         {
             for (int col = 0; col < TerrainGrid.GetLength(1); col++)
             {
 
                 Terrain Left = (col == 0) ? null : TerrainGrid[row, col - 1];
                 Terrain Top = (row == 0) ? null : TerrainGrid[row - 1, col];
                 Terrain Right = (col + 1 == TerrainGrid.GetLength(1)) ? null : TerrainGrid[row, col + 1];
                 Terrain Bottom = (row + 1 == TerrainGrid.GetLength(0)) ? null : TerrainGrid[row + 1, col];
 
                 TerrainGrid[row, col].SetNeighbors(Left, Top, Right, Bottom);
 
                 #region Print statement checks
                 /*
                 string txtLeft = (Left == null) ? "Nothing" : Left.name;
                 string txtTop = (Top == null) ? "Nothing" : Top.name;
                 string txtRight = (Right == null) ? "Nothing" : Right.name;
                 string txtBottom = (Bottom == null) ? "Nothing" : Bottom.name;
 
                 print("Using terrain " + TerrainGrid[row, col].name);
                 print("The left of this terrain is " + txtLeft);
                 print("The Top of this terrain is " + txtTop);
                 print("The Right of this terrain is " + txtRight);
                 print("The Bottom of this terrain is " + txtBottom);
                 print("------------------------------------------------");
                 */
                 #endregion
 
             } // end col
         }
 
     }
 
 
 
 }


Ok so all of that works. The terrains are stored in a 2d array and have their neighbors set. Now what do i do in order to have the camera load these terrains based off what it is? I image it would be something like creating a script for the camera and the code would be something like:

 Camera mainCam;
 TerrainLoader t1 = new TerrainLoader();
 for (int row = 0; row < t1.TerrainGrid.GetLength(0); row++)
          {
  
              for (int col = 0; col < t1.TerrainGrid.GetLength(1); col++)
              {
  
                  if(mainCam.transform.position == t1.TerrainGrid[row , col])
                  {
                              t1.TerrainGrid[row , col].load();
                  }
 
              }
          }




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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What size terrain for dynamic terrain loading? 0 Answers

How can i use Dynamic Terrain Loading? 1 Answer

Terrain Chunk loading 1 Answer

Distant terrain level of detail question. 0 Answers

resolution of textures with camera distance? 0 Answers


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