Last active
October 12, 2016 23:39
-
-
Save birdinforest/c919e98e96083d353d8e to your computer and use it in GitHub Desktop.
Tag:Untiy3D, LayerMask, Bitshift, Ignored layer, Bitmask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Turn on the bit using an OR operation: | |
private void Show() { | |
camera.cullingMask |= 1 << LayerMask.NameToLayer("SomeLayer"); | |
} | |
// Turn off the bit using an AND operation with the complement of the shifted int: | |
private void Hide() { | |
camera.cullingMask &= ~(1 << LayerMask.NameToLayer("SomeLayer")); | |
} | |
// Toggle the bit using a XOR operation: | |
private void Toggle() { | |
camera.cullingMask ^= 1 << LayerMask.NameToLayer("SomeLayer"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int _player = -1; | |
static public int player | |
{ | |
get | |
{ | |
if (_player == -1) | |
_player = LayerMask.NameToLayer("Player"); | |
return _player; | |
} | |
} | |
static int _npc = -1; | |
static public int npc | |
{ | |
get | |
{ | |
if (_npc == -1) | |
_npc = LayerMask.NameToLayer("NPC"); | |
return _npc; | |
} | |
} | |
static int _platform = -1; | |
static public int platform | |
{ | |
get | |
{ | |
if (_platform == -1) | |
_platform = LayerMask.NameToLayer("Platform"); | |
return _platform; | |
} | |
} | |
static int _onewayPlatform = -1; | |
static public int onewayPlatform | |
{ | |
get | |
{ | |
if (_onewayPlatform == -1) | |
_onewayPlatform = LayerMask.NameToLayer("OnewayPlatform"); | |
return _onewayPlatform; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Combine layers | |
public LayerMask ignoreLayer = LayerCacher.player | LayerCacher.npc; | |
public LayerMask platformMask = LayerCacher.platform; | |
public LyaerMask onewayPlatformMask = LayerCacher.oncewayPlatform; | |
void OnTriggerStay2D(Collider2D other) | |
{ | |
// Check whether this object's layer should be ignored. | |
LayerMask mask = 1 << other.gameObject.layer; // Bitshift | |
bool isIgnored = (mask & ignoreLayer) == mask; // Do AND. If combined layermask contains this layer, the result should be the same to it self. | |
if (!colliders.Contains(other) && !isIgnored) | |
{ | |
colliders.Add(other); | |
} | |
LayerMask twoWayPlatform = platformMask & ~onewayPlatform; // Get all objects which on platform layer but not on one way platform layer. | |
} | |
// Ref: http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment