Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 robinking · Aug 05, 2010 at 12:01 PM · javascriptvector3builtin arraymultidimarray

multidimensional builtin vector3 array?

I know I can use this helper script in order to create multidimensional builtin arrays in Unity Javascript -- but can I create multidimensional builtin VECTOR3 arrays? I don't know C# so I tried tinkering with the helper script to no avail.

As a side question, are multidimensional builtin arrays as fast in Unity as single-dimensional builtin arrays? I'd rather have performance and a little extra coding...

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 jashan · Aug 05, 2010 at 12:35 PM

Sure, you'd probably simply add this static method to the C# script:

public static Vector3[,] Vector3Array (int a, int b) {
    return new Vector3[a,b];
}

... so that would be for a 2-dimensional one ... for 3-dimensional, you'd add:

public static Vector3[,,] Vector3Array (int a, int b, int c) {
    return new Vector3[a,b,c];
}

Regarding the question of performance: I'm pretty sure there is no performance difference between one-dimensional arrays and multi-dimensional ones. Of course, creating those arrays will likely take a little longer because usually they have more "space" so more memory needs to be allocated and managed. But writing/reading into/from those arrays shouldn't take a significant amount of time.

Of course, it depends on how "much" you do of it. As a rule of thumb: If it's just once per frame: Don't worry. If you're looping or doing nested loops in each frame: Watch out.

As with anything that has to do with performance: "Premature optimization is the root of all evil" (Donald Knuth). See also: Wikipedia: Program Optimization - When to optimize

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 robinking · Aug 05, 2010 at 01:23 PM 0
Share

I just tried that and got the following Console error:

Assets/Standard Assets/Scripts/$$anonymous$$ultiDim.cs(31,19): error CS0246: The type or namespace name `Vector3' could not be found. Are you missing a using directive or an assembly reference?

avatar image Eric5h5 · Aug 05, 2010 at 02:44 PM 0
Share

@Robin $$anonymous$$ing: you need to use the UnityEngine namespace for Unity-specific structs like Vector3.

avatar image robinking · Aug 05, 2010 at 03:41 PM 0
Share

Brilliant, that's something else I've learned about today. Thanks!

avatar image
5

Answer by cncguy · Aug 05, 2010 at 12:41 PM

You can make a new class containing an array of vector3's

e.g.

class ThreeVecs
{
    var v3Arr : Vector3[] = new Vector3[3]; // Array of 3 x Vector3
    function ThreeVecs(){} // constructor - init the class variables here if you like
}

You can then create an array of ThreeVecs:

var multiArr : ThreeVecs[] = new ThreeVecs[10]; // Array of 10 ThreeVecs

multiArr[0] = new ThreeVecs(); multiArr[0].v3Arr[0] = Vector3(1,2,3); multiArr[0].v3Arr[1] = Vector3(4,5,6); multiArr[0].v3Arr[2] = Vector3(7,8,9);

Just one possible way. :)

[EDIT]

so a class to answer your question below and illustrate using the constructor to initialise:

class ThreeVecs { var corner0 : Vector3; var corner1 : Vector3; var corner2 : Vector3; function ThreeVecs(c0 : Vector3, c1 : Vector3, c2 : Vector3) { corner0 = c0; corner1 = c1; corner2 = c2; } }

var multiArr : ThreeVecs[] = new ThreeVecs[10]; // Array of 10 ThreeVecs

multiArr[0] = new ThreeVecs(Vector3(1,2,3), Vector3(4,5,6), Vector3(7,8,9));

Debug.Log(multiArr[0].corner0); //

Comment
Add comment · Show 8 · 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 robinking · Aug 05, 2010 at 01:38 PM 0
Share

I think I understand it! Could I ins$$anonymous$$d give the class three variables corner0, corner1 and corner2 - and then say... var multiArr : ThreeVecs[] = new ThreeVecs[10]; multiArr[0].corner0 = Vector3(1,2,3); ? I tried this and it doesn't work. Is it possible and I'm just getting the syntax wrong, or am I barking up the wrong SpeedTree?

avatar image cncguy · Aug 05, 2010 at 02:57 PM 0
Share

You can put whatever variables you like in the class. something like That should work - what errors are you getting?

avatar image cncguy · Aug 05, 2010 at 02:59 PM 0
Share

oh I see - don't forget to do this: multiArr[0] = new ThreeVecs();

avatar image cncguy · Aug 05, 2010 at 03:01 PM 0
Share

See you create an array of ThreeVecs but then you have to assign each ThreeVec object in the array with an instance of ThreeVec. Then you have to assign the class variables of each ThreeVec instance;

avatar image cncguy · Aug 05, 2010 at 03:02 PM 0
Share

Of course you can use the class constructor to assign the variables when you create each instance

Show more comments
avatar image
1

Answer by Eric5h5 · Aug 05, 2010 at 02:43 PM

Multi-dimensional arrays are slightly slower than 1-dimensional arrays. It's unlikely to make any difference in practice though.

Comment
Add comment · 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

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

How to set a target position in Vector3.MoveTowards using a variable 2 Answers

Move instantiated object towards player's posistion 3 Answers

Applying Constant Force on Multiple Axes Via Javascript? 1 Answer

Move empty object random 1 Answer

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