- Home /
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.
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:
i.e.
myArray[$$anonymous$$athf.FloorToInt(myVector2.x),$$anonymous$$athf.FloorToInt(myVector2.y)] = myValue;
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.
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
Follow this Question
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