Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
11
Question by Jordii · Mar 08, 2011 at 08:07 PM · floatintvector2point

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jordii · Mar 08, 2011 at 08:34 PM 0
Share

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!

avatar image Molion · Jun 28, 2018 at 09:58 AM 0
Share

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.

avatar image
10

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_Natural_Tanuki · Nov 06, 2018 at 01:40 PM 0
Share

This is very useful for game state logic. Upvoted and commented for visibility.

avatar image termway · Oct 04, 2019 at 07:11 PM 0
Share

Since 2017.2 ( https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Vector2Int.html )

avatar image
2

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];
 }   

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Mar 09, 2011 at 07:48 AM 0
Share

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.)

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheSmokingGnu · Nov 28, 2017 at 02:00 PM 0
Share

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

avatar image Salmjak TheSmokingGnu · Dec 18, 2017 at 02:22 PM 0
Share

Since Unity 2017.1.0 .NET 4.6 has been available in editor as an experimental feature. You have to switch in Player settings.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ToString ("f0") is rounding my float 1 Answer

Error CS0266 with my clicker game 1 Answer

Really need help please/A switch expression of type `float' cannot be converted to an integral type, bool, char, string, enum or nullable type 2 Answers

Adding Int's from different and the same script 1 Answer

How to make a number higher than Quintillion? (1000000000000000000) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges