- Home /
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)
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(); )
I get this error... Assets/$$anonymous$$y Assets/Scripts/Arms.js(612,58): BCE0019: 'WrapAround' is not a member of 'UnityEngine.Vector3'.
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
Okay, never $$anonymous$$d - thanks for the info anyway.
I would just make a static function in a shared class (e.g. Vector3Helpers.WrapAround) so that it's accessible from the entire project
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?
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
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
Follow this Question
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