Layers

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by KeliHlodversson)
Line 1: Line 1:
 +
by Ray Nothnagel
 +
=Description=
 +
A static class designed to ease layer usage.
  
 +
=Usage=
 +
<javascript>
 +
//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]);
 +
</javascript>
 +
 +
If you add more layers, this becomes even more handy as you no longer need to think about bitwise math to create complex masks.
 +
<javascript>
 +
//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]);
 +
</javascript>
 +
 +
=Layers.js=
 +
<javascript>
 +
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;
 +
}</javascript>

Revision as of 19:04, 19 October 2009

by Ray Nothnagel

Description

A static class designed to ease layer usage.

Usage

<javascript> //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]); </javascript>

If you add more layers, this becomes even more handy as you no longer need to think about bitwise math to create complex masks. <javascript> //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]); </javascript>

Layers.js

<javascript> 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; }</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox