Is there an int based class/struct like Vector2?
I am currently doing a lot of 2D texture based work, for which I am currently using Vector2 to store x,y coordinates. However, due to Vector2's x and y being floats, I have to typecast a lot in my source. There is no use for the floating point because all of these all pixel based coordinates, and therefore whole numbers. I just use Vector2 because it is handy to store an x and y :)
Is there a class for this, or do I have to write my own struct for this?
Thanks!
Answer by yoyo · Mar 08, 2011 at 08:16 PM
There is no such class or struct built into Unity.
Easy to implement though (C#) ...
public struct IntVector2 { int x; int y;
int sqrMagnitude
{
get { return x * x + y * y; }
}
}
You would probably want to follow the example of Vector2 and implement as many of its methods as make sense for your application. I've shown sqrMagnitude as an example.
O$$anonymous$$ too bad. I did my research but couldn't find any. When I started at AS3 I made the mistake of writing a few struct-like classes (like Point! :D) which later on were already in AS3. With a lot more functionality! So I didn't want to make that mistake again ;)
Thanks yoyo!
This is outdated, as bananaflame says in his answer unity now has int vectors, please update you aswer, we just wasted some time implementing our own int vectors becaus of this answer, now we have to ctr-f IntVector2 for Vector2Int.
Answer by bananaflame · Mar 06, 2018 at 06:02 AM
UPDATE: There has since been a Unity update where they added Vector2Int, Vector3Int, and Vector4Int classes with this type of functionality.
I was halfway through making my own structs when I updated unity and noticed Vector3Int autocompleting in intellisense, lol. Better late than never.
This is very useful for game state logic. Upvoted and commented for visibility.
Answer by Jessy · Mar 08, 2011 at 09:02 PM
I might recommend just writing an extension method, so that you don't have to rewrite everything, like yoyo said.
public static class Vector2Extensions { public static IntVector2 ToIntVector2 (this Vector2 vector2) { int[] intVector2 = new int[2]; for (int i = 0; i < 2; ++i) intVector2[i] = Mathf.RoundToInt(vector2[i]); return new IntVector2 (intVector2); } }
public struct IntVector2 { public int x, y;
public IntVector2 (int[] xy) {
x = xy[0];
y = xy[1];
}
}
Just be careful not to constantly convert back and forth or you'll chew up cycles and possibly have precision problems. (I'd probably give serious thought to using Vector2 out of the box.)
Answer by myxolobe · Sep 22, 2017 at 02:57 AM
You may also use the handy Tuple class. While it may be better to implement the way others have suggested in many (perhaps all) circumstances, this way is still worth a mention.
Tuple<int, int> position = new Tuple<int,int>(5, 8);
It's a bit weird in context though:
getTile(floor, position.item1, position.item2).isCorner = true;
It's easy to forget it's referring to x and y coordinates.
It seems that i can't use that class - the docs say it is in System namespace, but it is not recognised by the compiler. Is it available in the $$anonymous$$ono used in Unity? Because docs say it is available with .Net 4.0 and more
Since Unity 2017.1.0 .NET 4.6 has been available in editor as an experimental feature. You have to switch in Player settings.