unity Timeline的Playable脚本

Playabl脚本不使用MonoBehaviour而是BasicPlayableBehaviour,

名字域需要:using UnityEngine.Playables和using UnityEngine.Timeline

Playable中提供有8种方法:

1,OnGraphStart(Playable playable);

2,OnGraphStop(Playable playable);

3,PrepareFrame(Playable playable, FrameData info);

4,ProcessFrame(Playable playable, FrameData info, object playerData);

5,OnBehaviourPlay(Playable playable, FrameData info);

6,OnBehaviourPause(Playable playable, FrameData info);

7,OnPlayableCreate(Playable playable);

8,OnPlayableDestroy(Playable playable);
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
 
public class SimpleCustomPlayable : BasicPlayableBehaviour
{
    public override void OnGraphStart(Playable playable)
    {
        Debug.Log("Graph start");	
    }
 
    public override void OnGraphStop(Playable playable)
    {
        Debug.Log("Graph stop");
    }
 
    public override void PrepareFrame(Playable playable, FrameData info)
    {
        Debug.Log("1");
    }
 
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        Debug.Log("2");
    }
 
    public override void OnBehaviourPlay(Playable playable, FrameData info){
        Debug.Log("Play State Playing");
    }
 
    public override void OnBehaviourPause(Playable playable, FrameData info){
        Debug.Log("Play State Paused");
    }
}

OnBehaviourPlay和OnBehaviourPause分别为时间进度进入短条和离开短条所触发的方法。

OnGraphStart相当于一个初始化的方法,只有在Play之后才会调用

PrepareFrame和ProcessFrame调用与帧有关,两个方法调用的次数是一样的,只有时间进度进入短条后,这两个方法才会每帧调用。

要在Playable中控制外围对象不适用public这种,而是用public ExposedReference<GameObject>相当于public GameObject;

public ExposedReference<Transform>相当于public Transform;

还有一种切换继承对象的用法:public ExposedReference<MonoBehaviour> Component;相当于这个component是在MonoBehaviour下运行的;

public ExposedReference<EventTrigger>相当于EventTrigger;

所有这些方法都是先public ExposedReference然后在private一个<>里的结构,最后在OnGraphStart()中把private对象使用Resolve(playable.GetGraph().GetResolver());方法实例化使用。Resolve(playable.GetGraph().GetResolver())相当于把对象扔到Timeline里处理的意思。
 

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
 
public class ToggleComponentPlayable : BasicPlayableBehaviour
{
	public ExposedReference<MonoBehaviour> Component;
 
	private MonoBehaviour _component;
 
	public bool EnabledOnStart;
	public bool EnabledOnEnd;
 
	private GameObject m_GameObject;
 
	public override void OnGraphStart(Playable playable) 
	{
		_component = Component.Resolve(playable.GetGraph().GetResolver());
	}	
	
	public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        if (_component != null) 
		{
			_component.enabled = EnabledOnStart;
		}
    }
	
    public override void OnBehaviourPause(Playable playable, FrameData info)
	{
		if (_component != null) 
		{
			_component.enabled = EnabledOnEnd;
		}	
	}
}

转载自:https://blog.csdn.net/ILYPL/article/details/78062995

猜你喜欢

转载自blog.csdn.net/qq_38721111/article/details/84987344
今日推荐