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
1
Question by GamesDeveloper12 · Sep 28, 2015 at 03:03 PM · editorinspectorlayermaskserializedpropertyproperty

Custom Inspector Layer Mask variable

Hi,

I am trying to create a custom editor script for the inspector. I have managed to create the fields for my floats and booleans but cannot work out how i can create a field for my layermask. I have tried using "serializedObject" but could not get it to work.

Can someone show me how i can create a field for my layer mask in my custom inspector ?

Thanks

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
6
Best Answer

Answer by GamesDeveloper12 · Sep 28, 2015 at 03:38 PM

I managed to resolve the issue through looking through the code of a plugin i own.

I created a script called "EditorTools":

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EditorTools{
 
     static List<string> layers;
     static string[] layerNames;
 
     public static LayerMask LayerMaskField (string label, LayerMask selected) {
 
             if (layers == null) {
                 layers = new List<string>();
                 layerNames = new string[4];
             } else {
                 layers.Clear ();
             }
             
             int emptyLayers = 0;
             for (int i=0;i<32;i++) {
                 string layerName = LayerMask.LayerToName (i);
                 
                 if (layerName != "") {
                     
                     for (;emptyLayers>0;emptyLayers--) layers.Add ("Layer "+(i-emptyLayers));
                     layers.Add (layerName);
                 } else {
                     emptyLayers++;
                 }
             }
             
             if (layerNames.Length != layers.Count) {
                 layerNames = new string[layers.Count];
             }
             for (int i=0;i<layerNames.Length;i++) layerNames[i] = layers[i];
         
         selected.value =  EditorGUILayout.MaskField (label,selected.value,layerNames);
         
         return selected;
     }
 
 
 }

and then in my editor "OnInspectorGUI" function i call:

 test.Mask = EditorTools.LayerMaskField("Mask",test.Mask);

test being the name of the script im targeting.

Hope this helps other people who have the same problem :)

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 flamewave000 · Jun 10, 2017 at 07:26 PM

I was recently trying to do this and found a little bit more of a streamlined approach. I made two methods which convert a MaskField to a LayerMask and vice versa. Then you could use them inline with your regular gui script.

 // Converts the field value to a LayerMask
 private LayerMask FieldToLayerMask(int field)
 {
     LayerMask mask = 0;
     var layers = InternalEditorUtility.layers;
     for (int c = 0; c < layers.Length; c++)
     {
         if ((field & (1 << c)) != 0)
         {
             mask |= 1 << LayerMask.NameToLayer(layers[c]);
         }
     }
     return mask;
 }
 // Converts a LayerMask to a field value
 private int LayerMaskToField(LayerMask mask)
 {
     int field = 0;
     var layers = InternalEditorUtility.layers;
     for (int c = 0; c < layers.Length; c++)
     {
         if ((mask & (1 << LayerMask.NameToLayer(layers[c]))) != 0)
         {
             field |= 1 << c;
         }
     }
     return field;
 }

You can then use the methods as so:

 EditorGUI.BeginChangeCheck();
 var layersSelection = EditorGUILayout.MaskField("Layers", LayerMaskToField(script.layerMask), InternalEditorUtility.layers);
 if (EditorGUI.EndChangeCheck())
 {
     Undo.RecordObject(script, "Layers changed");
     script.layerMask= FieldToLayerMask(layersSelection);
 }

I don't know the efficiency of the script, but it didn't matter too much to me since it's for the editor and not the game itself :P

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

33 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

Related Questions

Exposing Enums in Custom Inspector 1 Answer

Property values changed by custom editor are reset when start playing 2 Answers

Creating vertical space for EditorGUI 2 Answers

How to assign a field within a PropertyDrawer? 1 Answer

expose public property (not variable) in inspector 9 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