2
\$\begingroup\$

Im not a programmer, but somehow like trying to make something, almost like a hobby. As of now i have made a list of different gameobjects (models) and i need to choose right one from that list. Everything works but i would like to see how someone who undestands what they'r doing could change the choosing part.


So first is my tile that that hold information:

using System;
using UnityEngine;

[Serializable]
public class Tiles {

public string Name;
public Type TileType;
public Vector3 Cordinates;
public GameObject Tile;

public Tiles()
{
    Name = "Unset";
    TileType = Type.Unset;
    Cordinates = new Vector3(0, 0, 0);
    Tile = null;
}

public Tiles(string name, Type type, Vector3 cord, GameObject tile)
{
    Name = name;
    TileType = type;
    Cordinates = cord;
    Tile = tile;
}

public enum Type
{
    Unset,
    Sand,
    Obstacle,
    ObstaclePass
}

}

And my so called Map, it draws it aswell just to be easy. Inside this map there is GetTileType() - it returns that gameobject i am drawing.

using System.Collections.Generic;
using UnityEngine;

public class Map : MonoBehaviour {

public Vector2 MapSize = new Vector2(5,5);
public List<Tiles> MapTile = new List<Tiles>();
//AllTiles holds all the gameobject i'm choosing from
public List<GameObject> AllTiles = new List<GameObject>();


public void GenerateMap()
{
    for (int x = 0; x < MapSize.x; x++)
    {
        for (int z = 0; z < MapSize.y; z++)
        {
            //just so i could test if it draws different gameobject
            if (x == 2 && z == 2)
            {
                MapTile.Add(new Tiles("Obstacle", Tiles.Type.Obstacle, new Vector3(x, 0, z), GetTileType(Tiles.Type.Obstacle)));
            }
            else
            {
                MapTile.Add(new Tiles("Unset", Tiles.Type.Unset, new Vector3(x, 0, z), GetTileType(Tiles.Type.Unset)));
            }
        }
    }
}

//This is my question
public GameObject GetTileType(Tiles.Type type)
{
    switch (type)
    {
        case Tiles.Type.Unset:
            return AllTiles[0];
        case Tiles.Type.Obstacle:
            return AllTiles[1];
        case Tiles.Type.ObstaclePass:
            return AllTiles[2];
        default:
            break;
    }
    return AllTiles[0];
}

public void DrawMap()
{
    for (int i = 0; i < MapTile.Count; i++)
    {
        Instantiate(MapTile[i].Tile, MapTile[i].Cordinates, Quaternion.Euler(90, 0, 0), transform);
    }
}

private void Start()
{
    GenerateMap();
    DrawMap();
}
}

I really could make it either with switch or if statment. Switch looks better for me so i stick with it, but somehow i think there should be better and cleaner way. Or should i choose the tile in Tiles class? So what are other options of doing this? Or something that really should be changed in this code. (not really trying to make anything here, just try something few times a week)

\$\endgroup\$
  • \$\begingroup\$ Why reinvent the wheel ? There's a tile map editor in Unity : blogs.unity3d.com/2016/06/13/2d-experimental-preview \$\endgroup\$ – Aybe Oct 11 '17 at 12:41
  • \$\begingroup\$ Well as I said I'm not trying to make a working game. I just like trying to do something new that's it. \$\endgroup\$ – Myrez Oct 11 '17 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.