Last active
July 4, 2019 10:52
-
-
Save Shilo/22bbb14c60f598d51e276d9e64196024 to your computer and use it in GitHub Desktop.
Unity camera extensions to hide/show specific layers.
This file contains 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
using UnityEngine; | |
public static class CameraExtensions { | |
public static void LayerCullingSetVisible(this Camera cam, int layerMask, bool visible) { | |
if (visible) LayerCullingShow(cam, layerMask); | |
else LayerCullingHide(cam, layerMask); | |
} | |
public static void LayerCullingSetVisible(this Camera cam, string layer, bool visible) { | |
if (visible) LayerCullingShow(cam, layer); | |
else LayerCullingHide(cam, layer); | |
} | |
public static void LayerCullingShow(this Camera cam, int layerMask) { | |
cam.cullingMask |= layerMask; | |
} | |
public static void LayerCullingShow(this Camera cam, string layer) { | |
LayerCullingShow(cam, 1 << LayerMask.NameToLayer(layer)); | |
} | |
public static void LayerCullingHide(this Camera cam, int layerMask) { | |
cam.cullingMask &= ~layerMask; | |
} | |
public static void LayerCullingHide(this Camera cam, string layer) { | |
LayerCullingHide(cam, 1 << LayerMask.NameToLayer(layer)); | |
} | |
public static void LayerCullingToggle(this Camera cam, int layerMask) { | |
cam.cullingMask ^= layerMask; | |
} | |
public static void LayerCullingToggle(this Camera cam, string layer) { | |
LayerCullingToggle(cam, 1 << LayerMask.NameToLayer(layer)); | |
} | |
public static bool LayerCullingIncludes(this Camera cam, int layerMask) { | |
return (cam.cullingMask & layerMask) > 0; | |
} | |
public static bool LayerCullingIncludes(this Camera cam, string layer) { | |
return LayerCullingIncludes(cam, 1 << LayerMask.NameToLayer(layer)); | |
} | |
public static void LayerCullingToggle(this Camera cam, int layerMask, bool isOn) { | |
bool included = LayerCullingIncludes(cam, layerMask); | |
if (isOn && !included) { | |
LayerCullingShow(cam, layerMask); | |
} else if (!isOn && included) { | |
LayerCullingHide(cam, layerMask); | |
} | |
} | |
public static void LayerCullingToggle(this Camera cam, string layer, bool isOn) { | |
LayerCullingToggle(cam, 1 << LayerMask.NameToLayer(layer), isOn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment