- Home /
Is there a way to override the Vector3 class or edit it?
I want to optimize the way it works , because i do not like creating a new Instance of the object (Vector3) each time I want to modify a single property.
Answer by tanoshimi · Feb 18, 2015 at 10:43 PM
Sure. Define an extension method.
Thought about them, but i really need to create some properties too. Vector3 class has a Set method but doesn't work for some reason.
The above is the answer to the question you asked. If you want to make altering some property easier (that can't already be easily altered), then you should use an extension method. It would be wrong to characterise this as "optimising" it, though. Your extension method would be using the methods that are already available, just wrapping them in something to make your life easier.
If (totally different question) you want to add fields as well as methods, your best bet (because Vector3 is sealed) might be to create a class that "wraps" a Vector3 ie has one as a member.
Vector3.Set does work, by the way. Are you by any chance trying to do something like transform.position.Set() in a GameObject? That won't change the object's position because the Vector3 is is being passed by value (so in that, Set() only changes the local copy). To change the transform's position you have to do transform.position = ... (or use one of Transform's member functions that modifies it of course).
I suspect the above to be the root of your confusion. It's very easy to change individual values of a Vector3 as others have pointed out. It's just that you may not be altering the Vector3 that you think you are.
I do understand and I know what optimising means, stop being condescending people! And what i wanted is to modify the class Vector3 but it is sealed i suppose i have to wait until unity actually let us do this... And thank you for answering.
I am truly sorry if you found it condescending, but the point of commenting on language is that sometimes it's not clear what people mean, and pointing out misuse of words can help point towards where they can make things clearer. Getting you to be clearer with your wording makes it easier for other people to assist.
The fact that it is so easy to change the basic individual properties of a Vector3 (as Zarentyx's answer points out) makes it difficult to be certain what it is you're actually looking for. You may have actually been looking for an optimisation, which is why I said that one of my interpretations of your question wouldn't count as optimisation.
It's still not clear what you're looking for. So can you give an example of a property of a Vector3 that you want to be able to change, that isn't already very easy to change?
Answer by Pangamini · Aug 06, 2019 at 12:59 PM
You don't have to worry about creating an instance of a value type (a struct), as long as you are not boxing it . Also, you can easily overwrite the single element of the vector, it's x,y,z are public variables
I'm pretty sure after 4 years he doesn't really struggle with this anymore ^^. The issue he might had referred to is probably the fact that you can not change individual components directly when you're working on properties like Transform.position or Transform.eulerAngles.
So this does not work:
transform.position.x = 5;
and you have to do
Vector3 tmp = transform.position;
tmp.x = 5;
transform.position = tmp;
Though I'm pretty sure he now understands value types, properties and that "new Vector3" does not create a "new object". Anyways +1 even though it doesn't address the question title but the indirect question in the description.
Answer by Jessespike · Feb 18, 2015 at 10:50 PM
You can write extension methods. http://shattereddeveloper.blogspot.ca/2012/10/making-use-of-c-extension-methods-in.html
I wouldn't call these optimizations, you're actually adding complexity. The difference is negligible though, so don't worry about it. There's actually a good reason why Vector3 behaves the way it does ( http://forum.unity3d.com/threads/is-new-vector3-x-y-z-costly.105480/ )