-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAnimPlayerGUI.cs
2757 lines (2404 loc) · 107 KB
/
AnimPlayerGUI.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2021 Victor Soupday
* This file is part of CC_Unity_Tools <https://github.com/soupday/CC_Unity_Tools>
*
* CC_Unity_Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CC_Unity_Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CC_Unity_Tools. If not, see <https://www.gnu.org/licenses/>.
*/
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using Object = UnityEngine.Object;
using UnityEditor.Animations;
using System;
namespace Reallusion.Import
{
public static class AnimPlayerGUI
{
#region AnimPlayer
//private static bool play = false;
//private static float time, prev, current = 0f;
public static bool AnimFoldOut { get; private set; } = true;
public static FacialProfile MeshFacialProfile { get; private set; }
public static FacialProfile ClipFacialProfile { get; private set; }
public static AnimationClip OriginalClip { get; set; }
public static AnimationClip WorkingClip { get ; set; }
public static Animator CharacterAnimator { get; set; }
//private static double updateTime = 0f;
//private static double deltaTime = 0f;
//private static double frameTime = 1f;
private static bool forceUpdate = false;
private static FacialProfile defaultProfile = new FacialProfile(ExpressionProfile.ExPlus, VisemeProfile.PairsCC3);
public static void OpenPlayer(GameObject scenePrefab)
{
if (scenePrefab)
{
//scenePrefab = Util.TryResetScenePrefab(scenePrefab);
SetCharacter(scenePrefab);
}
if (!IsPlayerShown())
{
#if SCENEVIEW_OVERLAY_COMPATIBLE
//2021.2.0a17+ When GUI.Window is called from a static SceneView delegate, it is broken in 2021.2.0f1 - 2021.2.1f1
//so we switch to overlays starting from an earlier version
AnimPlayerOverlay.ShowAll();
#else
//2020 LTS
AnimPlayerWindow.ShowPlayer();
#endif
//Common
SceneView.RepaintAll();
EditorApplication.update -= UpdateCallback;
EditorApplication.update += UpdateCallback;
EditorApplication.playModeStateChanged -= PlayStateChangeCallback;
EditorApplication.playModeStateChanged += PlayStateChangeCallback;
}
}
public static void ClosePlayer()
{
if (IsPlayerShown())
{
//clean up controller here
ResetToBaseAnimatorController();
EditorApplication.update -= UpdateCallback;
EditorApplication.playModeStateChanged -= PlayStateChangeCallback;
//if (CharacterAnimator)
///{
//GameObject scenePrefab = Util.GetScenePrefabInstanceRoot(CharacterAnimator.gameObject);
//Util.TryResetScenePrefab(scenePrefab);
//}
#if SCENEVIEW_OVERLAY_COMPATIBLE
//2021.2.0a17+
AnimPlayerOverlay.HideAll();
#else
//2020 LTS
AnimPlayerWindow.HidePlayer();
#endif
//Common
play = false;
time = 0f;
CharacterAnimator = null;
WorkingClip = null;
SceneView.RepaintAll();
}
}
public static bool IsPlayerShown()
{
#if SCENEVIEW_OVERLAY_COMPATIBLE
//2021.2.0a17+
return AnimPlayerOverlay.Visibility;
#else
//2020 LTS
return AnimPlayerWindow.isShown;
#endif
}
public static void SetCharacter(GameObject scenePrefab)
{
if (scenePrefab)
Util.LogDetail("scenePrefab.name: " + scenePrefab.name + " " + PrefabUtility.IsPartOfPrefabInstance(scenePrefab));
if (!scenePrefab && WindowManager.IsPreviewScene)
scenePrefab = WindowManager.GetPreviewScene().GetPreviewCharacter();
if (scenePrefab)
{
Animator animator = scenePrefab.GetComponent<Animator>();
if (!animator) animator = scenePrefab.GetComponentInChildren<Animator>();
if (animator != null)
{
if (PrefabUtility.IsPartOfPrefabInstance(scenePrefab))
{
GameObject sceneFbx = Util.FindRootPrefabAssetFromSceneObject(scenePrefab);
// in edit mode - find the first animation clip
AnimationClip clip = Util.GetFirstAnimationClipFromCharacter(sceneFbx);
if (sceneFbx && clip)
clip = AnimRetargetGUI.TryGetRetargetedAnimationClip(sceneFbx, clip);
UpdateAnimatorClip(animator, clip);
}
else
{
if (EditorApplication.isPlaying)
{
// in play mode - try to recover the stored last played animation
if (Util.TryDeSerializeAssetFromEditorPrefs<AnimationClip>(out Object obj, WindowManager.clipKey))
{
UpdateAnimatorClip(animator, obj as AnimationClip);
}
}
}
}
}
}
static public void UpdateAnimatorClip(Animator animator, AnimationClip clip)
{
if (doneInitFace) ResetFace(true, true);
if (!animator || CharacterAnimator != animator) doneInitFace = false;
CharacterAnimator = animator;
OriginalClip = clip;
SetupCharacterAndAnimation();
AnimRetargetGUI.RebuildClip();
MeshFacialProfile = FacialProfileMapper.GetMeshFacialProfile(animator ? animator.gameObject : null);
ClipFacialProfile = FacialProfileMapper.GetAnimationClipFacialProfile(clip);
time = 0f;
play = false;
// intitialise the face refs if needed
if (!doneInitFace) InitFace();
// finally, apply the face
ApplyFace();
}
#region Animator Setup
// ----------------------------------------------------------------------------
public static bool showMessages = false;
public static bool sceneFocus = false;
// initial selection
[SerializeField]
private static AnimatorController originalAnimatorController;
// Animaton Controller
[SerializeField]
private static AnimatorController playbackAnimatorController;
private static string controllerName = "--Temp-CCiC-Animator-Controller";
private static string overrideName = "--Temp-CCiC-Override-Controller";
private static string dirString = "Assets/";
private static string controllerPath;
private static string defaultState = "default_state";
[SerializeField]
private static int defaultStateNameHash;
private static string paramDirection = "param_direction";
[SerializeField]
private static AnimatorState playingState;
[SerializeField]
public static int controlStateHash { get; set; }
// animator/animation settings
public static bool FootIK = true;
[Flags]
enum AnimatorFlags
{
None = 0,
AnimateOnTheSpot = 1,
ShowMirrorImage = 2,
AutoLoopPlayback = 4,
Everything = ~0
}
static AnimatorFlags flagSettings;
// Animaton Override Controller
[SerializeField]
public static AnimatorOverrideController animatorOverrideController;
// Playback
private static bool play = false;
private static float playbackSpeed = 1f;
[SerializeField]
public static float time = 0f;
private static string realTime = "";
// Update
private static double updateTime = 0f;
private static double deltaTime = 0f;
private static double frameTime = 1f;
private static double current = 0f;
[SerializeField]
private static bool wasPlaying;
// GUIStyles
private static Styles guiStyles;
[SerializeField] private static List<BoneItem> boneItemList;
[SerializeField] public static bool isTracking = false;
[SerializeField] public static GameObject lastTracked;
[SerializeField] public static bool trackingPermitted = true;
private static string boneNotFound = "not found";
// ----------------------------------------------------------------------------
public static void SetupCharacterAndAnimation()
{
if (CharacterAnimator == null) return; // don't try and set up an override controller if the char has no animator
// retain the original AnimatorController from the scene model
//originalAnimatorController = GetControllerFromAnimator(CharacterAnimator);
// construct and use a new controller with specific parameters
playbackAnimatorController = CreateAnimatiorController();
CharacterAnimator.runtimeAnimatorController = playbackAnimatorController;
// create an animation override controller from the new controller
animatorOverrideController = CreateAnimatorOverrideController();
CharacterAnimator.runtimeAnimatorController = animatorOverrideController;
// defaults contain some flags that must be set in the selected animation
ApplyDefaultSettings();
// select the original clip using the override controller
// this method is normally used to switch animations during play mode
SelectOverrideAnimation(OriginalClip, animatorOverrideController);
// reset the animation player
ResetAnimationPlayer();
}
private static AnimatorController CreateAnimatiorController()
{
controllerPath = dirString + controllerName + ".controller";
Util.LogDetail("Creating Temporary file " + controllerPath);
AnimatorController a = AnimatorController.CreateAnimatorControllerAtPath(controllerPath);
a.name = controllerName;
// play mode parameters
a.AddParameter(paramDirection, AnimatorControllerParameterType.Float);
AnimatorStateMachine rootStateMachine = a.layers[0].stateMachine;
AnimatorState baseState = rootStateMachine.AddState(defaultState);
baseState.iKOnFeet = FootIK;
// play mode parameters
baseState.speedParameter = paramDirection;
baseState.speedParameterActive = true;
baseState.motion = OriginalClip;
playingState = baseState;
controlStateHash = baseState.nameHash;
return a;
}
private static AnimatorOverrideController CreateAnimatorOverrideController()
{
var aoc = new AnimatorOverrideController(CharacterAnimator.runtimeAnimatorController);
aoc.name = overrideName;
return aoc;
}
private static void ApplyDefaultSettings()
{
flagSettings = AnimatorFlags.AutoLoopPlayback;
SetFootIK(true);
CharacterAnimator.SetFloat(paramDirection, 0f);
if (CharacterAnimator != null)
{
CharacterAnimator.applyRootMotion = true;
}
}
// Important IK enable/disable function used by the retargeter
// (or you can't see the effects of changing the heel curves)
public static void SetFootIK(bool enable)
{
FootIK = enable;
if (playbackAnimatorController)
{
AnimatorControllerLayer[] allLayer = playbackAnimatorController.layers;
for (int i = 0; i < allLayer.Length; i++)
{
ChildAnimatorState[] states = allLayer[i].stateMachine.states;
for (int j = 0; j < states.Length; j++)
{
if (states[j].state.nameHash == controlStateHash)
{
states[j].state.iKOnFeet = FootIK;
allLayer[i].iKPass = FootIK;
}
}
}
if (EditorApplication.isPlaying) CharacterAnimator.gameObject.SetActive(false);
playbackAnimatorController.layers = allLayer;
if (EditorApplication.isPlaying) CharacterAnimator.gameObject.SetActive(true);
}
}
// called by the retargetter to revert all settings to default
public static void ForceSettingsReset()
{
ApplyDefaultSettings();
SetFootIK(false);
SetClipSettings(WorkingClip);
FirstFrameButton();
}
private static void SelectOverrideAnimation(AnimationClip clip, AnimatorOverrideController aoc)
{
ResetAnimationPlayer();
var clone = GameObject.Instantiate(clip);
clone.name = clip.name;
SetClipSettings(clone); // update the bake flags in AnimationClipSettings
// origingal clipsettings are untouched and should be copied
// directly into any saved animations from the retargeter
WorkingClip = clone;
List<KeyValuePair<AnimationClip, AnimationClip>> overrides = new List<KeyValuePair<AnimationClip, AnimationClip>>(aoc.overridesCount);
aoc.GetOverrides(overrides);
foreach (var v in overrides)
{
Util.LogDetail("Overrides: " + " Key: " + v.Key + " Value: " + v.Value);
}
overrides[0] = new KeyValuePair<AnimationClip, AnimationClip>(overrides[0].Key, WorkingClip);
aoc.ApplyOverrides(overrides);
FirstFrameButton();
}
public static void SelectOverrideAnimationWithoutReset(AnimationClip clip, AnimatorOverrideController aoc)
{
WorkingClip = clip;
List<KeyValuePair<AnimationClip, AnimationClip>> overrides = new List<KeyValuePair<AnimationClip, AnimationClip>>(aoc.overridesCount);
aoc.GetOverrides(overrides);
foreach (var v in overrides)
{
Util.LogDetail("Overrides: " + " Key: " + v.Key + " Value: " + v.Value);
}
overrides[0] = new KeyValuePair<AnimationClip, AnimationClip>(overrides[0].Key, WorkingClip);
aoc.ApplyOverrides(overrides);
}
private static void ResetAnimationPlayer()
{
play = false;
time = 0f;
playbackSpeed = 1f;
if (EditorApplication.isPlaying)
{
CharacterAnimator.SetFloat(paramDirection, 0f);
CharacterAnimator.Play(controlStateHash, 0, time);
}
else
{
CharacterAnimator.Update(time);
}
CharacterAnimator.gameObject.transform.localPosition = Vector3.zero;
CharacterAnimator.gameObject.transform.rotation = Quaternion.identity;
}
private static void SetClipSettings(AnimationClip clip)
{
AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(clip);
clipSettings.mirror = flagSettings.HasFlag(AnimatorFlags.ShowMirrorImage);
clipSettings.loopBlendPositionXZ = !flagSettings.HasFlag(AnimatorFlags.AnimateOnTheSpot);
clipSettings.loopBlendPositionY = !flagSettings.HasFlag(AnimatorFlags.AnimateOnTheSpot);
clipSettings.loopBlendOrientation = !flagSettings.HasFlag(AnimatorFlags.AnimateOnTheSpot);
AnimationUtility.SetAnimationClipSettings(clip, clipSettings);
CharacterAnimator.applyRootMotion = !flagSettings.HasFlag(AnimatorFlags.AnimateOnTheSpot);
}
public static void ResetToBaseAnimatorController()
{
// look up the original prefab corresponding to the model in the preview scene
// extract the path reference to the animator controller being used by the original prefab
// check the scene model is using the override controller created above
// replace the runtime animator controller of the scene model with the animatorcontroller asset at path
// destroy the disk asset temp override controller (that was created above)
GameObject characterPrefab = Util.GetScenePrefabInstanceRoot(CharacterAnimator);
if (!characterPrefab) return;
Util.LogDetail(("Attempting to reset: " + characterPrefab.name));
GameObject basePrefab = PrefabUtility.GetCorrespondingObjectFromSource(characterPrefab);
if (basePrefab != null)
{
if (true) //(PrefabUtility.IsAnyPrefabInstanceRoot(basePrefab))
{
string prefabPath = AssetDatabase.GetAssetPath(basePrefab);
Util.LogDetail((basePrefab.name + "Prefab instance root found: " + prefabPath));
Util.LogDetail("Loaded Prefab: " + basePrefab.name);
Animator baseAnimator = basePrefab.GetComponent<Animator>();
if (!baseAnimator) baseAnimator = basePrefab.GetComponentInChildren<Animator>();
if (baseAnimator != null)
{
Util.LogDetail("Prefab Animator: " + baseAnimator.name);
if (baseAnimator.runtimeAnimatorController)
{
Util.LogDetail("Prefab Animator Controller: " + baseAnimator.runtimeAnimatorController.name);
string controllerpath = AssetDatabase.GetAssetPath(baseAnimator.runtimeAnimatorController);
Util.LogDetail("Prefab Animator Controller Path: " + controllerpath);
AnimatorController baseController = AssetDatabase.LoadAssetAtPath<AnimatorController>(controllerpath);
if (CharacterAnimator.runtimeAnimatorController != null)
{
// ensure the created override controller is the one on the animator
// to avoid wiping user generated controller (it will have to be a disk asset - but nevertheless)
Util.LogDetail("Current controller on character: " + CharacterAnimator.runtimeAnimatorController.name);
if (CharacterAnimator.runtimeAnimatorController.GetType() == typeof(AnimatorOverrideController) && CharacterAnimator.runtimeAnimatorController.name == overrideName)
{
Util.LogDetail("Created override controller found: can reset");
CharacterAnimator.runtimeAnimatorController = baseController;
}
}
}
else
{
Util.LogDetail("NO Prefab Animator Controller");
CharacterAnimator.runtimeAnimatorController = null;
}
}
}
}
DestroyAnimationController();
}
private static void DestroyAnimationController()
{
Object tempControllerAsset = AssetDatabase.LoadAssetAtPath<AnimatorController>(controllerPath);
if (tempControllerAsset != null)
{
if (tempControllerAsset.GetType() == typeof(AnimatorController))
{
//if (showMessages)
Util.LogDetail("Override controller: " + controllerPath + " exists -- removing");
AssetDatabase.DeleteAsset(controllerPath);
}
}
}
#endregion Animator Setup
public static void ReCloneClip()
{
WorkingClip = CloneClip(OriginalClip);
time = 0f;
play = false;
}
public static AnimationClip CloneClip(AnimationClip clip)
{
if (clip)
{
var clone = Object.Instantiate(clip);
clone.name = clip.name;
AnimationClip clonedClip = clone as AnimationClip;
return clonedClip;
}
return null;
}
#region IMGUI
public class Styles
{
public GUIStyle settingsButton;
public GUIStyle playbackLabelStyle;
public GUIStyle playIconStyle;
public GUIStyle trackIconStyle;
public Styles()
{
settingsButton = new GUIStyle("toolbarbutton");
playbackLabelStyle = new GUIStyle("label");
playbackLabelStyle.alignment = TextAnchor.MiddleRight;
playIconStyle = new GUIStyle("label");
playIconStyle.contentOffset = new Vector2(5f, -4f);
trackIconStyle = new GUIStyle("label");
trackIconStyle.contentOffset = new Vector2(-6f, -4f);
}
}
public static void DrawPlayer()
{
if (guiStyles == null)
guiStyles = new Styles();
GUILayout.BeginVertical();
EditorGUI.BeginChangeCheck();
AnimFoldOut = EditorGUILayout.Foldout(AnimFoldOut, "Animation Playback", EditorStyles.foldout);
if (EditorGUI.EndChangeCheck())
{
//if (foldOut && FacialMorphIMGUI.FoldOut)
// FacialMorphIMGUI.FoldOut = false;
doOnceCatchMouse = true;
}
if (AnimFoldOut)
{
EditorGUI.BeginChangeCheck();
Animator selectedAnimator = (Animator)EditorGUILayout.ObjectField(new GUIContent("Scene Model", "Animated model in scene"), CharacterAnimator, typeof(Animator), true);
AnimationClip selectedClip = (AnimationClip)EditorGUILayout.ObjectField(new GUIContent("Animation", "Animation to play and manipulate"), OriginalClip, typeof(AnimationClip), false);
if (EditorGUI.EndChangeCheck())
{
UpdateAnimatorClip(selectedAnimator, selectedClip);
}
GUI.enabled = WorkingClip && CharacterAnimator;
// BEGIN PREFS AREA
EditorGUILayout.Space();
var rect = EditorGUILayout.BeginHorizontal();
Handles.color = Color.gray * 0.2f;
Handles.DrawLine(new Vector2(rect.x + 15, rect.y), new Vector2(rect.width - 15, rect.y));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
GUILayout.BeginHorizontal();
GUILayout.Space(4f);
EditorGUI.BeginDisabledGroup(AnimRetargetGUI.IsPlayerShown());
EditorGUI.BeginChangeCheck();
FootIK = GUILayout.Toggle(FootIK, new GUIContent("IK", FootIK ? "Toggle feet IK - Currently: ON" : "Toggle feet IK - Currently: OFF"), guiStyles.settingsButton, GUILayout.Width(24f), GUILayout.Height(24f));
if (EditorGUI.EndChangeCheck())
{
ToggleIKButton();
}
EditorGUI.EndDisabledGroup();
// Camera Bone Tracker
if (!CheckTackingStatus())
CancelBoneTracking(false); //object focus lost - arrange ui to reflect that, but dont fight with the scene camera
EditorGUI.BeginDisabledGroup(!trackingPermitted);
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("Camera Gizmo").image, "Select individual bone to track with the scene camera."), guiStyles.settingsButton, GUILayout.Width(24f), GUILayout.Height(24f)))
{
GenerateBoneMenu();
}
Texture cancelTrack = isTracking ? EditorGUIUtility.IconContent("toolbarsearchCancelButtonActive").image : EditorGUIUtility.IconContent("toolbarsearchCancelButtonOff").image;
string cancelTrackTxt = isTracking ? "Cancel camera tracking" : "Camera tracking controls.";
EditorGUI.BeginDisabledGroup(!isTracking);
if (GUILayout.Button(new GUIContent(cancelTrack, cancelTrackTxt), guiStyles.trackIconStyle, GUILayout.Width(24f), GUILayout.Height(24f)))
{
CancelBoneTracking(true); //tracking deliberately cancelled - leave scene camera in last position with last tracked object still selected
}
EditorGUI.EndDisabledGroup();
EditorGUI.EndDisabledGroup();
GUILayout.FlexibleSpace();
EditorGUI.BeginChangeCheck();
playbackSpeed = GUILayout.HorizontalSlider(playbackSpeed, -1f, 2f, GUILayout.Width(130f));
if (Math.Abs(1 - playbackSpeed) < 0.1f)
playbackSpeed = 1f;
if (EditorGUI.EndChangeCheck())
{
PlaybackSpeedSlider();
}
GUILayout.Label(new GUIContent(PlaybackText(), "Playback Speed"), guiStyles.playbackLabelStyle, GUILayout.MaxWidth(43f));
GUILayout.FlexibleSpace();
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("Refresh").image, "Reset model to T-Pose and player to defaults"), guiStyles.settingsButton, GUILayout.Width(24f), GUILayout.Height(24f)))
{
ResetCharacterAndPlayer();
}
EditorGUI.BeginDisabledGroup(AnimRetargetGUI.IsPlayerShown());
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("d__Menu").image, "Animation Clip Preferences"), guiStyles.settingsButton, GUILayout.Width(20f), GUILayout.Height(20f)))
{
ShowPrefsGenericMenu();
}
EditorGUI.EndDisabledGroup();
GUILayout.Space(4f);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.EndHorizontal();
EditorGUILayout.Space();
var rect2 = EditorGUILayout.BeginHorizontal();
Handles.color = Color.gray * 0.2f; //new Color(0.1372f, 0.1372f, 0.1372f, 1.0f);
Handles.DrawLine(new Vector2(rect2.x + 15f, rect2.y), new Vector2(rect2.width - 15f, rect2.y));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
// END PREFS AREA
GUILayout.BeginHorizontal();
if (CharacterAnimator && WorkingClip)
{
EditorGUI.BeginChangeCheck();
time = GUILayout.HorizontalSlider(time, 0f, 1f, GUILayout.Height(24f));
if (EditorGUI.EndChangeCheck())
{
ScrubTimeline();
}
realTime = TimeText();
EditorGUI.BeginChangeCheck();
realTime = EditorGUILayout.DelayedTextField(new GUIContent("", "Time index. Accepts numerical input."), realTime, GUILayout.MaxWidth(42f));
if (EditorGUI.EndChangeCheck())
{
ParseTimeInput();
}
}
else
{
EditorGUI.BeginDisabledGroup(true);
GUILayout.HorizontalSlider(0f, 0f, 1f, GUILayout.Height(24f));
EditorGUILayout.DelayedTextField(new GUIContent("", "Time index. Accepts numerical input."), "", GUILayout.MaxWidth(42f));
EditorGUI.EndDisabledGroup();
}
EditorGUILayout.EndHorizontal();
EditorGUI.BeginDisabledGroup(!(CharacterAnimator && WorkingClip));
GUILayout.BeginHorizontal(EditorStyles.toolbar);
// "Animation.FirstKey"
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("Animation.FirstKey").image, "First Frame"), EditorStyles.toolbarButton))
{
FirstFrameButton();
}
// "Animation.PrevKey"
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("Animation.PrevKey").image, "Previous Frame"), EditorStyles.toolbarButton))
{
PrevFrameButton();
}
// "Animation.Play"
Texture playButton = playbackSpeed > 0 ? EditorGUIUtility.IconContent("d_forward@2x").image : EditorGUIUtility.IconContent("d_back@2x").image;
Texture pauseButton = EditorGUIUtility.IconContent("PauseButton").image;
// play/pause: "Animation.Play" / "PauseButton"
if (GUILayout.Button(new GUIContent(play ? pauseButton : playButton, play ? "Pause" : "Play"), EditorStyles.toolbarButton, GUILayout.Height(30f)))
{
PlayPauseButton();
}
// "Animation.NextKey"
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("Animation.NextKey").image, "Next Frame"), EditorStyles.toolbarButton))
{
NextFrameButton();
}
// "Animation.LastKey"
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("Animation.LastKey").image, "Last Frame"), EditorStyles.toolbarButton))
{
LastFrameButton();
}
EditorGUI.EndDisabledGroup();
GUILayout.Space(10f);
//GUILayout.Label(new GUIContent(EditorGUIUtility.IconContent("d_UnityEditor.GameView").image, "Controls for 'Play Mode'"), guiStyles.playIconStyle, GUILayout.Width(24f), GUILayout.Height(24f));
if (GUILayout.Button(new GUIContent(EditorGUIUtility.IconContent("d_ViewToolOrbit On").image, "Select the character root."), EditorStyles.toolbarButton))
{
if (ColliderManagerEditor.EditMode)
{
Selection.activeObject = null;
}
else
{
Selection.activeObject = selectedAnimator.gameObject;
}
}
Texture bigPlayButton = EditorApplication.isPlaying ? EditorGUIUtility.IconContent("preAudioPlayOn").image : EditorGUIUtility.IconContent("preAudioPlayOff").image;
string playToggleTxt = EditorApplication.isPlaying ? "Exit 'Play Mode'." : "Enter 'Play Mode' and focus on the scene view window. This is to be used to evaluate play mode physics whilst allowing visualization of objects such as colliders.";
if (GUILayout.Button(new GUIContent(bigPlayButton, playToggleTxt), EditorStyles.toolbarButton, GUILayout.Height(24f), GUILayout.Width(60f)))
{
ApplicationPlayToggle();
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
#endregion IMGUI
#region Button Events/Functions
// Button functions
// "Toggle feet IK"
private static void ToggleIKButton()
{
// Alternative method - retrieve a copy of the layers - modify then reapply
// find the controlstate by nameHash
SetFootIK(FootIK);
// originally using the cached default state directly ...
// both methods encounter errors when changing foot ik during runtime
// unless the gameObject is disabled/re-enabled
/*
if (EditorApplication.isPlaying) sceneAnimator.gameObject.SetActive(false);
playingState.iKOnFeet = FootIK;
if (EditorApplication.isPlaying) sceneAnimator.gameObject.SetActive(true);
*/
if (EditorApplication.isPlaying)
{
//ResetAnimationPlayer();
CharacterAnimator.Play(controlStateHash, 0, time);
CharacterAnimator.SetFloat(paramDirection, play ? playbackSpeed : 0f);
}
else
{
CharacterAnimator.Update(time);
}
}
// playback speed slider sets speed multiplier directly in edit mode but requires an update in play mode
private static void PlaybackSpeedSlider()
{
if (EditorApplication.isPlaying)
{
if (play)
CharacterAnimator.SetFloat(paramDirection, playbackSpeed);
else
CharacterAnimator.SetFloat(paramDirection, 0f);
CharacterAnimator.Play(controlStateHash, 0, time);
}
}
// "Reset Model to T-Pose and player to defaults"
public static void ResetCharacterAndPlayer()
{
// re-apply all defaults
ApplyDefaultSettings();
// reset the player
ResetAnimationPlayer();
// clear all the animation data
WorkingClip = null;
OriginalClip = null;
// clear the animation controller + override controller
// remove the on-disk temporary controller
ResetToBaseAnimatorController();
//DestroyAnimationController();
// revert character pose to original T-Pose
ResetCharacterPose();
// user can now select a new animation for playing
}
// "Animation Clip Preferences"
private static void ShowPrefsGenericMenu()
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Animate On The Spot"), flagSettings.HasFlag(AnimatorFlags.AnimateOnTheSpot), OnPrefSelected, AnimatorFlags.AnimateOnTheSpot);
menu.AddItem(new GUIContent("Show Mirror Image"), flagSettings.HasFlag(AnimatorFlags.ShowMirrorImage), OnPrefSelected, AnimatorFlags.ShowMirrorImage);
menu.AddItem(new GUIContent("Auto Loop Playback"), flagSettings.HasFlag(AnimatorFlags.AutoLoopPlayback), OnPrefSelected, AnimatorFlags.AutoLoopPlayback);
menu.ShowAsContext();
}
private static void OnPrefSelected(object obj)
{
AnimatorFlags f = (AnimatorFlags)obj;
if (flagSettings.HasFlag(f))
flagSettings ^= f;
else
flagSettings |= f;
if (WorkingClip)
{
if (f == AnimatorFlags.AnimateOnTheSpot || f == AnimatorFlags.ShowMirrorImage)
{
SetClipSettings(WorkingClip);
ResetAnimationPlayer();
}
}
}
// Time Slider
public static void ScrubTimeline()
{
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
}
else
{
UpdateAnimator();
}
}
// "Time index. Accepts numerical input."
private static void ParseTimeInput()
{
float parsedTime;
if (float.TryParse(realTime, out parsedTime))
{
if (parsedTime > WorkingClip.length)
parsedTime = WorkingClip.length;
if (parsedTime < 0f)
parsedTime = 0f;
time = parsedTime / WorkingClip.length;
}
else
{
realTime = TimeText();
}
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
}
}
// "Animation.FirstKey"
private static void FirstFrameButton()
{
play = false;
time = 0f;
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
CharacterAnimator.SetFloat(paramDirection, 0f);
}
else
{
UpdateAnimator();
}
}
// "Animation.PrevKey"
private static void PrevFrameButton()
{
play = false;
time -= 0.0166f / WorkingClip.length;
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
CharacterAnimator.SetFloat(paramDirection, 0f);
}
else
{
UpdateAnimator();
}
}
// "Animation.Play"
private static void PlayButton()
{
play = true;
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
CharacterAnimator.SetFloat(paramDirection, playbackSpeed);
}
else
{
CharacterAnimator.Update(time);
CharacterAnimator.Play(controlStateHash, 0, time);
}
}
// "PauseButton"
private static void PauseButton()
{
play = false;
if (EditorApplication.isPlaying)
{
CharacterAnimator.SetFloat(paramDirection, 0f);
}
}
// "Animation.Play" / "PauseButton"
private static void PlayPauseButton()
{
if (play)
PauseButton();
else
PlayButton();
}
// "Animation.NextKey"
private static void NextFrameButton()
{
play = false;
time += 0.0166f / WorkingClip.length;
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
CharacterAnimator.SetFloat(paramDirection, 0f);
}
else
{
UpdateAnimator();
}
}
// "Animation.LastKey"
private static void LastFrameButton()
{
play = false;
time = 1f;
if (EditorApplication.isPlaying)
{
CharacterAnimator.Play(controlStateHash, 0, time);
CharacterAnimator.SetFloat(paramDirection, 0f);
}
else
{
UpdateAnimator();
}
}
private static void ApplicationPlayToggle()
{
// button to enter play mode and retain scene view
//
// if the application is not playing and will enter play mode:
// set the flag to true
// callback will focus the view back to the scene window
// if the application is playing:
// set the flag to false
// entering play mode maually wont cause callback to refocus on the scene
if (!EditorApplication.isPlaying)