• 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 benfattino · May 07, 2011 at 01:35 PM · vector3vectortxt

Vector3 array from external .txt file

Somebody can paste an little example code that create a vector3 array from reading external .txt file. The file is a continuos list of point coordinate (x,y,z) spare by comma like this:

2,2,3,   //first point of first vector x,y,z
3,4,5,   //second point of first vector x1,y1,z1
2,2,2,   //first point of second point x2,y2,z2
1,1,1,   //second point of second vector x3,y3,z3
ecc. 

Thanks.

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
Best Answer

Answer by DaveA · May 07, 2011 at 05:50 PM

If you could leave off the comma at the end of the line, and they are each on separate lines, it would be easier. Also see http://answers.unity3d.com/questions/56793/vector3-array-from-txt And http://unity3d.com/support/documentation/ScriptReference/TextAsset-text.html and http://msdn.microsoft.com/en-us/library/b873y76a.aspx

Once you have text in a string, you can split the lines with var pts = myString.Split("\n"[0]) and then each resulting string, split by comma: var xyz = pts[i].Split(","[0])

Ex (not tested, and no error checking done here)

Input is:

 1,2,3
 4,5,6


 var vectors : Vector3[];
 
 var inputFile = ( read the string from the file any way you want, could be WWW )
 var lines = inputFile.Split("\n"[0]); // gets all lines into separate strings
 vectors = new Vector3[lines.Length];
 for (var i = 0; i < lines.Length; i++)
 {
   var pt = lines[i].Split(","[0]); // gets 3 parts of the vector into separate strings
   var x = float.Parse(pt[0]);
   var y = float.Parse(pt[1]);
   var z = float.Parse(pt[2]);
   vectors[i] = new Vector3(x,y,z);
 }
Comment
Add comment · Show 2 · 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 benfattino · May 13, 2012 at 07:53 PM 0
Share

Can you give a script example? I try with no result...

avatar image DaveA · May 15, 2012 at 03:28 AM 0
Share

Added to my answer

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Filling an array with child vectors 2 Answers

Finding the Current Vector of Travel 1 Answer

How to make Y-Axis face away from a position? 2 Answers

3 vector3 distance checks, only one works 1 Answer

Rigidody.velocity = Direction * speed; How to get direction? 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