Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Myrmidou · Feb 13, 2018 at 11:36 AM · arraylisttable

Organize data in a multi-column table

Hi,

As a complete beginner, I'm working for a study project where I need to do a couple actions and export the resulting data. The way I export this data doesn't matter but it needs to be organized as a 3 columns table where the first column contains strings, the second floats and the third ints. I also need to increase the length of this table at runtime.

How can I manage to do this? How will I be able to add a whole new line (length)? How will I be able access a specific value of this list?

Thanks a lot

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 Bunny83 · Feb 13, 2018 at 12:04 PM

What do you mean by "export"? Do you mean visually or just to export the data to a data format like JSON, XML, ...? Since you said "organise" it sounds like a pure programming problem. However if you actually mean to visually display the data it would be a completely different problem.

To represent the data in C# the best solution for a fix data values per "line" / data record would be to simply use a class or struct

 [System.Serializable]
 public class DataRecord
 {
     public string nameOfFirstColumn;
     public float nameOfSecondColumn;
     public int nameOfThridColumn;
 }

The actual "table" would be just a List<DataRecord>

 List<DataRecord> table = new List<DataRecord>();

To add a new record you just do

 DataRecord row = new DataRecord();
 // set your values of "row".
 table.Add(row);

To export the data to JSON you can either represent each "row" as nested array or as object

 [
     ["some string", 0.45, 5],
     ["some string", 0.45, 5],
     ["some string", 0.45, 5],
 ]

Of course when exporting as object the data values do not have a specific "order" but they are named properties. Something like

 {
     {
         "name":"some string",
         "weight": 0.45,
         "value": 5
     },
     {
         "name":"some string",
         "weight": 0.45,
         "value": 5
     },
     {
         "name":"some string",
         "weight": 0.45,
         "value": 5
     },
 }

JSON text can be created in several ways. One would be to use Unity's JsonUtility class. However it's very limited as it's strictly bound to actual classes and variable names. I've created the SimpleJSON framework which allows easy creation and parsing of JSON text.


Another common export format for tables would be CSV, so just a comma / character seperated values. Such files can easily be opened in Excel or similar programs.

 some string;0.45;5
 some string;0.45;5
 some string;0.45;5

Those can be easily constructed since it's just plain text with a seperator.

If you want to visually display the data inside your Unity application you have to look into the UI system. There are too many ways how you can tackle this so without further details i will stop here ^^.

Comment
Add comment · Show 3 · 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 Myrmidou · Feb 13, 2018 at 12:29 PM 0
Share

Thanks for this awesome answer!

I've never used or seen this DataRecord thing. Can you explain it a bit further?

For exemple, if I need to add "car", "0.25", "3" to the list, how can I proceed?

At first I create the actual list with List table = new List(); Then, in a "CreateRow" function, I would do as you said earlier. But how can I change the value of "row" so that it contains my 3 informations?

avatar image Bunny83 Myrmidou · Feb 13, 2018 at 01:06 PM 0
Share

DataRecord is just a custom name for a class. It can be anything you like. This comment tells me you don't know much about object oriented program$$anonymous$$g, right?


To extend my example above to set the 3 values you would do this:

 DataRecord row = new DataRecord();
 row.nameOfFirstColumn = "car";
 row.nameOfSecondColumn = 0.25f;
 row.nameOfThridColumn = 3;
 table.Add(row);

Of course you should rename "nameOfFirstColumn", "nameOfSecondColumn", ... to something that makes sense. You have presented your question in an abstract way so we have no idea what those "columns" should represent. You should pick proper variable and classnames. For example in the JSON example above i used "name" for the string value, "weight" for the float value and "value" for the int value. Though it depends on the usage.


Unity's Transform class for example represents the 3d transformation of a gameobject which includes the scale, rotation and the translation (position) or the object. Therefore the class contains a property names "position", "rotation" and "localScale".

avatar image Myrmidou Bunny83 · Feb 13, 2018 at 01:29 PM 0
Share

As you guessed I only know half of the basics of program$$anonymous$$g but with your explanation it now makes perfect sense!

For the export part I don't know how I want to do it yet, as a txt or xls file.

To explain my project a bit, you would have a menu where you can choose between 4 paths. Each path will have its own list, feeded with strings and floats the way you showed me above. Then, back to the menu, you would be able to create a file gathering all 4 lists.

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

87 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keeping track of dynamic positions of items in a grid using List/Array? 0 Answers

I want to add items from a list into an array using the editor and popup menus 0 Answers

How to compress iterative code, perhaps using a list and/or loops? 1 Answer

Get all Children of an Object with a certain component 0 Answers

Assign role randomly from array for my online game 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