First type is Vector2, second type is MyVector2 which is:
public class MyVector2
{
public int x { get; set; }
public int y { get; set; }
public MyVector2(Vector2 a)
{
x = Mathf.RoundToInt(a.x);
y = Mathf.RoundToInt(a.y);
}
}
I have created this because too many times i have to use Mathf.RoundToInt() and that looked ugly xd.
What exacly I’m trying to achieve. I need an array with length near 50-100. First element is Vector2 and all the rest are MyVector2. I know 2 solutions but don’t know what are the differences between them and maybe there are better solutions to this.
-
Solution: making an array of Object’s.
-
Solution: making things that proposed Gilad Naaman in other topic on https://stackoverflow.com, that is:
public abstract class GameObject
{
public GameObject();
}
public class TileStuff : GameObject
{
public TileStuff()
{
}
}
public class MoreTileStuff : GameObject
{
public MoreTileStuff()
{
}
}
public class Game
{
static void Main(string[] args)
{
GameObject[] arr = new GameObject[2];
arr[0] = new TileStuff();
arr[1] = new MoreTileStuff();
}
}
So, which way is best for my particular problem ? Maybe other suggestions ?
does this meet your needs?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public List<MyVector2> myVectors = new List<MyVector2>();
void Start()
{
myVectors.Add(new MyVector2(13.9f, 6.4f));
Debug.Log(myVectors[0].GetX() + ", " + myVectors[0].GetY());
}
}
[System.Serializable]
public class MyVector2
{
public Vector2 vector;
public int GetX()
{
return (int)Mathf.Round(vector.x);
}
public int GetY()
{
return (int)Mathf.Round(vector.y);
}
public MyVector2(float x, float y)
{
vector = new Vector2(x, y);
}
}
I used a list instead of an array because it sounds like the size of the collection might be variable or unknown.
If the below take on your vector class does not meet your needs then you can likely achieve your goal by creating a new class to combine all of the required objects.
I typically avoid getters and setters in unity as they make it painful to access data in the inspector.
Also, I added the System.Serializable attribute to make the list of MyVector2 accessible in the inspector.
No, what you want doesn’t work on multiple levels and also doesn’t make much sense. Vector2 is a struct, not a class. Using a class for this purpose is not a good idea due to the garbage overhead. Using an object array is also a bad idea since it would require to box struct values and you actually need to cast the values in the array every time you want to use them. This doesn’t really increase the usability or performance.
Inheritance does only work if you have actually control over all of your classes. As i said Vector2 is not a class and struct do not support polymorphism at all.
If you actually need an integer version of Vector2 you should create a struct similar to Vector2. I’ve done this a long time ago for Vector3 by creating the Vector3i struct. This struct has implicit type conversion operators which allows implicit conversion between Vector3 and Vector3i values. If you create a Vector2i struct similar to my Vector3i struct there would be no need to store different types in an array. Just use an array of Vector2 values. Those can be implicitly converted to Vector2i values where they would be rounded.
As alternative you could create extension methods to get the rounded X and Y value from a Vector2 value:
public static class Vector2IntExtension
{
public static int GetX(this Vector2 aVec)
{
return Mathf.RountToInt(aVec.x);
}
public static int GetY(this Vector2 aVec)
{
return Mathf.RountToInt(aVec.y);
}
}
That way when you have a Vector2 array you can simply do:
Vector2[] someArray;
// ...
int x = someArray[3].GetX();
int y = someArray[3].GetY();
I agree with @Bunny83. I’m skeptical that you want this anyway.
When you need to treat instances of classes which are part of two different polymorphic sets as though they are interchangeable and when they fundamentally do the same kind of thing, you have a problem that indicates the Adapter pattern.
Most often this happens when you have a specific polymorphic set in mind, already, but you can also create an hierarchy just to address this problem - you would basically create one base class for the common concept that ties them together. They you create one kind of implementation that adapts one class into that abstraction and another that adapts the other.
Consider a design like this one:
abstract class CommonVector {
internal abstract int GetX();
internal abstract int GetY();
}
class Vector2CommonVectorAdapter : CommonVector {
internal override int GetX() { (int)Math.Round(return v.x); }
internal override int GetY() { (int)Math.Round(return v.y); }
}
class OtherKindOfCommonVector : CommonVector {
internal int X { get; set; }
internal int Y { get; set; }
internal override int GetX() { return X; }
internal override int GetY() { return Y; }
}
That would allow you to have an array of type CommonVector and treat them all the same, adapting Vector2 in where you need.
I’m really dubious of how useful this answer is for this particular question, because I think you might want to reconsider why you want to do this in the first place. However, it never hurts to tell someone the good news about Design Patterns.
It may someday help you.