- Home /
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();
}
}
}
Your answer
Follow this Question
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