- Home /
Tilemap2017 Access position & sprite of specific Tile in new Grid System 2dGame
Hey everyone! I try to access the Vector3 of a specific Tile in the Tilemap2017, maybe anyone can help me? I drew a map and I wanted to store each position of each Tile in a dictionary or list so the player can change a specific tile ingame for a ingame building mode. And I also could save the map ingame if any changes happen.
So, how can I access the position of a specific tile in a grid/tilemap and store into a List/Dictionary?
I hope someone can help me
@aldonaletto I hope a bit you can help me with your great knowledge :]
Answer by Cherno · Nov 24, 2017 at 10:03 PM
Instead of Vector3, use a custom class that is similar but doesn't involve expensive and postentially inaccurate floating point operations. I wrote one that uses integers instead of floating point numbers:
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public struct Coordinate3 : IEquatable<Coordinate3> {
public int x;
public int y;
public int z;
public Coordinate3() {
x = 0;
y = 0;
z = 0;
}
public Coordinate3(int a, int b, int c) {
x = a;
y = b;
z = c;
}
public Coordinate3(Vector3 v3) {
x = Mathf.RoundToInt(v3.x);
y = Mathf.RoundToInt(v3.y);
z = Mathf.RoundToInt(v3.z);
}
public bool Equals(Coordinate3 other) {
if (this.x == other.x && this.y == other.y && this.z == other.z) {
return true;
}
else {
return false;
}
}
public override int GetHashCode() {
return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
}
}
Your dictionary would looke like this, assuming that you have a class called Tile:
public Dictionary<Coordinate3,Tile> tiles = new Dictionary<Coordinate3,Tile>();
tiles.Add(new Coordinate3(2,65,7), new Tile());
Hey, thank you for your hint, I will consider it but I'm still confused how to access a single Tile in the new Unity Tilemap-System, normally I create my 2d-$$anonymous$$aps by using loops and add each tile to the next, so I would the position just by looking at the number of the loop but now I just paint the Tile on the map and as a Rookie I don't have any idea how to access it ^^