Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 dansav · Sep 13, 2013 at 07:01 PM · 2djavascriptarraylistjoin

Is there a way to convert a 2d generic list to a string in unityscript

I have some javascript code I'm trying to convert that takes a two dimensional array and uses a javascript join to turn it into a string then splits the string to turn it into a one dimensional array.

I'm converting this code to run on iphone in pragma strict so I'm using generic .net lists instead of javascript type arrays. Is there an equivalent of the javascript type of join for generic lists or maybe another way to do this.

Comment
Add comment · Show 6
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 ArkaneX · Sep 13, 2013 at 07:18 PM 0
Share

What do you mean by 2d? Two-dimensional?

avatar image dansav · Sep 13, 2013 at 07:55 PM 0
Share

A list like this [3,4],[1,2],[5,6]. I would like to convert it into 3,4,1,2,5,6 a flat array of ints. The javascript code I'm trying to convert used array.join to convert it to a string, then it split the string thus creating the final array.

avatar image ArkaneX · Sep 13, 2013 at 07:59 PM 0
Share

Is this list or array? How is it declared? Because you wrote about generic list and this what you posted looks like standard 2-dimensional array.

avatar image dansav · Sep 13, 2013 at 08:16 PM 0
Share

I created a generic list of lists of integers then populated it using a for loop and Add. The original code was in javascript from a webpage. I just wrote it that way to show the structure but it's in generic list format in unityscript. The comment section won't let me add code examples without messing up the format.

avatar image ArkaneX · Sep 13, 2013 at 08:35 PM 0
Share

You can post a code in comments - just paste it and then select and press 101010 button. Or use Ctrl+$$anonymous$$

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by ArkaneX · Sep 13, 2013 at 07:24 PM

You can use String.Join to join string arrays into one string. If you have a generic list of strings, then you have to convert it to array before joining:

 var list : List.<String> = new List.<String>();
 list.Add("aaa");
 list.Add("bbb");
 list.Add("ccc");
 var s = String.Join(",", list.ToArray());

If you want to convert generic list of non-string elements, then you have to cast them to strings first:

 var list : List.<int> = new List.<int>();
 list.Add(55);
 list.Add(2);
 list.Add(777);
 var stringList = list.Select(function(x) x.ToString());
 var s = String.Join(",", stringList.ToArray());

Above examples require System.Collections.Generic and System.Linq namespaces, so you have to import these:

 import System.Collections.Generic;
 import System.Linq;

UPDATE: after info from comments - you should be able to get flattened list from your variable using

 var flattenedArray = list.SelectMany(function(x) x).ToArray();

where list is your generic list.

Comment
Add comment · Show 1 · 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 dansav · Sep 13, 2013 at 09:30 PM 0
Share

Thanks so much for your thorough answer. I think this will do the trick.

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

16 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

Related Questions

Help Unity Script to C#!! 1 Answer

How to Find all objects with Tag and List their Transforms? 4 Answers

A node in a childnode? 1 Answer

Array of Transform[] not correctly typed in Javascript 1 Answer

Is there an easy way to add an array of raycasthit2Ds to a list? (javascript) 2 Answers


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