So I am re-tooling a “rogue like” into a random dungeon generating game, a very fun project. I have access to a type enum representation of a game map, like tiles, and monsters, and player. I take that information and draw out unity3d in simple cubes, scaled * 10. This is all working as intended. The issue is that the frames are extremely low. I have seen many of those Cube World type and they have a ton more cubes drawn then I do. So my question is how do I get the draw calls down. I have a little over 150 cubes being drawn at a time currently. I need to get that low so I can start spawning a ton of monsters. I do handle everything internally, like the maps, gameObjects, Materials, ect. Any one have an approach to take? Here is my main code, this is the dungeon manager, its job is to know about all of the dungeons structures, and initiate objects.
void Awake()
{
//start rogue like
SlashNetGame.Initialise();
//tell tiler where to start
StartingTileLocation = Vector3.zero;
//assign the rogue like game
SlashNetGame game = SlashNetGame.NewGame(DungeonStyle.Hack, 20, 80, 80);
// set local
_SNDungeon = game.Dungeon;
// custom loader for game objects and materials by enum type.
LoadTypesAsGameObjects<TileType>(ref TileGameObjects, ref _tileMaterials);
LoadTypesAsGameObjects<MonsterClass>(ref SpawnPointGameObjects, ref _spawnPointMaterials);
// load the player
LoadPlayer(ref Player);
// populate the dungeon, and set up the unity locations
Populate(DungeonContainer, SpawnpointContainer);
// dont need _SNDungeon, null it.
_SNDungeon = null;
}
private void LoadPlayer(ref GameObject Player)
{
Debug.Log("Loading Player");
Player = Resources.Load("Prefabs/Player") as GameObject;
if (Player == null)
{
Debug.LogError("Player did not load");
return;
}
}
// send in the enum type you want loaded, a referance to their gameObject arrary, and a refrance to their //materials array see the paths below.
private void LoadTypesAsGameObjects<T>(ref GameObject[] gameObjects, ref Material[] materials)
{
// Initialize the arrays
Type t = typeof(T);
if (!t.IsEnum)
Debug.LogError("Type is not enum");
var names = Enum.GetNames(t);
var values = Enum.GetValues(t);
gameObjects = new GameObject[values.Length];
materials = new Material[values.Length];
// load the game object, and apply the correct material
for (int i = 0; i < values.Length; i++)
{
Debug.Log("Loading " + names[i].ToString());
// notice the path is Resources/Prefabs/ (the name of the gameobject, so name it the same as the enum)
gameObjects[i] = Resources.Load("Prefabs/" + names[i].ToString()) as GameObject;
gameObjects[i].AddComponent<Rigidbody>();
gameObjects[i].rigidbody.isKinematic = true;
// notice the path is Resources/Graphics/Materials/ (the name of the gameobject, so name it the same as the enum)
materials[i] = Resources.Load("Graphics/Materials/" + names[i].ToString()) as Material;
if (gameObjects[i] == null || materials[i] == null)
{
Debug.LogError(names[i].ToString() + " Failed to load");
return;
}
else
{
// apply material and name
gameObjects[i].renderer.material = materials[i];
gameObjects[i].name = names[i].ToString();
}
}
}
// now load the dungeon, and their pointers
void Populate(GameObject dungeon, GameObject spawnPoints)
{
dungeon = new GameObject();
dungeon.name = "Dungeon";
spawnPoints = new GameObject();
spawnPoints.name = "Spawn Points";
for (int j = 0; j < GenericDungeon.HEIGHT; j++)
{
for (int i = 0; i < GenericDungeon.WIDTH; i++)
{
if (SlashNetGame.Instance.PCPosition.X == i SlashNetGame.Instance.PCPosition.Y == j)
{
(Instantiate(Player, GetTilePosition(i, j), Quaternion.identity) as GameObject).transform.position += Vector3.up * 11f;
}
//Debug.Log((int)_Dungeon[i, j].Type);
(Instantiate(TileGameObjects[(int)_SNDungeon[i, j].Type], GetTilePosition(i, j), Quaternion.identity) as GameObject).transform.parent = dungeon.transform;
if (_SNDungeon[i, j].Monster != null)
{
GameObject temp = Instantiate(SpawnPointGameObjects[(int)_SNDungeon[i, j].Type], GetTilePosition(i, j), Quaternion.identity) as GameObject;
temp.transform.position += Vector3.up * 1f;
temp.transform.parent = spawnPoints.transform;
}
}
}
}
private Vector3 GetTilePosition(int j, int i)
{
return new Vector3(StartingTileLocation.x + (i * _tileWidth), StartingTileLocation.y, StartingTileLocation.z + (j * _tileWidth));
}