unity error
This commit is contained in:
@@ -19,6 +19,7 @@ public class FrameAnimator : MonoBehaviour
|
||||
bool m_previewPlaying = false;
|
||||
#if UNITY_EDITOR
|
||||
double m_lastEditorTime = 0d;
|
||||
bool m_editorHooked;
|
||||
#endif
|
||||
Image m_img;
|
||||
Image img {
|
||||
@@ -42,19 +43,19 @@ public class FrameAnimator : MonoBehaviour
|
||||
void OnEnable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
m_lastEditorTime = EditorApplication.timeSinceStartup;
|
||||
EditorApplication.update -= OnEditorUpdate;
|
||||
EditorApplication.update += OnEditorUpdate;
|
||||
}
|
||||
// Never hook editor updates during play-mode enter/exit — QueuePlayerLoopUpdate
|
||||
// + isPlaying serialized true can infinite-reload Temp/__Backupscenes.
|
||||
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
TryHookEditorUpdate();
|
||||
#endif
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.update -= OnEditorUpdate;
|
||||
UnhookEditorUpdate();
|
||||
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
|
||||
#endif
|
||||
}
|
||||
void Start()
|
||||
@@ -80,24 +81,72 @@ public class FrameAnimator : MonoBehaviour
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnEditorUpdate()
|
||||
void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
{
|
||||
if (Application.isPlaying || this == null || !isActiveAndEnabled)
|
||||
if (state == PlayModeStateChange.ExitingEditMode ||
|
||||
state == PlayModeStateChange.EnteredPlayMode ||
|
||||
state == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
m_previewPlaying = false;
|
||||
UnhookEditorUpdate();
|
||||
}
|
||||
else if (state == PlayModeStateChange.EnteredEditMode)
|
||||
{
|
||||
TryHookEditorUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
void TryHookEditorUpdate()
|
||||
{
|
||||
if (Application.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
|
||||
{
|
||||
UnhookEditorUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_editorHooked)
|
||||
return;
|
||||
|
||||
m_lastEditorTime = EditorApplication.timeSinceStartup;
|
||||
EditorApplication.update += OnEditorUpdate;
|
||||
m_editorHooked = true;
|
||||
}
|
||||
|
||||
void UnhookEditorUpdate()
|
||||
{
|
||||
if (!m_editorHooked)
|
||||
return;
|
||||
EditorApplication.update -= OnEditorUpdate;
|
||||
m_editorHooked = false;
|
||||
}
|
||||
|
||||
void OnEditorUpdate()
|
||||
{
|
||||
if (Application.isPlaying ||
|
||||
EditorApplication.isPlayingOrWillChangePlaymode ||
|
||||
this == null ||
|
||||
!isActiveAndEnabled)
|
||||
{
|
||||
UnhookEditorUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
// Edit-mode animation is preview-only. Ignoring serialized isPlaying prevents
|
||||
// permanent editor loops when isPlaying was left true in the scene.
|
||||
if (!m_previewPlaying)
|
||||
return;
|
||||
|
||||
double now = EditorApplication.timeSinceStartup;
|
||||
float dt = (float)(now - m_lastEditorTime);
|
||||
m_lastEditorTime = now;
|
||||
|
||||
if (dt <= 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int frameBefore = currentFrame;
|
||||
TickAnimation(dt);
|
||||
EditorApplication.QueuePlayerLoopUpdate();
|
||||
if (currentFrame != frameBefore || m_previewPlaying)
|
||||
EditorApplication.QueuePlayerLoopUpdate();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -108,7 +157,9 @@ public class FrameAnimator : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
|
||||
bool shouldAnimate = m_previewPlaying || isPlaying;
|
||||
bool shouldAnimate = Application.isPlaying
|
||||
? (m_previewPlaying || isPlaying)
|
||||
: m_previewPlaying;
|
||||
if (!shouldAnimate)
|
||||
{
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user