-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathPreviewWindow.cs
More file actions
139 lines (120 loc) · 5.04 KB
/
PreviewWindow.cs
File metadata and controls
139 lines (120 loc) · 5.04 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
namespace ToolKits
{
public class PreviewWindow : EditorWindow
{
private static PreviewWindow _window;
private static readonly Vector2 MIN_SIZE = new Vector2(600, 400);
private static readonly Rect PREVIEW_RECT = new Rect(0, 50, 500, 300);
private const string PREVIEW_ANIMCONTROLLER_PATH =
"Assets/GameAssets/Arts/AnimatorControllers/PreviewController.controller";
private const string AVATAR_PATH = "Assets/GameAssets/Arts/Prefabs/Warrior.prefab";
private const string CLIP_PATH = "Assets/GameAssets/Arts/Models/AnimationClips/Female/1HIdle.anim";
private AvatarPreview _avatarPreview;
private AnimationClip _animationClip;
private Animator _animator;
private AnimatorController _previewAnimator;
private GameObject PreviewInstance;
private AnimatorState _animatorState;
[MenuItem("Tools/PreviewWindow", priority = 9)]
private static void PopUp()
{
_window = GetWindow<PreviewWindow>("预览窗口");
_window.minSize = MIN_SIZE;
_window.Init();
_window.Show();
}
private void Init()
{
}
private void OnGUI()
{
if (GUILayout.Button("加载预览"))
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(AVATAR_PATH);
PreviewInstance = EditorHelper.InstantiateGoByPrefab(prefab, null);
PreviewInstance.hideFlags = HideFlags.HideAndDontSave;
PreviewInstance.tag = Constants.PREVIRE_TAG;
_previewAnimator = AssetDatabase.LoadAssetAtPath<AnimatorController>(PREVIEW_ANIMCONTROLLER_PATH);
_animationClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(CLIP_PATH);
var states = _previewAnimator.layers[0].stateMachine.states;
foreach (var item in states)
{
if (item.state.name == "Preview")
{
_animatorState = item.state;
break;
}
}
InitController();
_avatarPreview = new AvatarPreview(_animator, _animationClip);
_avatarPreview.OnAvatarChangeFunc = SetPreviewAvatar;
_avatarPreview.fps = Mathf.RoundToInt(_animationClip.frameRate);
_avatarPreview.ShowIKOnFeetButton = (_animationClip as Motion).isHumanMotion;
_avatarPreview.ResetPreviewFocus();
_avatarPreview.timeControl.stopTime = _animationClip.length;
// force an update on timeControl if AvatarPreviewer is closed when creating/editing animation curves
// prevent from having a nomralizedTime == -inf
if (_avatarPreview.timeControl.currentTime == Mathf.NegativeInfinity)
_avatarPreview.timeControl.Update();
}
if (null != _avatarPreview)
{
if (Event.current.type == EventType.Repaint)
{
_avatarPreview.timeControl.loop = true;
_avatarPreview.timeControl.Update();
AnimationClipSettings previewInfo = AnimationUtility.GetAnimationClipSettings(_animationClip);
float normalizedTime = previewInfo.stopTime - previewInfo.startTime != 0
? (_avatarPreview.timeControl.currentTime - previewInfo.startTime) /
(previewInfo.stopTime - previewInfo.startTime)
: 0.0f;
_avatarPreview.Animator.Play(0, 0, normalizedTime);
_avatarPreview.Animator.Update(_avatarPreview.timeControl.deltaTime);
}
_avatarPreview.DoAvatarPreview(PREVIEW_RECT, Constants.preBackgroundSolid);
}
Repaint();
}
private void SetPreviewAvatar()
{
DestroyController();
InitController();
}
private void InitController()
{
_animator = PreviewInstance.GetComponent<Animator>();
_animatorState.motion = _animationClip;
_animator.runtimeAnimatorController = _previewAnimator;
}
private void DestroyController()
{
}
private void OnDestroy()
{
if (null != _avatarPreview)
{
_avatarPreview.OnDestroy();
_avatarPreview = null;
}
GameObject.DestroyImmediate(PreviewInstance);
PreviewInstance = null;
_animationClip = null;
_animator = null;
_previewAnimator = null;
_animatorState = null;
}
private void OnDisable()
{
if (null != _avatarPreview)
{
_avatarPreview.OnDisable();
}
}
}
}