Below is the code I am currently building.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//add threading so we can thread the rendering..
using System.Threading;
namespace MapEditor
{
public class MapBlock : IEquatable<MapEditor.MapBlock>{
private Transform _transform;
private Vector3 _position;
public Transform transform{
get{return _transform; }
set{_transform = value; }
}
public Vector3 position{
get{return _position; }
set{_position = value; }
}
public bool Equals (MapBlock other)
{
throw new NotImplementedException ();
}
public enum BlockType : byte
{
Ground= 0,
Stone = 1,
Water = 99
}
}
}
using UnityEngine;
using System.Collections;
using MapEditor;
public class UserInputTerrain : MonoBehaviour {
//private float _clickdelay = 0.5f;
//private float _nextclick = 0.0f;
private ModifyTerrain _modifyterrain;
private GenerateTerrain _generateterrain;
void Awake () {
//Get ref to terrain modification functions.
_modifyterrain = transform.root.GetComponent<ModifyTerrain>();
_generateterrain = transform.root.GetComponent<GenerateTerrain>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.G))
{
_generateterrain.BuildBaseTerrain();
}
if (Input.GetKeyUp(KeyCode.D))
{
_generateterrain.ClearTerrain();
}
}
}
using System.Collections;
namespace MapEditor{
public class GenerateTerrain : MonoBehaviour {
private WorldObjectManager _worldobjectmanager;
private Transform _gameparent;
private Transform _transform;
// Use this for initialization
void Start () {
}
void Awake(){
//optimizations..
_worldobjectmanager = transform.root.GetComponent<WorldObjectManager>();
_gameparent = transform.FindChild("Map");
_transform = transform;
}
// Update is called once per frame
void Update () {
}
public void BuildBaseTerrain(){
Debug.Log("TerrainGeneration");
int size = _worldobjectmanager.MapSize;
for (int i=0; i<size; i++)
{
for (int j=0; j<size; j++)
{
Transform clone = Instantiate(_worldobjectmanager.PrefabBaseBlock, transform.position,transform.rotation) as Transform;
clone.transform.position = transform.position + new Vector3 (i * _worldobjectmanager.MapOffSet,0, j * _worldobjectmanager.MapOffSet);
clone.parent = _gameparent;
MapBlock myblock = new MapBlock();
myblock.transform = clone;
//myblock.transform.tag = "mapblock";
//may use in the future.. not yet...
//Vector3 BlockPos = _transform.position + new Vector3 (i * _worldobjectmanager.MapOffSet,0, j * _worldobjectmanager.MapOffSet);
//MapBlock myblock = new MapBlock();
//myblock.position = BlockPos;
_worldobjectmanager.MapBlocks.Add(myblock);
}
}
}
public void ClearTerrain(){
Debug.Log("ClearingTerrain");
foreach (MapBlock myblock in _worldobjectmanager.MapBlocks)
{
Destroy(myblock.transform.gameObject);
}
_worldobjectmanager.MapBlocks.Clear();
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace MapEditor {
public class WorldObjectManager : MonoBehaviour {
// put it here so it changes easily between all classes.
public Transform PrefabBaseBlock;
public int MapSize = 100;
public float MapOffSet = 5;
public List<MapBlock> MapBlocks = new List<MapBlock>();
// Use this for initialization
void Start () {
}
void Awake(){
}
// Update is called once per frame
void Update () {
}
}
}
Ok, so it works, but my problem is when I generate large maps…
they seem to generate to slow and freeze up the unity engine.
Oh ya ignore the using threading…
I was thinking that might help but then I really was lost when it came to figuring out what to actually thread since I am using the unity engine scripting system and the game engine is doing most of the heavy lifting for me.
I am wondering if anyone has any ideas to help me optimize the code or change the way it generates…
Thanks!