Skip to content

Instantly share code, notes, and snippets.

@sisso
Last active August 29, 2015 14:21
Show Gist options
  • Save sisso/9033c30aae3f285e2826 to your computer and use it in GitHub Desktop.
Save sisso/9033c30aae3f285e2826 to your computer and use it in GitHub Desktop.
#pragma strict
var next : KeyCode = KeyCode.RightArrow;
var previous : KeyCode = KeyCode.LeftArrow;
var disableObject: boolean = false;
private var current = 0;
private function SetRenderersEnabled(obj: Transform, enabled: boolean) {
if (disableObject) {
obj.gameObject.SetActive(enabled);
}
else {
for (var comp: Renderer in obj.GetComponentsInChildren(Renderer)) {
comp.enabled = enabled;
}
}
}
private function ShowCurrent() {
var currentObject = transform.GetChild(current);
SetRenderersEnabled(currentObject, true);
}
private function ShowCurrentOnly() {
HideAll();
ShowCurrent();
}
private function HideAll() {
for (var child : Transform in transform) {
SetRenderersEnabled(child, false);
}
}
private function ShowNext() {
current = current + 1;
if (current >= transform.childCount) current = 0;
ShowCurrentOnly();
}
private function ShowPrevious() {
current = current - 1;
if (current < 0) current = transform.childCount - 1;
ShowCurrentOnly();
}
function Awake() {
ShowCurrentOnly();
}
function Update () {
if (Input.GetKeyUp(next)) {
ShowNext();
} else if (Input.GetKeyUp(previous)) {
ShowPrevious();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment