-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatorPreviewSyncParticle.cs
124 lines (111 loc) · 3.95 KB
/
AnimatorPreviewSyncParticle.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
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEditor.Animations;
namespace Syy.Tools
{
public class AnimatorPreviewSyncParticle : EditorWindow
{
[MenuItem("Window/AnimatorPreviewSyncParticle")]
public static void Open()
{
GetWindow<AnimatorPreviewSyncParticle>("AnimatorPreview");
}
Animator _animTarget;
AnimationClip[] _clips = new AnimationClip[0];
int _clipIndex = -1;
bool _isPreviewPlaying;
void OnEnable()
{
EditorApplication.update += Update;
}
void OnDisable()
{
EditorApplication.update -= Update;
if (AnimationMode.InAnimationMode())
{
AnimationMode.StopAnimationMode();
_isPreviewPlaying = false;
}
}
void OnGUI()
{
using (var check = new EditorGUI.ChangeCheckScope())
{
_animTarget = (Animator)EditorGUILayout.ObjectField(_animTarget, typeof(Animator), true);
if (check.changed)
{
_clipIndex = 0;
if (_animTarget != null)
{
var controller = _animTarget.runtimeAnimatorController as AnimatorController;
_clips = controller.animationClips;
}
else
{
_clips = new AnimationClip[0];
}
}
}
if (_clips.Length != 0)
{
_clipIndex = EditorGUILayout.Popup(_clipIndex, _clips.Select(c => c.name).ToArray());
}
using (var check = new EditorGUI.ChangeCheckScope())
{
_isPreviewPlaying = EditorGUILayout.ToggleLeft("Preview Sync", _isPreviewPlaying);
if (check.changed)
{
if (_isPreviewPlaying)
{
AnimationMode.StartAnimationMode();
}
else
{
AnimationMode.StopAnimationMode();
}
}
}
}
void Update()
{
if (_isPreviewPlaying && AnimationMode.InAnimationMode())
{
if (_animTarget != null && _clips.Length != 0)
{
var clip = _clips[_clipIndex];
AnimationMode.SampleAnimationClip(_animTarget.gameObject, clip, ParticleSystemEditorHelper.I.PlaybackTime);
}
}
}
}
public class ParticleSystemEditorHelper
{
private static ParticleSystemEditorHelper _instance;
public static ParticleSystemEditorHelper I
{
get
{
if (_instance == null)
{
_instance = new ParticleSystemEditorHelper();
}
return _instance;
}
}
private static PropertyInfo _playbackTimePI;
private static Func<float> _playbackTimeGetFunc;
private ParticleSystemEditorHelper()
{
var assembly = Assembly.GetAssembly(typeof(Editor));
var type = assembly.GetType("UnityEditor.ParticleSystemEditorUtils");
_playbackTimePI = type.GetProperty("playbackTime", BindingFlags.Static | BindingFlags.NonPublic);
_playbackTimeGetFunc = (Func<float>)Delegate.CreateDelegate(typeof(Func<float>), _playbackTimePI.GetGetMethod(true));
}
public float PlaybackTime { get { return _playbackTimeGetFunc(); } set { _playbackTimePI.SetValue(null, value, null); } }
}
}