Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Saripsis · Feb 16, 2017 at 07:41 AM · variablequaternioninspector

Use Euler angles for Quaternion variable in the Inspector

I have a Quaternion variable that I declared as public so I could edit it through the inspector. But since I don't know how quaternions work, I would like to us Euler angles. Is there a way to have the editor show the values in Euler rather than quaternion?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Adam-Mechtley · Feb 16, 2017 at 08:13 AM

I would recommend creating a custom PropertyAttribute and PropertyDrawer.

For example, given a PropertyAttribute like this:

 using UnityEngine;
 
 public class EulerAnglesAttribute : PropertyAttribute {}

You could create a PropertyDrawer like this:

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer(typeof(EulerAnglesAttribute))]
 public class EulerAnglesDrawer : PropertyDrawer {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
         return EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1f : 2f);
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
         Vector3 euler = property.quaternionValue.eulerAngles;
         EditorGUI.BeginProperty(position, label, property);
         EditorGUI.BeginChangeCheck();
             euler = EditorGUI.Vector3Field(position, label, euler);
         if (EditorGUI.EndChangeCheck())
           property.quaternionValue = Quaternion.Euler(euler);
         EditorGUI.EndProperty();
     }
 }

You would then decorate your field like this:

 using UnityEngine;

 public class MyComponent : MonoBehaviour {
     [SerializeField, EulerAngles]
     Quaternion m_Orientation;
 }

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 Suminsky · Mar 05, 2018 at 08:19 PM 0
Share

Hi, this however is not like the transform inspector as it snaps the euler to 0-359, it doesnt allow negative eulers.. I tried creating variables for the attribute, but it affects all of the attributes (for example, inside a list all of them would use the same vars, as expected..)

So really, how to make it work as the transform inspector? If the only way is creating a custom quaternion struct thats pretty lame..

avatar image Bunny83 Suminsky · Mar 06, 2018 at 12:45 AM 1
Share

You can't just "make it" work like the transform inspector as the transform holds an internal cached eulerangles value for the inspector. Usually the easiest solution is to just use a Vector3 value ins$$anonymous$$d of a quaternion and just convert the vector3 into a quaternion when you want to use it.

 public Vector3 rotationEuler;
 public Quaternion rotation
 {
     get { return Quaternion.Euler(rotationEuler); }
 }

$$anonymous$$eep in $$anonymous$$d that a Quaternion is a completely different way to represent a rotation compared to eulerangles. Euler angles are 3 seperate rotations which are executed one after another while a Quaternion directly represents a rotation as a direction vector and and additional "rotation value". Quaternions aren't meant to be easy "editable" by a user. It's just a way better approach to do rotations.


If you want to know more about Quaternions i suggest you have a look at the Numberphile video on Quaternions

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

68 People are following this question.

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

Related Questions

MouseLookPlus Script- can't access certain variables in the inspector view 1 Answer

Is it possible to show Static Variables in the Inspector? 10 Answers

Javascript and C#, different behaviour in inspector ? 1 Answer

How to expose variables in c# according to selected booleans 1 Answer

How can i assign prefab to variable without drag & drop from project to inspector. 2 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