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
1
Question by DaveA · Dec 07, 2010 at 11:10 PM · javascriptarrayresizestructure

Resizing arrays, and what is the default array type anyway?

I need to be able to add and remove things from an array of custom structures.

In my JS file I have:

var stops : TourStop[];

where TourStop is a class containing various things. This 'stops' variable shows up in the Inspector, and I can edit it there (to initialize it).

I need to resize the array (add/remove) at runtime.

Is this a 'built-in' array? Or a JS array? Or something else?

I tried

stops.length = stops.length + 1;

But despite what the documentation says (assuming this were an Array), 'length' is read-only. So I'm not quite sure what this thing is so I don't know which API's to call to muck with it.

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

Answer by skovacs1 · Dec 07, 2010 at 11:28 PM

A built-in array instance : T[] is a fast, fixed-sized array. To resize such an array, you would need to create a second array of the new size and duplicate the items from the old array over. It may show up in the inspector as its content's type is known, but only if it is a serializable type.

A "javascript" Array instance : Array is a not-so-fast, dynamically-sized array. It does not show up in the inspector as its content's type is unspecified.

See here for more about arrays. Note that you can convert between built-in arrays and Arrays, so if you start with an array and then convert it to an Array, change the Array and then convert back, you can shorthand the process, but this is hardly the most efficient route.

If you wanted a dynamically sized array-like structure that serializes in the inspector, you want a System.Collections.Generic List of a serializable type. See here for more about serializable types.

Comment
Add comment · Show 4 · 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 DaveA · Dec 07, 2010 at 11:37 PM 0
Share

Thanks. $$anonymous$$y 'TourStop' is a class containing serializable types (strings, ints, Vector3, etc). But it being a class, I don't suppose that itself is serializable? I suppose worst case, I could make a custom inspector editor which exposes javascript arrays(?)

avatar image skovacs1 · Dec 07, 2010 at 11:49 PM 0
Share

If you specify the class TourStop as serializable (http://unity3d.com/support/documentation/ScriptReference/Serializable.html) by extending System.Object, then it too will be a serializable type. As far as making dynamically sized arrays, you could make a custom inspector that exposes js Arrays (or at least exposes the arrays of something that contains them), but when List already serializes, it seems like wasted effort.

avatar image DaveA · Dec 08, 2010 at 08:29 PM 0
Share

According to the $$anonymous$$SDN page, List is not supported or no example available in JScript. Would you know the syntax for declaring that List variable (if possible)?

avatar image skovacs1 · Dec 08, 2010 at 09:48 PM 0
Share

That's correct. I too know of no syntax to specify the type for generics in javascript. I believe none exists in unity's js but would be glad to learn otherwise. The solution is to create such a list in c# and if this c# script is compiled first, you can then reference the list elsewhere. If you like, you could easily put together a serializable wrapper for the generic list that js can take in. Pretty much all you would need is a class whose constructor takes in the type and stores a reference to a List to hold that type.

avatar image
1

Answer by Novodantis 1 · Dec 07, 2010 at 11:27 PM

Using:

var stops : TourStop[];

Initialises one of Unity's builtin/.NET arrays. These cannot be resized, but are fast and exposed in the Inspector.

var stops = new Array();

Creates a more traditional (to programmers) dynamic Javascript array. You can add new values onto this using stops.Push(value), and alter its size. The downside is that they aren't available in the Inspector window.

Unity's reference asserts it is easy to convert between them to cover all requirements. Check out the full page 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 DaveA · Dec 07, 2010 at 11:39 PM 0
Share

So, in theory, I could convert my built-in TourStop array to a JS array, manipulate it, then reassign it back to the 'stops' variable using the reverse conversion?

avatar image Novodantis 1 · Dec 08, 2010 at 12:09 AM 0
Share

Yup, you can just convert to a dynamic array when you need to push or pop them etc. The script manual linked gives a good example of switching between them (that's neater than me trying to paste code into these kooky comment windows :P )

avatar image DaveA · Dec 08, 2010 at 08:47 PM 0
Share

This works nicely, thanks.

avatar image
0

Answer by Skjalg · Dec 07, 2010 at 11:18 PM

You probably need to create a new array with length = oldArray.length+1 and copy over the objects inside.

What you probably want to do, is to use a 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 DaveA · Dec 07, 2010 at 11:24 PM 0
Share

How would I construct the new array? Where can I find documentation on 'List' (not in Unity nor msdn (but they do have IList and ArrayList)? Thanks

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

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Reference JS array of Objects = errors 1 Answer

array out of range in simple pathinder javascript 1 Answer

How do I Resize an Array in JS? 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