Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to remove a single layer from cullingmask

Discussion in 'Scripting' started by TRuoss, Jun 13, 2016.

  1. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    Hi,

    i have a culling mask with multiple Layers selected. for example: 0, 8, 9, 10

    In game i create a second Camera that copies that culling mask, so i don´t / can´t predefine it in the inspector. The problem i can not solve is, how do i remove that single layer from my copied cullingmask. so the camera sees 0, 8, and 10.
    I do not understand that bit shifting so i ask here, if someone can help me to remove that layer 9 from my copied culling mask.

    Thanks in Advance.
     
    SamFernGamer4k likes this.
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    If you want to make layer 9 visible:
    Code (csharp):
    1. var newMask = oldMask | (1 << 9);
    If you want to make layer 9 invisible:
    Code (csharp):
    1. var newMask = oldMask & ~(1 << 9);
     
  3. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    Thank you, that did the job.
     
    arda_bpg likes this.
  4. yotingo

    yotingo

    Joined:
    Jul 10, 2013
    Posts:
    44
    This helped me too. Thanks!
     
    makeshiftwings likes this.
  5. Piyush_Pandey

    Piyush_Pandey

    Joined:
    Aug 29, 2014
    Posts:
    2
    This worked for me too. Thanks makeshiftwings !
     
    makeshiftwings likes this.
  6. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    75
    Thanks :) !
     
    makeshiftwings likes this.
  7. dimmduh1

    dimmduh1

    Joined:
    Feb 5, 2021
    Posts:
    24
    Thanks
    Wrote extension methods
    Code (CSharp):
    1.     public static int AddLayerToLayerMask(this int layerMask, int layer)
    2.     {
    3.         return layerMask | 1 << layer;
    4.     }
    5.    
    6.     public static int RemoveLayerFromLayerMask(this int layerMask, int layer)
    7.     {
    8.         return layerMask & ~(1 << layer);
    9.     }
    10.  
    11.     public static void AddLayerToCullingMask(this Camera camera, int layer)
    12.     {
    13.         camera.cullingMask |= 1 << layer;
    14.     }
    15.    
    16.     public static void RemoveLayerFromCullingMask(this Camera camera, int layer)
    17.     {
    18.         camera.cullingMask &= ~(1 << layer);
    19.     }
     
    antonsem, louis_sogeti and Eddycted like this.