Search Unity

How to make a Clip that makes a game object disappear gradually?

Discussion in 'Timeline' started by landings, Nov 18, 2019.

  1. landings

    landings

    Joined:
    Jan 23, 2018
    Posts:
    5
    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:

    Code (CSharp):
    1.  
    2. public class ObjectExitAsset : UnityEngine.Playables.PlayableAsset {
    3.     public ExposedReference<GameObject> gameObjectRef;
    4.     public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) {
    5.         var playable = ScriptPlayable<ObjectExitBahaviour>.Create(graph);
    6.         playable.GetBehaviour().gameObject =
    7.             gameObjectRef.Resolve(graph.GetResolver());
    8.         return playable;
    9.     }
    10. }
    11.  
    Code (CSharp):
    1.  
    2. public class ObjectExitBahaviour : PlayableBehaviour
    3. {
    4.     public GameObject gameObject;
    5.     private Material _mat;
    6.     public override void ProcessFrame(Playable playable, FrameData info, object playerData) {
    7.         // How to calculate progress?
    8.         float start = //????;
    9.         float end = //????;
    10.         float current = //????;
    11.         float progress = current / (end - start);
    12.         _mat.SetFloat(Shader.PropertyToID("FadeProgress"), progress);
    13.     }
    14.     // When to set material?
    15.     private void SetMaterial() {
    16.         if (gameObject == null) return;
    17.         var mr = gameObject.GetComponent<MeshRenderer>();
    18.         if (mr == null) return;
    19.         _mat = mr.sharedMaterial;
    20.         // MaterialManager is custom.
    21.         if (_mat != MaterialManager.Fade) mr.sharedMaterial = MaterialManager.Fade;
    22.     }
    23.     // And When to Destroy?
    24.     private void DestroyIt() {
    25.         GameObject.Destroy(gameObject);
    26.     }
    27. }
    28.  
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    OnBehaviourPlay/OnBehaviourPause are roughly equivalent to OnClipStart, OnClipStop.

    progess = playable.GetTime() / playable.GetDuration() will give you the normalized progress through the clip, unless your clip supports ClipIn or Time Scaling (supplied to the editor using ITimelineClipAsset on the PlayableAsset).

    The Playable API isn't an exact 1-to-1 to timeline, Timeline is implement using Unity ScriptPlayables, so not all the information about the clip is available by default to the playable.
     
    landings likes this.