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 EDartBlade · Jul 24, 2020 at 02:20 PM · editor-scriptingcomponents

How do you edit default components?

If possible, how do you edit default unity-made components, like the Transform component.


If you want to know, I'd like to add a custom property to the Transform component in unity, which would return the game object's position as a Vector2 for my 2D game.

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
0

Answer by smark12007 · Jul 24, 2020 at 06:21 PM

https://wiki.unity3d.com/index.php/TransformInspector

This just looks like the default one.

 [CanEditMultipleObjects, CustomEditor(typeof(Transform))]
 public class TransformInspector : Editor {
  
     private const float FIELD_WIDTH = 212.0f;
     private const bool WIDE_MODE = true;
  
     private const float POSITION_MAX = 100000.0f;
  
     private static GUIContent positionGUIContent = new GUIContent(LocalString("Position")
                                                                  ,LocalString("The local position of this Game Object relative to the parent."));
     private static GUIContent rotationGUIContent = new GUIContent(LocalString("Rotation")
                                                                  ,LocalString("The local rotation of this Game Object relative to the parent."));
     private static GUIContent scaleGUIContent    = new GUIContent(LocalString("Scale")
                                                                  ,LocalString("The local scaling of this Game Object relative to the parent."));
  
     private static string positionWarningText = LocalString("Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range.");
  
     private SerializedProperty positionProperty;
     private SerializedProperty rotationProperty;
     private SerializedProperty scaleProperty;
  
     private static string LocalString(string text) {
         return LocalizationDatabase.GetLocalizedString(text);
     }
  
     public void OnEnable() {
         this.positionProperty = this.serializedObject.FindProperty("m_LocalPosition");
         this.rotationProperty = this.serializedObject.FindProperty("m_LocalRotation");
         this.scaleProperty    = this.serializedObject.FindProperty("m_LocalScale");
     }
  
     public override void OnInspectorGUI() {
         EditorGUIUtility.wideMode = TransformInspector.WIDE_MODE;
         EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth - TransformInspector.FIELD_WIDTH; // align field to right of inspector
  
         this.serializedObject.Update();
  
         EditorGUILayout.PropertyField(this.positionProperty, positionGUIContent);
         this.RotationPropertyField(   this.rotationProperty, rotationGUIContent);
         EditorGUILayout.PropertyField(this.scaleProperty,    scaleGUIContent);
  
         if (!ValidatePosition(((Transform) this.target).position)) {
             EditorGUILayout.HelpBox(positionWarningText, MessageType.Warning);
         }
  
         this.serializedObject.ApplyModifiedProperties();
     }
  
     private bool ValidatePosition(Vector3 position) {
         if (Mathf.Abs(position.x) > TransformInspector.POSITION_MAX) return false;
         if (Mathf.Abs(position.y) > TransformInspector.POSITION_MAX) return false;
         if (Mathf.Abs(position.z) > TransformInspector.POSITION_MAX) return false;
         return true;
     }
  
     private void RotationPropertyField(SerializedProperty rotationProperty, GUIContent content) {
         Transform transform = (Transform) this.targets[0];
         Quaternion localRotation = transform.localRotation;
         foreach (UnityEngine.Object t in (UnityEngine.Object[]) this.targets) {
             if (!SameRotation(localRotation, ((Transform) t).localRotation)) {
                 EditorGUI.showMixedValue = true;
                 break;
             }
         }
  
         EditorGUI.BeginChangeCheck();
  
         Vector3 eulerAngles = EditorGUILayout.Vector3Field(content, localRotation.eulerAngles);
  
         if (EditorGUI.EndChangeCheck()) {
             Undo.RecordObjects(this.targets, "Rotation Changed");
             foreach (UnityEngine.Object obj in this.targets) {
                 Transform t = (Transform) obj;
                 t.localEulerAngles = eulerAngles;
             }
             rotationProperty.serializedObject.SetIsDifferentCacheDirty();
         }
  
         EditorGUI.showMixedValue = false;
     }
  
     private bool SameRotation(Quaternion rot1, Quaternion rot2) {
         if (rot1.x != rot2.x) return false;
         if (rot1.y != rot2.y) return false;
         if (rot1.z != rot2.z) return false;
         if (rot1.w != rot2.w) return false;
         return true;
     }
 }

Modify the method, OnInspectorGUI, to approach whatever you need. Maybe you'll need to call EditorGUILayout.Vector2Field() on your own.

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

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

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

Related Questions

Find all components in project 1 Answer

way to get all components of type in all scenes and prefabs 1 Answer

(Editor Scripting) Is There A Way To Mimic The "Add Component" Menu?? 1 Answer

Save Components setting for reuse 1 Answer

RequireComponent with custom public vars? 1 Answer


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