Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Ochreous · Aug 24, 2015 at 03:20 AM · c#arrayvector3vector2convert

C# Convert Vector3[] to Vector2[]

I tried looking for a specific Vector3 function that converts a Vector3[] to a Vector2[] but I wasn't able to find anything named like toVector2() . Is there actually a function that converts a Vector3[] to a Vector2[] or will I need to create one from scratch?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by NeverHopeless · Aug 24, 2015 at 07:55 AM

Perhaps using extension methods in combination with Array.ConvertAll will be more close to your needs:

Add this extension to your script:

 public static class MyVector3Extension
 {
     public static Vector2[] toVector2Array (this Vector3[] v3)
     {
         return System.Array.ConvertAll<Vector3, Vector2> (v3, getV3fromV2);
     }
         
     public static Vector2 getV3fromV2 (Vector3 v3)
     {
         return new Vector2 (v3.x, v3.y);
     }
 }

and use it like:

 Vector2[] v2 = v3.toVector2Array ();

Here is an example enclosed:

 using UnityEngine;
 using System.Collections;
 
 public class TestVectorConvert : MonoBehaviour
 {
     // Use this for initialization
     void Start ()
     {
         Vector3[] v3 = new Vector3[5];
         for (int i = 0; i < 5; i++) {
             v3 [i] = new Vector3 (0.5f, 0.4f, 1.0f);
         }
 
         Vector2[] v2 = v3.toVector2 ();
         for (int i = 0; i < 5; i++) {
             Debug.Log (v2 [i].ToString ());
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
 }
 
 public static class MyVector3Extension
 {
     public static Vector2[] toVector2 (this Vector3[] v3)
     {
         return System.Array.ConvertAll<Vector3, Vector2> (v3, getV3fromV2);
     }
         
     public static Vector2 getV3fromV2 (Vector3 v3)
     {
         return new Vector2 (v3.x, v3.y);
     }
 }





Comment
Add comment · 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
3

Answer by fafase · Aug 24, 2015 at 03:46 AM

There probably isn't any so you can do you own:

 Vector2[] ConvertArray(Vector3[] v3){
     Vector2 [] v2 = new Vector2[v3.Length];
     for(int i = 0; i <  v3.Length; i++){
         Vector3 tempV3 = v3[i];
         v2[i] = new Vector2(tempV3.x, tempV3.y);
     }
     return v2;
 }

 

Comment
Add comment · 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
0

Answer by gabriel-diquinzio · Apr 24 at 01:36 AM

the one liner way using linq library.

   using System.Linq;
          List<Vector3> v3positions = new List<Vector3>();
     
     
            List<Vector2> v2Positions => v3positions.Select((v3) => (Vector2)v3).ToList();
 
Comment
Add comment · 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

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

27 People are following this question.

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

Related Questions

Writing Policy class for handling Vector2s and 3s. 1 Answer

Im misunderstanding arrays. 1 Answer

How do I place the matching Vector3 array values of a Vector3 array in a new array ? 1 Answer

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 2 Answers

String Parsing 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