Attaching data to RuleTile and Sprite GetHashCode

This is very frustrating!

I’d like to attach additional data to my tiles (in particular, to tiles AFTER the rule tile has decided which sprite to display). The information is not per individual tile in the world, nor is it per tile type, but rather it’s per tile sprite.

  • I can’t extend Sprite, because it’s sealed.

  • Extending Tile is not an option because the Tile object is shared across cells with the same rule (just the sprite changes)

  • Mapping each Sprite object to my own data structure would be a reasonable workaround, except that Sprite does not implement GetHashcode - instead it uses the default GetHashCode for Object, which is not supposed to be used as a hash key. I would normally ignore this, except that I’ll be using the map up to a few thousand times per frame and I’m worried about the performance. It seems very poor if this is the only option.

  • I could create my own parallel data structure for storing my own data at each tile location, but this feels like a massive waste. I don’t even need per-cell data, I just need a bit of data per rule tile / sprite

Other possible hacks:

  • What if I leave the GameObject field in the rule tile blank, but then manually set it to a static gameobject (i.e. all tiles with the same sprite point to the same gameobject) reference , and use this instead. I won’t be using the usual GameObject functionality, though I’m concerned that it might break something.

Any recommendations, or alternative suggestions?

Ah, I see that Unity does actually override GetHashCode on Object; however, it’s still unclear whether it’s suitable as a hash key. The thing that bothers me about this is that it could behave differently on different platforms. It’s unlikely to be an issue, but difficult to be sure. I believe it uses the underlying C++ object reference, and seems to be approximately sequential.