Layers

From Unify Community Wiki
Jump to: navigation, search

by Ray Nothnagel

Description

A static class designed to ease layer usage.

Usage

//set the layer to 2 (ignore raycast)
gameObject.layer = Layers.ignoreRaycast;
//make the camera render only objects in the ignore raycast layer
camera.cullingMask = Layers.CreateInclusiveMask([Layers.ignoreRaycast]);
//make the camera render everyting but that layer
camera.cullingMask = Layers.CreateExclusiveMask([Layers.ignoreRaycast]);

If you add more layers, this becomes even more handy as you no longer need to think about bitwise math to create complex masks.

//this assumes you've added "player" and "landscape" layer variables to Layers.js
//the camera will render only objects in those two layers
camera.cullingMask = Layers.CreateInclusiveMask([Layers.player, Layers.landscape]);

Layers.js

static var ignoreRaycast = 2;
//any other layers you would like to access can go here
//static var player = 8;
 
static function CreateInclusiveMask(layers : int[]) : LayerMask{
	var m : int = 0;
	for (l=0;l<layers.length;l++) {
		m |= (1<<layers[l]);
	}
	return m;
}
 
static function CreateExclusiveMask(layers : int[]) : LayerMask{
	var m : int = 0;
	for (l=0;l<layers.length;l++) {
		m |= (1<<layers[l]);
	}
	return ~m;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox