Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by james2238 · Oct 16, 2016 at 06:26 PM · vector22d array

Using Vector2 as 2D Array Index

I'm new to Unity and what I want to do is use a vector2 as an index position for a 2D Array, like this

 2dArray[vector2] = int;

I've tried a lot of ways to get this to work but haven't found any. Any ideas would help.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Cherno · Oct 16, 2016 at 07:27 PM

A two-dimensional array has this format:

 int[,] myArray

and elements are accessed like this:

 myArray[3,76] = myValue;

So much for the basics. To use a Vector2, you first have to be aware of the fact that a Vector2 consists of 2 floats, and not integers. That's ok though, as long as the floats always are whole numbers. Otherwise, you will have to round, or floor, them to an integer.

 Vector2 myVector2 = new Vector2(3,76);
 
 myArray[myVector2.x,myVector2.y] = myValue;

As mentioned above, it's possible that you have to cast the floats as integers, or round them, in order to be accepted as indices for the two-dimensional array.

See also:

Choosing the right collection type

Comment
Add comment · Show 3 · 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 ElijahShadbolt · Oct 16, 2016 at 10:35 PM 0
Share

i.e.

 myArray[$$anonymous$$athf.FloorToInt(myVector2.x),$$anonymous$$athf.FloorToInt(myVector2.y)] = myValue;
avatar image Cherno ElijahShadbolt · Oct 16, 2016 at 10:39 PM 0
Share

Yup :)

It might be easier to just write your own custom class that is like a Vector2 but with integers ins$$anonymous$$d of floats so you don't have to round all the time.

avatar image ElijahShadbolt · Oct 17, 2016 at 04:15 AM 1
Share

Here's a struct I made called int2 which holds two int's that you can set in the inspector. It also includes extension methods for 2D arrays to use the int2 as an index, as well as easy conversion from Vector2's.

int2.cs

 using UnityEngine;
 
 [System.Serializable]
 public struct int2
 {
     public int x, y;
 
     public int2(int x, int y) {
         this.x = x;
         this.y = y;
     }
     public int2(Vector2 vector) {
         x = (int)vector.x;
         y = (int)vector.y;
     }
 
     public enum RoundingType { Round, Floor, Ceil, Cast }
     public int2(Vector2 vector, RoundingType roundingType)
     {
         switch (roundingType)
         {
             case RoundingType.Round:
                 x = $$anonymous$$athf.RoundToInt(vector.x);
                 y = $$anonymous$$athf.RoundToInt(vector.y);
                 break;
             case RoundingType.Floor:
                 x = $$anonymous$$athf.FloorToInt(vector.x);
                 y = $$anonymous$$athf.FloorToInt(vector.y);
                 break;
             case RoundingType.Ceil:
                 x = $$anonymous$$athf.CeilToInt(vector.x);
                 y = $$anonymous$$athf.CeilToInt(vector.y);
                 break;
             default:
                 x = (int)vector.x;
                 y = (int)vector.y;
                 break;
         }
     }
 }
 
 public static class ArrayExtensions
 {
     public static T GetValue<T>(this T[,] array, int2 index)
     {
         return array[index.x, index.y];
     }
     public static void SetValue<T>(this T[,] array, T value, int2 index)
     {
         array[index.x, index.y] = value;
     }
 }

ExampleScript.cs

 using UnityEngine;
 
 public class ExampleScript : $$anonymous$$onoBehaviour
 {
     public int2 arraySize = new int2(3,4);
 
     public int2 index = new int2(0, 0);
     public string value = "Hello World";
 
     public string[,] array;
 
     void Start()
     {
         array = new string[arraySize.x, arraySize.y];
         array.SetValue(value, index);
         Debug.Log(array.GetValue(index));
     }
 }

Examples of conversion from Vector2

 Vector2 vector = new Vector2(3.4f, 6.6f);
 
 int2 a = new int2(vector);
 int2 b = new int2(vector, int2.RoundingType.Round);
 int2 c = new int2(vector, int2.RoundingType.Floor);
 int2 d = new int2(vector, int2.RoundingType.Ceil);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Returning Multiple Variables From a Function 1 Answer

Is there a more efficient way to pass two integers via "SendMessage" than using a Vector2 Struct? 1 Answer

Rotating A Vector2 Input Into A Vector3 1 Answer

Using vector2 (transform.position.x, transform.position.y -= 1) error 2 Answers

2 fingers sprite rotation 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