Tanoda
StateMachine.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using UnityEngine;
11
12namespace Leap.Unity.Recording {
13 using Attributes;
14
15 public class StateMachine : MonoBehaviour {
16
17 public bool enableManualTransition = false;
18
19 [DisableIf("enableManualTransition", isEqualTo: false)]
20 public KeyCode manualTransitionKey = KeyCode.F5;
21
22 public GameObject activeState {
23 get {
24 for (int i = 0; i < transform.childCount; i++) {
25 var child = transform.GetChild(i).gameObject;
26 if (child.activeInHierarchy) {
27 return child;
28 }
29 }
30 return null;
31 }
32 }
33
34 private void Awake() {
35 int enabledCount = 0;
36 for (int i = 0; i < transform.childCount; i++) {
37 if (transform.GetChild(i).gameObject.activeSelf) {
38 enabledCount++;
39 }
40 }
41
42 //If there is not one state currently enabled, disable all but the first
43 if (enabledCount != 1) {
44 for (int i = 0; i < transform.childCount; i++) {
45 transform.GetChild(i).gameObject.SetActive(i == 0);
46 }
47 }
48 }
49
50 private void Update() {
51 if (enableManualTransition && Input.GetKeyDown(manualTransitionKey)) {
52 for (int i = 0; i < transform.childCount; i++) {
53 var child = transform.GetChild(i).gameObject;
54 if (child.activeSelf) {
55 var t = child.GetComponent<TransitionBehaviour>();
56 if (t != null) {
57 t.Transition();
58 break;
59 }
60 }
61 }
62 }
63 }
64 }
65}