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 anwserman · Jul 28, 2012 at 11:35 AM · javascriptarrayclassdataexternal

Reading and Storing External Data into Memory (From Text)

I'm using JavaScript, so I think this will make a big difference compared to other languages.

 //count the number of items we need to load in
 4
 //[ITEMID] : [SCALEX] , [SCALEY] : [TEXTURENAME] : [STEPCOUNT] : [STEPS] , ... , [STEPSN]
 0 : 1, 1 : foo : 3 : 0, 1, 2
 1 : 1, 1 : bar : 3 : 2, 1, 0
 2 : 2, 2 : pud : 5 : 1, 2, 3, 4, 5
 3 : 3, 3 : lye : 8 : 1, 2, 3, 4, 5, 6, 7, 8

So, this is a new area of Unity that I haven't experienced yet. I know how to load and read data into fixed-size arrays in Unity. This one I'm not so sure about, because the data-types change (and that a field can have an N amount of values). However, prior programming experience tells me that this ends up turning into (pseudocode):

 class CustomClass
     ID as integer
     Scale as Vector2
     Tex as string
     Count as integer
     Steps[] as integer
 end class

 classArray as List<CustomClass>


Thankfully, this data is fixed - it will never get modified while in memory and won't need to be saved on exit. How does one go about defining a class array and populating it in JavaScript? Any pointers or tips?

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 TowerOfBricks · Jul 28, 2012 at 12:35 PM

My UnityScript is a bit rusty, but I know you can use the class Array (yes, the class named Array). http://docs.unity3d.com/Documentation/ScriptReference/Array.html

So if you define a class like that. You could do something like this: (pseudocodish, almost valid UnityScript). The read... functions could be any text reader class you use to parse the information.

 var arr = new Array ();
 var itemcount = readInteger ();
 for (var i=0;i<itemcount;i++) {
     var obj = new CustomClass ();
     obj.ID = readInteger ();
     obj.Scale = new Vector2 (readFloat(),readFloat(),readFloat ());
     obj.Tex = readString ();
     obj.Count = readInteger ();
     obj.Steps = new int[obj.Count]; //Count was to be used for this, right?
     for (var j=0;j<obj.Steps.length;j++) obj.Steps[j] = readInteger ();
     arr.Add (obj);
 }
Comment
Add comment · Show 7 · 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 whydoidoit · Jul 28, 2012 at 12:41 PM 0
Share

It would be preferable to use

    List.<CustomClass>

over Array - type safety, same access methods.

avatar image TowerOfBricks · Jul 28, 2012 at 12:42 PM 0
Share

Yes probably better. As I said, my UnityScript is a bit rusty, so I wasn't sure how to use it in UnityScript. Only in C#. So I went for the safe approach.

avatar image whydoidoit · Jul 28, 2012 at 12:46 PM 0
Share

It's a wacky format, I can't quite get used to typing "dot" "less than" - screams wrong to me :)

avatar image TowerOfBricks · Jul 28, 2012 at 01:06 PM 0
Share

Wait... It is actually valid UnityScript to write List.<....> !? Though that dot was a typing error of yours. That just looks ugly.

avatar image whydoidoit · Jul 28, 2012 at 01:09 PM 0
Share

Yes to both. Legal and ugly :)

Show more comments

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

7 People are following this question.

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

Related Questions

What to use to store data like eg. block types? 1 Answer

Creating a dynamic array of objects of custom class 1 Answer

Serialize large array of class 0 Answers

How to increase the size of a class array? 4 Answers

Why Does Initialising Built-In Array of Classes Require Constructor? 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