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 /
  • Help Room /
avatar image
0
Question by supremegrandruler · Mar 17, 2016 at 12:12 PM · c#editoreditor-scripting

Why am I getting this strange behaviour?

I made my own transform component and I want it to hide the default Unity transform when added to a game object. This is the editor code I have where "Transform" = my own transform.

  [CustomEditor(typeof(Transform))]
     public sealed class TransformEditor : UnityEditor.Editor
     {
         private UnityEngine.Transform _transform;
         private HideFlags _hideFlags;
 
         void OnEnable()
         {
             Debug.Log("Enabled");
             var dTransform = (Transform) target;
             _transform = dTransform.transform;
             _hideFlags = _transform.hideFlags;
             _transform.hideFlags = HideFlags.HideInInspector;
             Debug.Log("Flags = " + _transform.hideFlags);
         }
 
         void OnDestroy()
         {
             Debug.Log("Flags = " + _hideFlags);
             _transform.hideFlags = _hideFlags;
         }
 }

For some reason what ends up happening is when I place my own transform on a game object, nothing happens, but when I remove it, the Unity transform becomes hidden.

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
1

Answer by Bunny83 · Mar 17, 2016 at 12:37 PM

First of all it's not a good idea to name your own Transform after a built in component. However if the namespaces are carefully choosen it should work but can cause a lot of namespace problems later on.

Since you change something on the transform component you should use Undo.RecordObject to make Unity aware of a change and you might have to redraw the inspector window.

Furthermore you should never "set" the hideflags to a value. The hideflags is a bitmask so you should always enable / disable certain bits by using bit operations.

Also note that OnEnable and OnDestroy are related to your Editor, not to your component.

 // set the bit "HideInInspector"
 _transform.hideFlags |= HideFlags.HideInInspector;
 
 // reset the bit
 _transform.hideFlags &= ~HideFlags.HideInInspector;

Of course you can store the whole bitflag and assign it back, but i don't see the point in setting and removing the hideflags each time a custom editor shows your transform or when it's closed. I guess you want to set the hideflags inside your Transform components Awake / OnEnable method and reset it in OnDestroy.

Comment
Add comment · Show 1 · 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 supremegrandruler · Mar 17, 2016 at 01:14 PM 0
Share

Thanks for your reply. This is my new code:

         void OnEnable()
         {
             var dTransform = (Transform) target;
             _transform = dTransform.transform;
             _transform.hideFlags |= HideFlags.HideInInspector;
             Debug.Log("Enable Flags = " + _transform.hideFlags);
             //Undo.RecordObject(_transform, "");
             Repaint();
         }
 
         void OnDestroy()
         {
           _transform.hideFlags &= ~HideFlags.HideInInspector;
         }

But when I add my component, it's not hidden in the inspector.

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

112 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

Related Questions

Loading Json from Resources via Editor Script 1 Answer

How to Have a ReoderableList in a Custom Editor Window 1 Answer

DrawGizmo - Accessing Editor script in DrawGizmo 1 Answer

How to read from editor preferences? 0 Answers

Gizmos and Handles 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