0
\$\begingroup\$

I want like in games to pass over a cut scene or splash screen in this case. So the user can hit the escape key to pass it over.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Assertions.Must;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class Splashes : UnityEngine.MonoBehaviour
{
    [Header("Splash Screen")]
    public bool useSplashScreen = true;
    public GameObject splashesContent;
    private List<Graphic> splashes = new List<Graphic>();
    public float splashStayDiration = 3f;
    public float splashCrossFadeTime = 1f;



    void Start()
    {

        if (!useSplashScreen || splashesContent.GetComponentsInChildren<Graphic>(true).Length <= 0) return;

        //if we use splash screens and we have splash screens
#region Get All Splashes
        //if you build on PC Standalone - you can uncomment this
        //foreach (var splash in splashesContent.GetComponentsInChildren<Graphic>(true).Where(splash => splash != splashesContent.GetComponent<Graphic>()))
        //{
        //    splashes.Add(splash);
        //}

        for (var i = 0; i < splashesContent.GetComponentsInChildren<Graphic>(true).Length; i++)
        {
            var splash = splashesContent.GetComponentsInChildren<Graphic>(true)[i];
            if (splash != splashesContent.GetComponent<Graphic>())
            {
                splashes.Add(splash);
            }
        }

#endregion

        //And starting playing splashes
        StartCoroutine(PlayAllSplashes());
    }

    private IEnumerator PlayAllSplashes()
    {
        //Enabling Splashes root transform
        if (!splashesContent.activeSelf) splashesContent.SetActive(true);

        //main loop for playing
        foreach (var t in splashes)
        {
            t.gameObject.SetActive(true);
            t.canvasRenderer.SetAlpha(0.0f);
            t.CrossFadeAlpha(1, splashCrossFadeTime, false);
            yield return new WaitForSeconds(splashStayDiration + splashCrossFadeTime);
            t.CrossFadeAlpha(0, splashCrossFadeTime, false);
            yield return new WaitForSeconds(splashCrossFadeTime);
            t.gameObject.SetActive(false);
        }

        //Smooth main menu enabling
        splashesContent.GetComponent<Graphic>().CrossFadeAlpha(0, 0.5f, false);
        yield return new WaitForSeconds(0.5f);
        splashesContent.gameObject.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {

        }
    }

    public void ExitGame()
    {
        Application.Quit();
    }
}

This splash is showing up each time the game is running. So I want to give the player the chance to press escape key to pass over it.

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

StartCoroutine() returns a reference to the Coroutine it kicks off.

This reference can be passed to the StopCoroutine() method to stop running the coroutine prematurely.

You may need to do a bit more work in your case to handle any in-progress tweening from CrossFadeAlpha(), but that's a different question.

\$\endgroup\$
4
\$\begingroup\$

The answer by Chris Mills-Price is preferable in most cases: Keep the Coroutine object which gets returned by StartCoroutine and call StopCoroutine if you need to abort the running coroutine.

But sometimes it can be cumbersome to do this. For example if coroutines are started by objects which might no longer exist.

In that case you can also check for key inputs within your coroutine after each yield return <expression>;. If you detect the user-input which is supposed to abort the coroutine, you can do so with yield break;.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .