Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Titan216 · Mar 06, 2016 at 06:06 AM · inspectoreditor-scriptingdrawermulti-object editing

How to correctly use EditorGUILayout.MaskField in multi-edit ?

In a custom inspector, I have a bitfield that I set with EditorGUILayout.MaskField, but I get a wierd behaviour when I'm multi-editing prefabs: the value of the last selected prefab overide the value of every prefabs, if I don't put the changeCheck, prefabs get messed as soon as I select them. Am I doing something wrong ?

     [CustomEditor(typeof(Module))]
     [CanEditMultipleObjects]
     public class ModuleDrawer : Editor
     {
         SerializedProperty _serializedBitField  ;
     
         void OnEnable()
         {
             _serializedBitField  = serializedObject.FindProperty("_bitField ");
         }
         public override void OnInspectorGUI()
         {
             DrawDefaultInspector();
         
             EditorGUI.BeginChangeCheck();
             _serializedBitField.intValue =  EditorGUILayout.MaskField("BitField", _serializedBitField.intValue, randomStringArray);
             if (EditorGUI.EndChangeCheck())
                 serializedObject.ApplyModifiedProperties();
         }









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

2 Replies

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

Answer by Bunny83 · Jul 06, 2018 at 11:26 AM

Well, multi object editing is generally a bit tricky. To avoid unwanted changes you must prevent the assignment to _serializedBitField.intValue. Whenever you assign a value here the serialized object / property will set the value for all objects represented by the SerializedObject.


One straight forward way would be:

 int newVal = EditorGUILayout.MaskField("BitField", _serializedBitField.intValue, randomStringArray);
 if (newVal !=_serializedBitField.intValue)
     _serializedBitField.intValue = newVal;

This way you only actually change the value when you tinker with the maskfield UI. However for multiobject editing it's usually common to visually show the user that the property has multiple values.


Most editor controls which support multiediting internally check SerializedProperty.hasMultipleDifferentValues and replace the shown content by a dash. Of course that is not possible with the maskfield. You may just display an additional warning box if "hasMultipleDifferentValues" is true. Another solution is to simply omit the whole maskfield if you're editing multiple objects with different values and just show an info box instead.


Note that if you're editing multiple different values, the value that is returned when you read intValue is just the value of the first object. Unfortunately the SerializedProperty doesn't provide a way to read or write the individual values.

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 Bunny83 · Jul 06, 2018 at 11:51 AM 0
Share

For example, have a look at Unity's implementation of the Popup field for SerializedProperties

avatar image
0

Answer by mapbox · Jul 06, 2018 at 12:04 AM

Here's a way to convert the generated int from the EditorGUILayout.MaskField back to the array https://answers.unity.com/questions/319940/maskfield-selected-values.html

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 Titan216 · Jul 06, 2018 at 08:17 AM 0
Share

Thanks but this question was about fixing a weird behavior when multi-editing objects with $$anonymous$$askField controls, not "how to convert bitfield into array". by the way, since last year, I never found the answer and got used to manage around this.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get notification of moving a GameObject in the hierachy when editing the scene? 1 Answer

Unity editor not saving object changes 4 Answers

Change only PrefixLabel with Property drawer, use default drawing for rest of property. 2 Answers

Creating a custom inspector utilizing a list of class instances with serialization? 1 Answer

Renaming Inspector 2D Array Element 0 , Element 1 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