Unity–Timeline自定义PlayableTrack

  • Post author:
  • Post category:其他



今天尝试使用Unity timeline来做段动画, 发现BasicPlayableBehaviour已弃用, 而新的PlayableBehaviour不能直接往Playable Track拖。





翻了下文档使用PlayableAsset来新建PlayableBehaviour,然后使用ExposedReference,具体实现如下:

C# PlayableTest代码实现,功能就是当timeline开始播放时,设置Text为true,并把m_DialogStr字符串赋值给Text:

using UnityEngine.Playables;
using UnityEngine.UI;

// A behaviour that is attached to a playable
public class PlayableTest : PlayableBehaviour
{
    public Text m_DialogContainer;
    public string m_DialogStr;

    // Called when the state of the playable is set to Play
    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        if (m_DialogContainer != null)
        {
            m_DialogContainer.gameObject.SetActive(true);
            m_DialogContainer.text = m_DialogStr;
        }
    }

    // Called when the state of the playable is set to Paused
    public override void OnBehaviourPause(Playable playable, FrameData info)
    {
        if (m_DialogContainer != null)
        {
            m_DialogContainer.gameObject.SetActive(false);
        }
    }
}

C# PlayableAssetTest 代码实现,真正拖入Timeline的PlayableTrack中的脚本,在Scene中创建一个UI的Text组件,并把Text组件拖到

PlayableAssetTest 上的Text上,在m_DialogStr随便填写一段字符串,运行Unity即可看到效果

注:Timeline的PlayOnAwake,一定要勾选,否则不会自动执行。当然也可以在代码中调用Timeline的Play()方法。

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI;

[System.Serializable]
public class PlayableAssetTest : PlayableAsset
{
    public ExposedReference<Text> m_DialogContainer;
    public string m_DialogStr;

    // Factory method that generates a playable based on this asset
    public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    {
        var playable = ScriptPlayable<PlayableTest>.Create(graph);
        playable.GetBehaviour().m_DialogContainer = m_DialogContainer.Resolve(graph.GetResolver());
        playable.GetBehaviour().m_DialogStr = m_DialogStr;
        return playable;
    }
}

下面给出代码控制Timeline执行,把这个脚本挂在Scene中的随便一个GameObject上,不勾选PlayOnAwake,并把之前创建好的Text组件赋值给脚本的Text,把Timeline赋值给脚本的PlayableDirector,OK,现在运行Unity,只有当按下k键时,Timeline才会运行:

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using UnityEngine.UI;

public class DialogDirector : MonoBehaviour
{
    public PlayableDirector director;
    public Text text;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            foreach (PlayableBinding bind in director.playableAsset.outputs)
            {
                if (bind.sourceObject.GetType() == typeof(PlayableTrack))
                {
                    PlayableTrack playableTrack = (PlayableTrack)bind.sourceObject;
                    foreach (var clip in playableTrack.GetClips())
                    {
                        object clipAsset = clip.asset;
                        if (clipAsset is PlayableAssetTest)
                        {
                            PlayableAssetTest test = clipAsset as PlayableAssetTest;
                            director.SetReferenceValue(test.m_DialogContainer.exposedName, text);
                        }
                    }
                }
            }
            director.Play();
        }
    }
}



版权声明:本文为qq826364410原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。