简体   繁体   中英

Vector2 cast to Vector3 in C#/Unity3D

I had some code like this

Vector3 someFun (Vector2 v) {

    return new Vector2 (...);
}

As you see the types dont match but there is no error. Does Vector2 inherit from Vector3 or is there a cast between the two?

If I am understanding their documentation as found here , they use implicit operators to convert a Vector3 to a Vector2 and vice versa. I think that they are structs (for greater performance), which rules out inheritance.

You can pass the needed values in the Vector3 constructor:

Vector3 someFun (Vector2 v) {

    return new Vector3 (v.x, v.y, 0.0f);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM