Hello, I’m new to Timeline so this post may be naive. I want to have a clip that makes a game object disappear gradually in scene. As simple as possible.
I already have a material named Fade, which has a float value named FadeProgress ranging from 0.0 to 1.0.
So I plan to set this material to my target game object at the beginning of the clip, set FadeProgress on every frame, and finally destroy this game object when the clip is over.
But I can’t find start time and end time inside ProcessFrame. Also, I can’t find any overrides like OnClipStart. I can only find OnGraphStart. This seems not equivalent to OnClipStart, does it?
This is what I have done:
public class ObjectExitAsset : UnityEngine.Playables.PlayableAsset {
public ExposedReference<GameObject> gameObjectRef;
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) {
var playable = ScriptPlayable<ObjectExitBahaviour>.Create(graph);
playable.GetBehaviour().gameObject =
gameObjectRef.Resolve(graph.GetResolver());
return playable;
}
}
public class ObjectExitBahaviour : PlayableBehaviour
{
public GameObject gameObject;
private Material _mat;
public override void ProcessFrame(Playable playable, FrameData info, object playerData) {
// How to calculate progress?
float start = //????;
float end = //????;
float current = //????;
float progress = current / (end - start);
_mat.SetFloat(Shader.PropertyToID("FadeProgress"), progress);
}
// When to set material?
private void SetMaterial() {
if (gameObject == null) return;
var mr = gameObject.GetComponent<MeshRenderer>();
if (mr == null) return;
_mat = mr.sharedMaterial;
// MaterialManager is custom.
if (_mat != MaterialManager.Fade) mr.sharedMaterial = MaterialManager.Fade;
}
// And When to Destroy?
private void DestroyIt() {
GameObject.Destroy(gameObject);
}
}