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
0
Question by ChuckIes · Jan 29, 2016 at 01:34 AM · layerslayermask

How to get all layers included in a LayerMask?

I want to use LayerMasks in my script, but not for raycasting. I can make a public LayerMask variable which will appear in the inspector, but I'm not sure how to get the layer information out of the LayerMask.

In the end, I want something like this:

 // whether the layer was selected in the inspector or not
 bool[] layersArray = myLayerMask.GetLayers(); 
 
 for(int i = 0; i < layersArray.length; i++) {
     // do something with the layers
 }
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

4 Replies

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

Answer by brunocoimbra · Jan 29, 2016 at 02:31 AM

Can't test right know, but try that:

 [SerializeField]
 private LayerMask layerMask;

 private bool[] hasLayers = new bool[32];

 private void CheckMasks()
 {
     for (int i = 0; i < 32; i++)
     {
         if (layerMask == (layerMask | (1 << i)))
         {
             hasLayers [i] = true;
         }
     }
 }

EDIT: Already tested and working!

EDIT2: Here is two LayerMask extensions to help! Just create a c# script "LayerMaskExtensions" and copy paste the code.

 public static class LayerMaskExtensions
 {
     public static bool HasLayer(this LayerMask layerMask, int layer)
     {
         if (layerMask == (layerMask | (1 << layer)))
         {
             return true;
         }
 
         return false;
     }
 
     public static bool[] HasLayers(this LayerMask layerMask)
     {
         var hasLayers = new bool[32];
 
         for (int i = 0; i < 32; i++)
         {
             if (layerMask == (layerMask | (1 << i)))
             {
                 hasLayers[i] = true;
             }
         }
 
         return hasLayers;
     }
 }

To use it:

 public LayerMask layerMask;
 public int layer;
 
 void Method()
 {
     bool hasLayer = layerMask.HasLayer(layer);
     bool[] hasLayers = layerMask.HasLayers();
 }
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 ChuckIes · Jan 29, 2016 at 05:21 PM 0
Share

Thank you! That was a very good answer. And now I know about extension methods :D

avatar image
1

Answer by Bunny83 · Jan 29, 2016 at 02:45 AM

Uhm it's not really clear what you want to do with the layer information. The LayerMask struct can only handle the layers defined in the tag / layer manager. A layermask is simply an uint value which represents a bit mask where each bit of the 32 bit number represent one layer.

So a value of "6" would be: 00000110 in binary. As you can see bit 1 and 2 are set if you start counting at 0.

You can use usual bit operations to check for a specific bit in a bit mask.

 uint bitMask  = 0x00e5; // 0000 0000 1110 0101
 if ((bitMask & (1 << 3)) > 0) // check if bit 3 is set. In this case it is not set. Keep in mind the lowest bit is 0
 bitMask |= 1<<4; // set bit 4 --> 0001 0000
 // bitMask now contains        -> 1111 0101

See the following posts for more information:

  • how do i use layermasks

  • BitMask editor

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
0

Answer by MrBalin · Nov 05, 2019 at 12:30 AM

if you want to check against all layers, I found this substitute works: Physics.DefaultRaycastLayers

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
0

Answer by sj631 · Sep 01, 2021 at 10:24 AM

 LayerMask originalLayerMask = 2 | 4;
 LayerMask layerMaskToCheck = 2;
 LayerMask combinedLayerMask = originalLayerMask | layerMaskToCheck;
 
 if (combinedLayerMask.value == originalLayerMask.value)
    Debug.Log(layerMaskToCheck.value +" layer Found");


You can try this but keep raycast and collision layers different for performance reasons.

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

37 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

Related Questions

How to achieve this basic physics effect? Detect when to change layers 2 Answers

Setting Layer Weights properly 0 Answers

OverlapSphere doesn't detect colliders 1 Answer

Physics2D.CircleCast produces no result whenever a layer mask is specified 1 Answer

How do I use layermasks? 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