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
2
Question by robinking · Nov 03, 2010 at 10:36 AM · javascriptvector3classextension

Can I extend Vector3 using Javascript?

I made the following function so that a euler angle vector3 will always be in the range -180 to 180 rather than 0 to 360:

function WrapAround( v : Vector3 ) {
    if (v.x >  180) { v.x -= 360; } // wrap around the origin
    if (v.y >  180) { v.y -= 360; } // wrap around the origin
    if (v.z >  180) { v.z -= 360; } // wrap around the origin
    if (v.x <- 180) { v.x += 360; } // wrap around the origin
    if (v.y <- 180) { v.y += 360; } // wrap around the origin
    if (v.z <- 180) { v.z += 360; } // wrap around the origin
    return v;
}

My question is, can I extend the Vector3 class itself? At the moment I call the function like so:

var v3 = Vector3(130,290,210);
var wrappedV3 = WrapAround(v3); // returns (130,-70,-150)

But I would like instead to use:

var v3 = Vector3(130,290,210);
var wrappedV3 = v3.WrapAround; // returns (130,-70,-150)

Is there a way to do this? (In Javascript preferably)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · Nov 03, 2010 at 10:58 AM

Not possible in JS as far as I can tell (you can't do extension methods as you can't create static classes nor use the correct syntax for the function)

In c#:

public static class ExtensionMethods
{
    public static Vector3 WrapAround(this Vector3 v)
    {
        if (v.x >  180) { v.x -= 360; } // wrap around the origin
        if (v.y >  180) { v.y -= 360; } // wrap around the origin
        if (v.z >  180) { v.z -= 360; } // wrap around the origin
        if (v.x <- 180) { v.x += 360; } // wrap around the origin
        if (v.y <- 180) { v.y += 360; } // wrap around the origin
        if (v.z <- 180) { v.z += 360; } // wrap around the origin
        return v;
    }
}

If you slapped that class in the Assets/Plugins folder, it'd work as is from JS, used like your own example, albeit with the correct syntax for the function :P ( var wrappedV3 = v3.WrapAround(); )

Comment
Add comment · Show 5 · 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 · Nov 03, 2010 at 11:13 AM 0
Share

I get this error... Assets/$$anonymous$$y Assets/Scripts/Arms.js(612,58): BCE0019: 'WrapAround' is not a member of 'UnityEngine.Vector3'.

avatar image Mike 3 · Nov 03, 2010 at 11:23 AM 0
Share

Ugh - I just tested it again, and c# picks up the extension methods fine, but JS doesn't. In that case, I don't think there's a way at all with JS, sorry

avatar image robinking · Nov 03, 2010 at 11:27 AM 0
Share

Okay, never $$anonymous$$d - thanks for the info anyway.

avatar image Mike 3 · Nov 03, 2010 at 11:40 AM 1
Share

I would just make a static function in a shared class (e.g. Vector3Helpers.WrapAround) so that it's accessible from the entire project

avatar image robinking · Nov 03, 2010 at 12:42 PM 0
Share

That's what I've done :) thanks...

avatar image
0

Answer by Kyllan · Oct 20, 2012 at 08:31 AM

I don't know about JS in Unity3d, but perhaps you could use prototyping like in normal JS?

You'll need to do something like this:

 Vector3.prototype.constructor = function Vector3(x, y, z)
 {
     // from here call the base constructor of Vector3 and then do your 360 modifications afterwards.
 }

I'm a little rusty on the specifics, but I've done this before. You should be able to Google for it now that you know about prototyping in JS. Either way, if you're going to write complicated code in JS it's worth understanding prototyping; it's how object orientation is achieved in JS.

I do have two questions for you though (out of interest, not necessarily because I know better):

  • Why do you want to do this? There must be a reason why Unity3d doesn't work like that and you may run the danger of forcing it to work differently which will cause other problems down the line.

  • Why not use C# instead of JS?

Comment
Add comment · Show 2 · 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 narthur157 · Dec 13, 2012 at 02:41 AM 0
Share

I found this question with a google search, so for the purposes of archiving I am fairly sure that prototyping is NOT a feature of unityscript

avatar image Eric5h5 · Dec 13, 2012 at 03:57 AM 0
Share

Unityscript is not really that much like Javascript, so that's correct, there is no prototype. (Also the other answer is partially incorrect; you can do static classes in Unityscript. Not extension methods though.)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Access Variable in a class from another script 4 Answers

Return Angle as Vector 3 2 Answers

How to Typecast JS Variables as C# Classes? 0 Answers

Missing Assembly References. 0 Answers

how to call class details from another class.? 0 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