smallarrows.GIF
titlebar_image

Unity Video - AVPro video plugin VIDEO PLAYER

You may want to use this plugin because it can play many video codecs including HAP. Unity video player is limited. This tutorial assumes you are making a simple video player, no lighting or 3D effects.

In
Unity

Choose
Window->Asset Store

Type AVPro Video (Windows) in the search bar, Select the one that is $150 
Pay and download

The AVProVideo
folder and the Plugins folder need to be in your Unity project Assets folder.
You can copy these between projects.

A SteamingAssets folder was created by AVPro, in your Assets folder. Put your movies here.


Basic Scene Setup for a video player on a 3D object, no lighting
Switch out of the Asset Store to the main Scene tab

Save your scene as a new name. This is a scene within the project.

For efficiency you can delete the default Directional Light in the Hierarchy tab

Select the Main Camera
     Notice the Main Camera's default Position
is 0, 1, -10 x,y,z, Field of View 60
     Clear Flags change Skybox to Solid Color
     Change the Background color to black
    Uncheck Occlusion Culling
     Uncheck Allow HDR
     Uncheck Allow MSAA
     Uncheck Audio Listener (unless you have audio)

Choose Edit->Project Settings->Quality

     Trash all the Levels except Low  (Video does not need high)
     All the other defaults for Low should be good.
          Texture quality full res probably important

Choose Window->Rendering->Lighting Settings
   Skybox Material, Select the unity_dot button and choose None
    Environmental Lighting, select Source Color
    Change the Ambient Color to black
   
Environmental Reflections change Source to Custom
    Uncheck Realtime Global Illumination

    Uncheck Baked Global Illumination


Add a Media Player Object to your Hierarchy tab
Select GameObject->AVpro->Media Player
     In the Inspector on the right:
     Click Browse
to add your movie (located in your StreamingAssets folder)
     Select Playback   Loop
     Under the Platform Specific button
          Select Windows
               Select Preferred Video API  choose Direct Show

FYI defaults were:
Auto Open checked
Auto Play checked
Loop unchecked (we changed)
Playback Rate 1
Persistent unchecked
Force File Format Automatic (by extension)
Filter Bilinear
Wrap Clamp
Aniso 0
Vido Mapping Unknown
Transparency Packing None
Audio Packing None
Resampler unchecked
Windows Preferred Video API Media Foundation (we changed)
Generate Texture Mips unchecked
Media Foundation Options
     Hardware Decoding checked
     Use Low Latency unchecked
     Audio Mode System Direct
DirectShow Options
     Alpha Channel Hint unchecked
     Force Audio Output Device Name none listed
     Preferred Filters Size 0


IMGUI Script
This will let you play video on top of the scene.
Select MediaPlayer from the Hierarchy
     Choose Add Component from the Inspector
     Type displ in the search, Select Display IMGUI Script

Display IMGUI options:
Media Player
select the 
unity_dot button and choose MediaPlayer
Display in Editor checked (uncheck turns off the pink wallpaper)
Scale Mode is Scale to Fit
Color white
(any other color tints or fades the video)
Alpha Blend unchecked
Use Depth unchecked
Depth 0
Full Screen
unchecked (checked by default)
X 0
Y 0
Width 1
Height 1


Hit the Play arrow to check if everything is working.
unity_play_arrow


Publish a Player
Save your Unity Scene

File->Build Settings...
     Click Player Settings... button
     Open Resolution and Presentation
          Uncheck Default is Native Resolution
          Input your resolution if you want to control this.
          Change Display Resolution Dialog to Hidden by Default
Open Splash Image
     Change the Background Color to black

Select Build
Click New folder, make a new player folder on your desktop
     Select the folder, it will take a minute to build.
Doubleclick
the player.exe
     Alt + F4 to quit the player

(If you have problems with the build, uncheck any scenes listed unScenes in Build)


Add a script to hide the mouse cursor and add an Escape button

Select the Main Camera
Select the Add Component button
     Scroll down to New script
          Type a name: hide_cursor_escape
         Double click the C# script in your Assets folder

Add this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class hide_cursor_escape : MonoBehaviour {

 // Use this for initialization
 void Start () {
  Cursor.visible = false;
 }
 
 // Update is called once per frame
 void Update () {
   if (Input.GetKeyDown(KeyCode.Escape)) {
   Application.Quit();
  }
 }
}




Republish your player



Add a text overlay that fades
Select GameObject->UI->Text
     Type something in the Text field in the Inspector
    Input Pos X and Pos Y to 0
Move the Text around with the Move Tool


Add this textController script to the text, under Canvas:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class textController : MonoBehaviour
{
    private IEnumerator fadeCoroutine;

    // Use this for initialization
    void Start()
    {
        // title text: invisible at first!
  Text text = GetComponent<Text>();
        text.color = new Color(1f, 1f, 1f, 1f);
  this.fadeCoroutine = FadeTo(0.0f, 6f, 0.25f);
  StartCoroutine(this.fadeCoroutine);
    }

    /* Coroutine for animating the opacity of title text when a new movie is selected.
     * aTime is how long the title is visible before fade begins
     * aFadeTime is how long the title takes to disappear  */

    IEnumerator FadeTo(float aValue, float aTime, float aFadeTime)
    {
        for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
        {
            yield return null;
        }

  Text text = GetComponent<Text>();
        float alpha = text.color.a;
        for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aFadeTime)
        {
            Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
            text.color = newColor;
            yield return null;
        }

        // just make sure it doesn't stick around at end of loop;
        // goes completely invisible.
        Color finalColor = new Color(1, 1, 1, aValue);
        text.color = finalColor;
  Destroy(this.transform.parent.gameObject);
    }
}



Script to switch movies

Add this switch_art script to the MediaPlayer

using UnityEngine;
using System;
using System.IO;
using RenderHeads.Media.AVProVideo;

public class switch_art : MonoBehaviour
{
    [SerializeField]
    private MediaPlayer _mediaPlayer;

    public void LoadVideo(string filePath)
    {
        _mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, filePath, true);
    }

    [Tooltip("Relative to StreamingAssets movie file 1.")]
    public string movie1File;
    [Tooltip("Relative to StreamingAssets movie file 2.")]
    public string movie2File;
  
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            _mediaPlayer.CloseVideo();
            _mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, movie1File);
        }
  if (Input.GetKeyDown(KeyCode.Alpha2))
  {
            _mediaPlayer.CloseVideo();
            _mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, movie2File);
  }
    }
}



Add Video to a 3D Object
Choose GameObject->3D Object Quad
     Position the Quad at   0, 1, -9.134    x,y,z
     Scale X to match your aspect ratio, ie 1920x1200 = 1.6
     Under Mesh Renderer
          Light Probes Off
          Reflection Probes Off
          Cast Shadows Off
          Uncheck Receive Shadows
          Uncheck Dynamic Occluded

     Open Materials (kind of hidden)
          Element 0
unity_dot button and choose VideoMappingMaterial
              Change the Shader for the VideomappingMaterial to AVProVideo/Unlit/Opaque

     Turn off
Mesh Collider

Choose the MediaPlayer object from the Hierarchy tab
Choose the
Add Component button
     Choose AVPro Video->Apply to Mesh  
     Within the Apply to Mesh section:
          Media
unity_dot button and choose MediaPlayer

          Mesh  unity_dot button and choose Quad

 

© 1988-2024 36 years