Search:

  • Portfolio
  • Blog
  • About
  • Contact

LayerMasks simplified

Posted date:  January 31, 2012  |  3 Comments


January 31, 2012

I love LayerMasks because of their power and ability to filter things. I hate LayerMasks because I can never remember how to properly create them in code. To end this love/hate relationship I finally sat down and made a solution that’s incredibly simple and invaluable for taming LayerMasks. You too can now punch LayerMasks in their confusing face.

//Set a camera to only look at layer 7:
void Awake (){
	//by layer id:
	camera.cullingMask = LayerMaskHelper.OnlyIncluding( 7 );

	//or by layer name:
	camera.cullingMask = LayerMaskHelper.OnlyIncluding( LayerMask.NameToLayer("UserInterface") );
}

//Set a light to affect layers 4, 8 and 11:
void Awake (){
	//LayerMaskHelper doesn't care if you give them in order
	//by id:
	light.cullingMask = LayerMaskHelper.OnlyIncluding( 4, 11, 8 ); 

	//or by layer names:
	light.cullingMask = LayerMaskHelper.OnlyIncluding( LayerMask.NameToLayer("Landscape"), LayerMask.NameToLayer("Player"), LayerMask.NameToLayer("Enemies") );
}

//Set a light to affect everything except for layers 5 and 6:
void Awake (){
	//by layer id:
	light.cullingMask = LayerMaskHelper.EverythingBut( 5, 6 );

	//or by layer names:
	light.cullingMask = LayerMaskHelper.EverythingBut( LayerMask.NameToLayer("Landscape"), LayerMask.NameToLayer("Player") );
}

Post to Twitter


3 Comments

DBK

Ha, I just went through a whole thing with changing layer masks on the fly. Your “helpers” are always welcome! Thanks a lot PixelPlacement!

Reply

Alessandro Cominu

Great little utility! I’m using it inside Blackreef Pirates! thanks

Reply

    pixelplacement

    YARRRRR!!! Glad I could help.

    Reply


Wanna say something?





  Cancel Reply


  • Recent Posts

    • Simple State Saving
      Saving the state of a GameObject(read MonoBehavior) via traditional serialization...
    • The best darn throwable object spinner
      Nothing more I can say about this release other than its a draggable object rotator...
    • Unity needed cmd-g and ctrl-g
      I've found my left hand hitting the trusty "make group" key combination in Unity...
  • Categories

    • Augmented Reality
    • General Babble
    • Unity





© 2012 Bob Berkebile (PixelPlacementâ„¢) and additional respective holders all content used with permission