AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CompoundButtonAnim.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using System;
6 using HoloToolkit.Unity;
7 
8 namespace HoloToolkit.Unity.Buttons
9 {
13  [RequireComponent(typeof(CompoundButton))]
14  public class CompoundButtonAnim : MonoBehaviour
15  {
16  [DropDownComponent]
17  public Animator TargetAnimator;
18 
24 
25  private void Awake() {
26  GetComponent<Button>().StateChange += StateChange;
27  if (TargetAnimator == null) {
28  TargetAnimator = GetComponent<Animator>();
29  }
30  }
31 
35  void StateChange(ButtonStateEnum newState) {
36  if (TargetAnimator == null) {
37  return;
38  }
39 
40  if (AnimActions == null) {
41  return;
42  }
43 
44  if (!gameObject.activeSelf)
45  return;
46 
47  for (int i = 0; i < AnimActions.Length; i++) {
48  if (AnimActions[i].ButtonState == newState) {
49  if (!string.IsNullOrEmpty(AnimActions[i].ParamName)) {
50  switch (AnimActions[i].ParamType) {
51  case AnimatorControllerParameterType.Bool:
52  TargetAnimator.SetBool(AnimActions[i].ParamName, AnimActions[i].BoolValue);
53  break;
54 
55  case AnimatorControllerParameterType.Float:
56  TargetAnimator.SetFloat(AnimActions[i].ParamName, AnimActions[i].FloatValue);
57  break;
58 
59  case AnimatorControllerParameterType.Int:
60  TargetAnimator.SetInteger(AnimActions[i].ParamName, AnimActions[i].IntValue);
61  break;
62 
63  case AnimatorControllerParameterType.Trigger:
64  TargetAnimator.SetTrigger(AnimActions[i].ParamName);
65  break;
66 
67  default:
68  throw new ArgumentOutOfRangeException();
69  }
70  }
71  break;
72  }
73  }
74  }
75 
76 #if UNITY_EDITOR
77  [UnityEditor.CustomEditor(typeof(CompoundButtonAnim))]
78  public class CustomEditor : MRTKEditor
79  {
83  protected override void DrawCustomFooter() {
84 
86  Animator animator = acb.TargetAnimator;
87  AnimatorControllerParameter[] animParams = null;
88 
89  // Validate the AnimButton controls - make sure there's one control for each button state
90  ButtonStateEnum[] buttonStates = (ButtonStateEnum[])System.Enum.GetValues(typeof(ButtonStateEnum));
91  if (acb.AnimActions == null || acb.AnimActions.Length != buttonStates.Length) {
92  acb.AnimActions = new AnimatorControllerAction[buttonStates.Length];
93  }
94 
95  // Don't allow user to change setup during play mode
96  if (!Application.isPlaying && !string.IsNullOrEmpty (acb.gameObject.scene.name)) {
97 
98  // Get the available animation parameters
99  animParams = animator.parameters;
100 
101  for (int i = 0; i < buttonStates.Length; i++) {
102  acb.AnimActions[i].ButtonState = buttonStates[i];
103  }
104 
105  // Now make sure all animation parameters are found
106  for (int i = 0; i < acb.AnimActions.Length; i++) {
107  if (!string.IsNullOrEmpty(acb.AnimActions[i].ParamName)) {
108  bool invalidParam = true;
109  foreach (AnimatorControllerParameter animParam in animParams) {
110  if (acb.AnimActions[i].ParamName == animParam.name) {
111  // Update the type while we're here
112  invalidParam = false;
113  acb.AnimActions[i].ParamType = animParam.type;
114  break;
115  }
116  }
117 
118  // If we didn't find it, mark it as invalid
119  acb.AnimActions[i].InvalidParam = invalidParam;
120  }
121  }
122  }
123 
124  UnityEditor.EditorGUILayout.BeginVertical(UnityEditor.EditorStyles.helpBox);
125  UnityEditor.EditorGUILayout.LabelField("Animation states:", UnityEditor.EditorStyles.miniBoldLabel);
126 
127  // Draw the editor for all the animation actions
128  for (int i = 0; i < acb.AnimActions.Length; i++) {
129  acb.AnimActions[i] = DrawAnimActionEditor(acb.AnimActions[i], animParams);
130  }
131 
132  UnityEditor.EditorGUILayout.EndVertical();
133  }
134 
135  AnimatorControllerAction DrawAnimActionEditor(AnimatorControllerAction action, AnimatorControllerParameter[] animParams) {
136  bool actionIsEmpty = string.IsNullOrEmpty(action.ParamName);
137  UnityEditor.EditorGUILayout.BeginHorizontal();
138  UnityEditor.EditorGUILayout.LabelField(action.ButtonState.ToString(), GUILayout.MaxWidth(150f), GUILayout.MinWidth(150f));
139 
140  if (animParams != null && animParams.Length > 0) {
141  // Show a dropdown
142  string[] options = new string[animParams.Length + 1];
143  options[0] = "(None)";
144  int currentIndex = 0;
145  for (int i = 0; i < animParams.Length; i++) {
146  options[i + 1] = animParams[i].name;
147  if (animParams[i].name == action.ParamName) {
148  currentIndex = i + 1;
149  }
150  }
151  GUI.color = actionIsEmpty ? Color.gray : Color.white;
152  int newIndex = UnityEditor.EditorGUILayout.Popup(currentIndex, options, GUILayout.MinWidth(150f), GUILayout.MaxWidth(150f));
153  if (newIndex == 0) {
154  action.ParamName = string.Empty;
155  } else {
156  action.ParamName = animParams[newIndex - 1].name;
157  action.ParamType = animParams[newIndex - 1].type;
158  }
159  } else {
160  // Just show a label
161  GUI.color = action.InvalidParam ? Color.yellow : Color.white;
162  UnityEditor.EditorGUILayout.LabelField(actionIsEmpty ? "(None)" : action.ParamName, GUILayout.MinWidth(75f), GUILayout.MaxWidth(75f));
163  }
164 
165  GUI.color = Color.white;
166 
167  if (!actionIsEmpty) {
168  UnityEditor.EditorGUILayout.LabelField(action.ParamType.ToString(), UnityEditor.EditorStyles.miniLabel, GUILayout.MinWidth(75f), GUILayout.MaxWidth(75f));
169  switch (action.ParamType) {
170  case AnimatorControllerParameterType.Bool:
171  action.BoolValue = UnityEditor.EditorGUILayout.Toggle(action.BoolValue);
172  break;
173 
174  case AnimatorControllerParameterType.Float:
175  action.FloatValue = UnityEditor.EditorGUILayout.FloatField(action.FloatValue);
176  break;
177 
178  case AnimatorControllerParameterType.Int:
179  action.IntValue = UnityEditor.EditorGUILayout.IntField(action.IntValue);
180  break;
181 
182  case AnimatorControllerParameterType.Trigger:
183  break;
184 
185  default:
186  break;
187 
188  }
189  }
190 
191  UnityEditor.EditorGUILayout.EndHorizontal();
192 
193  return action;
194  }
195  }
196 #endif
197  }
198 }
ButtonStateEnum
State enum for buttons.
AnimatorControllerAction [] AnimActions
List of animation actions
Anim controller button offers as simple way to link button states to animation controller parameters ...