白泽图

  • 文章
    • Unity渲染
    • Unity项目开发
    • 工具
    • 数学
    • 算法
    • 网站搭建
    • 网络&操作系统
蒋程个人博客
互联网技术经验总结&分享
  1. 首页
  2. Unity项目开发
  3. 正文

关于PlayableGrahp在编辑器下改变状态无法立即更新的问题

2022-10-28 1172点热度 0人点赞 0条评论

在编辑器下,做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();
        }
    }
}

标签: 暂无
最后更新:2022-10-28

蒋程

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

COPYRIGHT © 2023 白泽图. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

登录
注册|忘记密码?