Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Play different timelines when loading a scene

Discussion in 'Timeline' started by samjm, Nov 14, 2019.

  1. samjm

    samjm

    Joined:
    Apr 20, 2018
    Posts:
    3
    Hi community!
    I'm relatively a beginner and I'm wondering if you could give me an advise on how to perform the following task. I haven't found a solution and now I'm not sure if it's possible to do.

    We have a main scene on which the user decides to go to different mini games (scenes). Every time the user finish a game, he goes back to the main scene and it plays a timeline showing different components (audio, animation, quiz,..). The problem we have is that, we want to play a timeline (always in the main scene) with different components (other audio, animation and quiz) depending on the scene from which the user went back.

    One way could be to have several timelines and play each depending on the scene, however I haven't found how to perform this action: load the scene and indicate which timeline to play in the main scene.

    I'm wondering what could be the best approach to change the timeline we want to use when loading the main scene or if is there a better way to accomplish this task. I'm using Unity 2018.3.14.

    Thanks in advance for your help and hope to hear from you!
     
    Last edited: Nov 14, 2019
    cristiamh likes this.
  2. cristiamh

    cristiamh

    Joined:
    Mar 31, 2019
    Posts:
    1
    Hello, I am interested in your question, because I have an entire scene duplicated many times and I'know it's not the right way to do it!
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,068
    Others might have better suggestions. Here is one way, or at least a starting point.

    Create all your playableDirectors/timelines in your main scene, but turn off PlayOnAwake.

    Have a script with a list of playable directors, and a static property (int or enum) that indicates which timeline to play.
    In Start, play the director according to the static property.

    In each scene that needs to change it, use a script that just changes the value of the static property, so when the main scene reloads it has a new value.

    If that adds too many resources to the main scene, then you will need to get into loading timelines dynamically.
     
  4. samjm

    samjm

    Joined:
    Apr 20, 2018
    Posts:
    3
    Thanks for your answer seant_unity! I'm trying to perform your suggested solution in a single scene and it seems to work, I just have a doubt on how to change the value of the static property, I mean, how can I change the value to the script in the main scene from the script on each scene, does the script in the main scene needs to be alive as with the DontDestroyOnLoad function? I guess it's a trivial question but how could the value of the static variable be changed from different scenes?

    Thanks again for your help!
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,068
    You will need a different script that runs in each scene that should change the timeline.

    If just loading that scene is enough, then a script on a gameObject that changes the static property in Awake or OnEnable() should be enough.

    Changing the static variable doesn't require the main scene to be loaded, it should retain it's value for the duration of the application. (In editor it can change when scripts are modified, or when going into playmode).
     
  6. samjm

    samjm

    Joined:
    Apr 20, 2018
    Posts:
    3
    Thanks for your help seant_unity , we managed to make it work with your suggestion, FYI, basically we created a gameobject in the main scene with the following script on which we have the list of PlayableDirector assigned through the inspector and the variable to select the current timeline to play:

    Code (CSharp):
    1. public class TimelinesManager : MonoBehaviour
    2. {
    3.     public List<PlayableDirector> timelines;
    4.  
    5.     public static int currentTimeline=0;
    6.  
    7.     void Start()
    8.     {
    9.         PlayableDirector current = timelines[currentTimeline];
    10.         current.Play();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.      
    16.     }
    17. }
    On every scene we have an event to load the main scene by setting the desired timeline to play beforehand:

    Code (CSharp):
    1. public void goHome(int timelineNum)
    2. {
    3.     TimelinesManager.currentTimeline = timelineNum;
    4.     SceneManager.LoadScene("main");
    5. }
     
    seant_unity likes this.
  7. pbritton_unity

    pbritton_unity

    Joined:
    Aug 3, 2018
    Posts:
    3
    I ran into a similar issue but my approach was different. We used one Timeline and swapped the Timeline Asset as needed.