|
I need to be able to add and remove things from an array of custom structures. In my JS file I have:
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
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.
(comments are locked)
|
|
A built-in array A "javascript" Array 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. Thanks. My '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(?)
Dec 07 '10 at 11:37 PM
DaveA
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.
Dec 07 '10 at 11:49 PM
skovacs1
According to the MSDN page, List<> is not supported or no example available in JScript. Would you know the syntax for declaring that List variable (if possible)?
Dec 08 '10 at 08:29 PM
DaveA
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.
Dec 08 '10 at 09:48 PM
skovacs1
(comments are locked)
|
|
Using:
Initialises one of Unity's builtin/.NET arrays. These cannot be resized, but are fast and exposed in the Inspector.
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. 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?
Dec 07 '10 at 11:39 PM
DaveA
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 )
Dec 08 '10 at 12:09 AM
Novodantis 1
This works nicely, thanks.
Dec 08 '10 at 08:47 PM
DaveA
(comments are locked)
|
|
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. 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
Dec 07 '10 at 11:24 PM
DaveA
(comments are locked)
|
