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
0
Question by smeffles · Mar 24, 2012 at 07:34 PM · vector3api

Question regarding how Vector3 works

I'm currently learning C++ and as an exercise I tried to duplicate the Vector3 structure. However, when changing the x, y, or z variables, I was unable to have other variables such as magnitude and normalized change. This is because, to my knowledge, C++ does not support properties.

I thought Unity3D had its API classes and structures built using C++, so how do they support changing the magnitude and normalized variables when a component variable changes?

I know that understanding how the API works isn't vital for using Unity3D, but I'm curious and since this directly relates to the Unity API, I thought I would ask it here.

Comment
Add comment · Show 1
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 Eric5h5 · Mar 24, 2012 at 08:27 PM 2
Share

I thought Unity3D had its API classes and structures built using C++

A lot of it is built on $$anonymous$$ono. This question would be better posted on the forums though.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Mar 25, 2012 at 01:58 AM

Just to clarify a little bit. The Vector3 struct only contains 3 data members: x, y, z and nothing else. The Vector3 struct itself is a Mono type, however if native code function get passed a Vector3, C++ will receive a pointer to 3 floats. The memory foodprint of the struct looks exactly the same in C++.

If you want to dig a bit deeper to understand how Unity's scripting environment works, just download ILSpy which is an open source C# / CIL reflector. Open UnityEngine.dll and / or UnityEditor.dll to see how the classes are implemented. A lot functions / properties map directly to native code so you can't go deeper than that, but like Eric mentioned a lot stuff is done directly in managed code.

Only a few methods of Vector3 are native code functions (Slerp, OrthoNormalize, RotateTowards). Everything else is pure managed code.

On the other hand most stuff in the Transform-class is native code, even the .position property:

 public Vector3 position
 {
     get
     {
         Vector3 result;
         this.INTERNAL_get_position(out result);
         return result;
     }
     set
     {
         this.INTERNAL_set_position(ref value);
     }
 }

This is the magnitude property:

 public float magnitude
 {
     get
     {
         return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
     }
 }

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
avatar image
1

Answer by rutter · Mar 24, 2012 at 08:39 PM

Values such as magnitude ultimately depend on the individual components of the vector. Someone building a vector library has two main choices:

  • Each time someone asks for the magnitude, re-calculate it based on the current components

  • Each time someone changes a component, cache the current magnitude

The first case would be mostly driven by accessor method which calculate values on "get".

The second case would be mostly driven by mutator methods which calculate values on "set".

If you add caching or other "lazy" operations, it can get quite complicated.

Comment
Add comment · Show 6 · 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 smeffles · Mar 24, 2012 at 09:12 PM 0
Share

Right, I'm aware of how it can be done in C++, but Unity doesn't seem to use those methods. In Unity, you can simply change the x component, for example, using "vec3.x = 100;" as long as you're not using C#, and the magnitude and normalized variables will change automatically.

avatar image rutter · Mar 24, 2012 at 10:26 PM 1
Share

A lot of people overlook one of the most powerful elements in C# properties: they are implicitly functions.

Supposing we wanted to implement the second method I described above, we could do something like this:

 private float _x = 0f;
 public float x
 {
     get
     {
         return _x;
     }
     set
     {
         _x = value;
         Recalc(); //rebuild other values
     }
 }

To someone using your interface, x looks just like a float which you can get/set normally, but setting it can also cause other operations to fire.

avatar image smeffles · Mar 25, 2012 at 12:15 AM 0
Share

If Vector3 was developed in C#, that would definitely explain it, but my understanding was the Unity API was created with C++ and only the editor was created with C#. Am I wrong in that assumption?

avatar image Eric5h5 · Mar 25, 2012 at 12:22 AM 1
Share

Yes, you're wrong. Lots of the API is built on $$anonymous$$ono; look at the $$anonymous$$athf class for example, or the terrain engine, or OnGUI, etc.

avatar image Bunny83 · Mar 25, 2012 at 02:27 AM 1
Share

@smeffles: It's actually the other way round ;) The Unity engine is written mostly in C++, but the API is written in managed code (UnityEngine.dll). The editor is also written in C++. The editor is actually just a extension of the engine itself. Each window in the editor has it's own device context. You can draw everything in any window inside the editor ;)

The UnityEditor.dll contains most classes that made up the editor. The editor is some kind of a "special Unity game / application" that runs on a modified version of the engine.

Note: that are just my interpretations of what i know from the outside about Unity. Don't take my words for granted.

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find the Vector to divert from colliding? 1 Answer

Accessing game object based on position 2 Answers

How to make a Race Standing / Position Indicator 0 Answers

I thought I was doing this right, but it gives me an error. 0 Answers

Vector3 is always subject to serious stutter in Translation 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