I have a general dictionary as struct as key and a class reference as a value.
Dictionary<IntVector2, SpriteRenderer> tileRendererMap;
I get a link to render via coordinates and change it like this:
tileRendererMap[tileCoord].color = Color.cyan;
This generates 0.8 Kbytes of garbage every time I use it. Is this the default behavior? I would think that finding a dictionary would be one of the most interesting things. I need to find a way around this, as I work on a mobile platform, and this is a critical system.
Any ideas what I can do wrong or how to get free viewing access?
Edit after testing:
Using an int key instead of my custom structure works as expected. No allocation, no problem, and I don't find it strange to use an identifier as a key. However, for my game, I want to save the rendering associated with a certain tile in the grid, which in fact should not be an object of any type, because I care only about the position of the grid. Therefore, I thought IntVector2 might be a practical identifier, but I could get around it if I had to.
, Unity3D Profiler. 0.8KB Dictionary_get_item, DefaultComparer.Equals(). -, /, , - , , .
:
public struct IntVector2
{
public int x, y;
public override bool Equals(object obj)
{
if (obj == null || obj is IntVector2 == false)
return false;
var data = (IntVector2)obj;
return x == data.x && y == data.y;
}
public override int GetHashCode()
{
return x.GetHashCode() ^ y.GetHashCode();
}
}
:
dictionar.
public struct IntVector2 : IEquatable<IntVector2>
{
public int x;
public int y;
public IntVector2(int x, int y)
{
this.x = x;
this.y = y;
}
public bool Equals(IntVector2 other)
{
return x == other.x && y == other.y;
}
}
Dictionary IEquatable , Equals, .