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
5
Question by Antony-Blackett · Apr 27, 2011 at 03:29 AM · inspectormaskpopup

Mask field in the editor

How do i make a mask selection popup like the layer selection popup you get in the inspector for any LayerMask, except for my own mask object.

EnumPopup is almost right but does not allow multiple selection.

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

8 Replies

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

Answer by Eric5h5 · Apr 27, 2011 at 03:30 AM

var mask : LayerMask;
Comment
Add comment · Show 3 · 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 Antony-Blackett · Apr 27, 2011 at 04:39 AM 0
Share

No I meant my own mask type. Layer$$anonymous$$ask will give me the layer options. I want my own custom options.

avatar image Eric5h5 · Apr 27, 2011 at 05:46 AM 0
Share

In that case I guess you'd have to write your own function for that.

avatar image Antony-Blackett · Apr 28, 2011 at 06:27 AM 0
Share

That's disappointing as the functionality is already written for the layermask selection... oh well I guess I'll do this at some point.

avatar image
12

Answer by TowerOfBricks · May 28, 2011 at 02:18 PM

I've written some code for this, unfortunately I cannot use the √ character to indicate a selected layer, I have to use [X] instead which looks a bit ugly. But it works. It does even support the Everything and Nothing selections (that's the 'showSpecial' flag).

 public static LayerMask LayerMaskField (string label, LayerMask selected) {
     return LayerMaskField (label,selected,true);
 }
 
 public static LayerMask LayerMaskField (string label, LayerMask selected, bool showSpecial) {
     
     List<string> layers = new List<string>();
     List<int> layerNumbers = new List<int>();
     
     string selectedLayers = "";
     
     for (int i=0;i<32;i++) {
         
         string layerName = LayerMask.LayerToName (i);
         
         if (layerName != "") {
             if (selected == (selected | (1 << i))) {
                 
                 if (selectedLayers == "") {
                     selectedLayers = layerName;
                 } else {
                     selectedLayers = "Mixed";
                 }
             }
         }
     }
     
     EventType lastEvent = Event.current.type;
     
     if (Event.current.type != EventType.MouseDown && Event.current.type != EventType.ExecuteCommand) {
         if (selected.value == 0) {
             layers.Add ("Nothing");
         } else if (selected.value == -1) {
             layers.Add ("Everything");
         } else {
             layers.Add (selectedLayers);
         }
         layerNumbers.Add (-1);
     }
     
     if (showSpecial) {
         layers.Add ((selected.value == 0 ? "[X] " : "     ") + "Nothing");
         layerNumbers.Add (-2);
         
         layers.Add ((selected.value == -1 ? "[X] " : "     ") + "Everything");
         layerNumbers.Add (-3);
     }
     
     for (int i=0;i<32;i++) {
         
         string layerName = LayerMask.LayerToName (i);
         
         if (layerName != "") {
             if (selected == (selected | (1 << i))) {
                 layers.Add ("[X] "+layerName);
             } else {
                 layers.Add ("     "+layerName);
             }
             layerNumbers.Add (i);
         }
     }
     
     bool preChange = GUI.changed;
     
     GUI.changed = false;
     
     int newSelected = 0;
     
     if (Event.current.type == EventType.MouseDown) {
         newSelected = -1;
     }
     
     newSelected = EditorGUILayout.Popup (label,newSelected,layers.ToArray(),EditorStyles.layerMaskField);
     
     if (GUI.changed && newSelected >= 0) {
         //newSelected -= 1;
         
         Debug.Log (lastEvent +" "+newSelected + " "+layerNumbers[newSelected]);
         
         if (showSpecial && newSelected == 0) {
             selected = 0;
         } else if (showSpecial && newSelected == 1) {
             selected = -1;
         } else {
             
             if (selected == (selected | (1 << layerNumbers[newSelected]))) {
                 selected &= ~(1 << layerNumbers[newSelected]);
                 //Debug.Log ("Set Layer "+LayerMask.LayerToName (LayerNumbers[newSelected]) + " To False "+selected.value);
             } else {
                 //Debug.Log ("Set Layer "+LayerMask.LayerToName (LayerNumbers[newSelected]) + " To True "+selected.value);
                 selected = selected | (1 << layerNumbers[newSelected]);
             }
         }
     } else {
         GUI.changed = preChange;
     }
     
     return selected;
 }
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 Antony-Blackett · May 30, 2011 at 09:08 PM 0
Share

Awesome, thank you I'll give it a try. Only I wanted this popup for any enum mask type. I'll see if I can modify this to do what I need. Thanks.

avatar image numberkruncher · May 28, 2012 at 05:04 PM 0
Share

I don't know if this will be of use to you, but please check my answer below. This might be a new feature of Unity which wasn't available previously.

avatar image numberkruncher · Jun 08, 2012 at 02:46 PM 0
Share

You can also create custom popup menus: http://unity3d.com/support/documentation/ScriptReference/Generic$$anonymous$$enu.html

avatar image ilya_ca · Nov 18, 2012 at 02:54 PM 0
Share

Unfortunately I cannot give you a "+" yet, but I really wanted to thank you for this solution!

avatar image Esteem · Apr 20, 2018 at 12:20 AM 0
Share

several years later, still helpful. Cheers buddy.

Show more comments
avatar image
6

Answer by Bentley · Aug 30, 2020 at 11:31 AM

This can now be done without any custom editor classes with the [Flags] attribute:

 using System;
 
 [System.Flags]
 public enum MyEnum
 {
      Nothing = 0,
      Val1 = 1,
      Val2 = 2,
      Val3 = 4,
      Val4 = 8
      Everything = 0b1111
 }

And your class/struct:

 [System.Serializable]
 public struct MyStruct
 {
     public MyEnum myValue;
 }

Result:

alt text http://pasteboard.co/JoKFdyji.png


flagattribute.png (7.2 kB)
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 the_real_ijed · Oct 25, 2021 at 11:50 AM 1
Share

[Flags] should be [System.Flags]

avatar image Bentley the_real_ijed · Oct 25, 2021 at 12:01 PM 0
Share

I didn't include it as Flags is not ambiguous in the context, but you're right—much better to be explicit, especially on an answers page. I've updated the answer to use System.Flags. Thanks!

avatar image
3

Answer by numberkruncher · May 28, 2012 at 04:15 PM

What about the EditorGUILayout.EnumMaskField? http://unity3d.com/support/documentation/ScriptReference/EditorGUILayout.EnumMaskField.html

Another option might be EditorGUILayout.MaskField if you want to define custom strings: http://unity3d.com/support/documentation/ScriptReference/EditorGUILayout.MaskField.html

Note: Specifying null seems to insert horizontal splitter (at expense of a flag)

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
2

Answer by ikriz · Aug 10, 2015 at 08:17 AM

I believe this is what your looking for: http://wiki.unity3d.com/index.php/EnumFlagPropertyDrawer

Turn your enum into a multi flag selection variable ;)

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 DungDajHjep · Feb 24, 2017 at 10:15 AM 0
Share

thank you very much !

  • 1
  • 2
  • ›

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

11 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

Related Questions

Light Cookie not in Inspector 0 Answers

Set the Rect of PopupWindowContent 1 Answer

Problem with MaskField ints 1 Answer

Field in PropertyDrawer isn't remembered 1 Answer

Inspector Popup format to look like tag or layer popup 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