/// <summary> /// Switches to a new acitivity, initialization argument and optional transition. /// The initialization argument is strongly typed. /// If no transition is specified then InstantActivityTransition will be used. /// This function will result in the target activity's Init(TArgs initArgs) function being called with the args passed in here /// </summary> /// <param name="initArgs">The initialization argument</param> /// <param name="transition">The transition to use</param> /// <typeparam name="TActivity">The target activity</typeparam> /// <typeparam name="TInitArgs">The type of the initialization argument</typeparam> public void SwitchActivity <TActivity, TInitArgs>(TInitArgs initArgs, IActivityTransition transition = null) where TActivity : Component, IActivity <TInitArgs> { SwitchActivityInternal <TActivity>(a => a.Init(initArgs), transition); }
private void SwitchActivityInternal <TActivity>(Action <TActivity> initActivity, IActivityTransition transition) where TActivity : Component { if (transitionInProgress) { throw new Exception("Cannot SwitchActivity, there is an activity transition already in progress."); } Action startActivity = () => { var newActivity = new GameObject(typeof(TActivity).Name).AddComponent <TActivity>(); newActivity.transform.SetParent(transform); currentActivity = newActivity.gameObject; initActivity(newActivity); }; if (transition == null) { transition = new InstantActivityTransition(); } transitionInProgress = true; transitionController.StartActivityFunc = startActivity; transitionController.OnComplete = () => transitionInProgress = false; transition.Start(transitionController); }
/// <summary> /// Switches to a new acitivity, with optional transition. /// If no transition is specified then InstantActivityTransition will be used. /// This function will result in the target activity's Init() function being called /// </summary> /// <param name="transition">The transition to use</param> /// <typeparam name="TActivity">The target activity</typeparam> public void SwitchActivity <TActivity>(IActivityTransition transition = null) where TActivity : Component, IActivity { SwitchActivityInternal <TActivity>(a => a.Init(), transition); }