Posted on

Creating a simple, effective intro video for Unity projects

As anyone who has used the free version of Unity will tell you, native support for adding your own introduction video / logo to your project isn’t available in the free version of Unity, however you can, quite easily add one without falling foul of any rules.

First you need to make a blank scene, for this example we will called it “Intro”, and then a C# script, called IntroLogo.
In the script, paste the following:

using UnityEngine;
using UnityEngine.SceneManagement;

public class IntroLogo : MonoBehaviour
{
    public MovieTexture movieToUse;

    // Use this for initialization
    private void Start()
    {
        movieToUse.Play();
    }
    void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), movieToUse, ScaleMode.StretchToFill);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape) || Input.GetButtonDown("XB1_A"))
        {
            SceneManager.LoadScene("Title");
        }
        if (movieToUse.isPlaying == false)
        {
            SceneManager.LoadScene("Title");
        }
    }
}

From there attach the script to your camera in the scene, and you’ll see a “Movie To Use” box in the inspector, for good compatibility use OGG Theora to encode a video, import it into your project then simply drag it into your “Movie To Use” box, next up we’ll break down the code to better understand what it’s doing.

using UnityEngine;
using UnityEngine.SceneManagement;

This sections actually pretty important, since we’ll want to be able to handle what scene we’re currently on, we must include the line “using UnityEngine.SceneManagement” so our references later to controlling which scene we want using SceneManager will actually work, without it the code won’t do anything and will likely even throw an error your way!

 public MovieTexture movieToUse; 

This allows us to attach a movie easily to the script, which is then used by the script whenever it is referenced.

    private void Start()
    {
        movieToUse.Play();
    }

This basically tells the script to play the movie defined by movieToUse.

    void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), movieToUse, ScaleMode.StretchToFill);
    }

We now setup a GUI element, in this case a texture, which is positioned and scaled to always perfectly fill and fit the screen regardless of format/resolution, this *may* cause minor stretch issues for people using ultrawide or 4:3 layout displays, which would manifest in the image seeming a little squashed or stretched.

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape) || Input.GetButtonDown("XB1_A"))
        {
            SceneManager.LoadScene("Title");
        }
        if (movieToUse.isPlaying == false)
        {
            SceneManager.LoadScene("Title");
        }
    }

This is simply setup to check if escape or the A button on an xbox one controller is pressed, if it is, the script tells the scene manager to go straight to the defined scene, in our case the menu scene (Title), you’ll want to change this bit of code to the name of the scene you want to load after the logo/intro.
The second section checks to see if the movie is actually playing, if it is the script does nothing, however if the movie is no longer playing it will advance to the defined scene, again in this case the menu scene.

One thing to keep in mind is that certain video formats will not work on certain devices, the above works just fine for PC/Android, but if exporting to a developer enabled xbox one, the movie does not work and instead displays the last frame of the video file for the duration of the video length, I’ll be looking into this and will update this post with a solution once I find one!