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