在编辑器下,做playable相关的操作,比如调用PlayableGrahp.Stop()后再执行PlayableGrahp.Play(),但是更新函数没有被调用
解决方法:
调用UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
Normally, a player loop update will occur in the editor when the Scene has been modified. This method allows you to queue a player loop update regardless of whether the Scene has been modified.
通常情况下,当场景已经完成时,玩家循环更新将在编辑器中发生修改。这个方法允许你排队播放循环更新不管场景是否被修改过。
测试代码如下
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
[ExecuteAlways]
public class PlayableTest : MonoBehaviour
{
private PlayableGraph m_Graph;
private Animator m_Animator;
private Animator animator
{
get
{
if (m_Animator == null)
{
m_Animator = GetComponent<Animator>();
if (m_Animator == null)
m_Animator = gameObject.AddComponent<Animator>();
}
return m_Animator;
}
}
public void Init()
{
m_Graph = PlayableGraph.Create(gameObject.name + gameObject.GetHashCode());
PlayableTestBehaviour template = new PlayableTestBehaviour();
var playable = ScriptPlayable<PlayableTestBehaviour>.Create(m_Graph, template, 1);
AnimationPlayableOutput output = AnimationPlayableOutput.Create(m_Graph, "TBCAnimatorOutput", animator);
output.SetSourcePlayable(playable, 0);
}
public void Stop()
{
if (m_Graph.IsValid())
{
Debug.Log("*** m_Graph.Stop **");
m_Graph.Stop();
}
}
public void Play()
{
if (m_Graph.IsValid())
{
Debug.Log("*** m_Graph.Play **");
m_Graph.Play();
}
}
void Destroy()
{
if (m_Graph.IsValid())
{
Debug.Log("*** m_Graph.Destroy **");
m_Graph.Destroy();
}
}
}
using UnityEngine;
using UnityEngine.Playables;
public class PlayableTestBehaviour : PlayableBehaviour
{
public override void PrepareFrame(Playable playable, FrameData info)
{
Debug.LogFormat("**PrepareFrame**{0}",playable.GetTime());
}
}
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(PlayableTest))]
public class PlayableTestInspector : Editor
{
public override void OnInspectorGUI()
{
var test = target as PlayableTest;
if (GUILayout.Button("Init"))
{
test.Init();
}
if (GUILayout.Button("Play"))
{
test.Play();
EditorApplication.QueuePlayerLoopUpdate();
}
if (GUILayout.Button("Stop"))
{
test.Stop();
}
}
}
文章评论