How to Play Video Files in Unity

In Unity, playing video clips can add depth and immersion to your game. Whether it's a cutscene, tutorial video, or background ambiance, integrating videos can enhance the player experience. In this tutorial, we'll walk through the steps to play videos in Unity, covering both the older method using MovieTexture and the newer VideoPlayer component.

1. Using MovieTexture (Deprecated)

Step 1: Import Video File

  • First, make sure your video file is in a format supported by Unity (such as .mp4 or .mov).
  • Drag and drop the video file into your Unity project's Assets folder.

Step 2: Create Material

  • Right-click in the Assets window, then go to 'Create -> Material'.
  • Name the material appropriately (e.g., "VideoMaterial").
  • Select the material, then in the Inspector window, choose the Shader dropdown and select "Legacy Shaders -> Diffuse".
  • Drag and drop the video file onto the Albedo slot of the material.

Step 3: Create Plane

  • Right-click in the Hierarchy window, then go to '3D Object -> Plane'.
  • Position and scale the plane as desired.

Step 4: Apply Material to Plane

  • Drag the VideoMaterial onto the plane in the Scene view.

Step 5: Write Script

  • Create a new C# script (e.g., "PlayVideo").
  • Open the script and write the following code:
using UnityEngine;

public class PlayVideo : MonoBehaviour
{
    public MovieTexture video;

    void Start()
    {
        GetComponent<Renderer>().material.mainTexture = video;
        video.Play();
    }
}

Step 6: Attach Script

  • Attach the PlayVideo script to the plane in the Hierarchy window.
  • Drag and drop the video material into the "Video" field of the PlayVideo script component.

Step 7: Play Test

  • Press the Play button to test your game. The video should play on the plane's surface.

2. Using VideoPlayer Component (Recommended)

Unity introduced the VideoPlayer component as a modern replacement for playing videos. It provides more features and better performance.

Step 1: Import Video File

  • Follow Step 1 from the previous method to import your video file.

Step 2: Create UI Canvas

  • Right-click in the Hierarchy window, then go to 'UI -> Canvas'.
  • This will create a canvas for displaying the video.

Step 3: Add Raw Image

  • Right-click on the Canvas object, then go to 'UI -> Raw Image'.
  • This will create an image object for displaying the video texture.

Step 4: Attach VideoPlayer Component

  • Select the Raw Image object in the Hierarchy window.
  • In the Inspector window, click "Add Component" and search for "Video Player".
  • Click "New" to create a new Video Player component.

Step 5: Set Video Clip

  • With the Video Player component selected, drag and drop your video file into the "Video Clip" field.

Step 6: Play Test

  • Press the Play button to test your game. The video should play on the Raw Image object.

Conclusion

In this tutorial, you've learned two methods to play videos in Unity games. While MovieTexture is deprecated, the VideoPlayer component offers more features and better performance, making it the recommended approach for playing videos in Unity.

Suggested Articles
How to Add Sniper Scope Effect in Unity
How to Trigger a Cutscene in Unity
Making Inventory and Item Crafting System in Unity
Using Runtime Animator Controller in Unity
Creating a Turret Controller in Unity
Creating a Puzzle Game in Unity
Creating a Pac-Man-Inspired Game in Unity