Layers

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
Line 11: Line 11:
 
//make the camera render everyting but that layer
 
//make the camera render everyting but that layer
 
camera.cullingMask = Layers.CreateExclusiveMask([Layers.ignoreRaycast]);
 
camera.cullingMask = Layers.CreateExclusiveMask([Layers.ignoreRaycast]);
</javascript>
+
</syntaxhighlight>
  
 
If you add more layers, this becomes even more handy as you no longer need to think about bitwise math to create complex masks.
 
If you add more layers, this becomes even more handy as you no longer need to think about bitwise math to create complex masks.
Line 18: Line 18:
 
//the camera will render only objects in those two layers
 
//the camera will render only objects in those two layers
 
camera.cullingMask = Layers.CreateInclusiveMask([Layers.player, Layers.landscape]);
 
camera.cullingMask = Layers.CreateInclusiveMask([Layers.player, Layers.landscape]);
</javascript>
+
</syntaxhighlight>
  
 
=Layers.js=
 
=Layers.js=
Line 40: Line 40:
 
}
 
}
 
return ~m;
 
return ~m;
}</javascript>
+
}</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

by Ray Nothnagel

[edit] Description

A static class designed to ease layer usage.

[edit] 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]);

[edit] 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