QTPlayback

From Unify Community Wiki
Jump to: navigation, search


Author: Frank McPherson

Contents

Description

This plugin uses the QuickTime library to play a movie back to a Unity texture. If the movie contains sound, the sound should stay synchronized with the video. Interactive features of QuickTime are not supported - this is for in-order playback only. Using this (or any) C plugin requires Unity Pro.

Usage

When the texture script is attached to a GUITexture, the approach we're taking on our current project, you can get the movie to play right side up by making the Pixel Inset Y Min greater than the Y Max.

When the texture script is attached to a primitive object, you can flip the movie rightside up by attaching a material and changing the Position->Y Scale to -1. Thanks to NCarter for the suggestion on that one.

If getting Error -43, check that the MoviePath() paths are correct in TexturePlayback.cs. Better yet, expose the paths as vars and you can set them in the Inspector.--casemon 01:07, 13 May 2007 (PDT)

Note for use with Unity Versions 3.x.x the function GetInstanceID needs to be replaced with GetNativeTextureID --lowbloodsugar

Caveats

This plugin was written when Unity3D published to only the Mac OS X, Windows, and Web Player platforms, and each of the platforms used the OpenGL renderer. Since that time, Unity has shipped the capability to import and play back Ogg Theora movies, which has obviated much of the original need for this plugin. It will also no longer function correctly on Windows players with the default renderer, since Windows players now use the Direct3D renderer by default. One could modernize the plugin to support the D3D renderer on Windows by replacing calls to GetInstanceID() and GetNativeTextureID() with Texture.GetNativeTexturePtr(). --fhmiv Mon Feb 25 10:35:42 EST 2013

Downloads

I've provided a number of downloads for this example. The first download contains the full Unity project with the simple example assets.

Updated 20070404 version from the forums, Unity Project with Assets

Original 20070209 Unity Project with Assets

The second download contains the XCode and Visual Studio projects I used to build the plug-in (restored).

Original 20070206 version of the Source Code

Versions

  • Added 20070404 update from forums. --casemon 13:15, 7 July 2007 (PDT)
  • Restored source code link (thanks WikiHistory and Neil!) Looks like this was accidentally edited; the edit was incomplete, open brackets, missing info, etc. --casemon 03:04, 2 May 2007 (PDT)
  • Added Jergen's modified csharp script from the forums. --casemon 03:04, 2 May 2007 (PDT)

Alternate Code

  • This code was originally for the 20070209 version and likely needs updating to work with the 20070404 version of the plugin.

This is Jergen's code posted on the forum; it makes using the plugin a little more accessible. To use, replace <TexturePlayback.cs> in your project with this:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
 
public class TexturePlayback : MonoBehaviour {
 
   public int width = 512;
   public int height = 512;
   public float PlayAtFramerate = 25;
   public string myMovie = "SomeMovie.mov";
   public bool PlayLoop = false;
   public bool PlayAtStart = false;
 
   private int LoopCounter = 0;
   private float MovieFramerate = .03f;
 
   private Texture2D m_Texture;
 
   [DllImport ("UnityQTPlayback")]
   private static extern int SetMovie(string moviePath, int width, int height, int textureID);
 
   [DllImport ("UnityQTPlayback")]
   private static extern void UpdateTexture(int textureID);
 
   [DllImport ("UnityQTPlayback")]
   private static extern void DisposeResources();
 
   [DllImport ("UnityQTPlayback")]
   private static extern void StartMoviePlaying();
 
   [DllImport ("UnityQTPlayback")]
   private static extern void StopMoviePlaying();
 
   [DllImport ("UnityQTPlayback")]
   private static extern Boolean IsMovieDonePlaying();
 
   void OnApplicationQuit() {
      DisposeResources();
      Debug.Log("Exiting, disposed QT stuff");
   }
 
   void Start () {
      // TODO: Convert PlayAtFramerate to float
      // MovieFramerate = .025f; //.03f;
 
 
      // Create texture that will be updated in the plugin
      m_Texture = new Texture2D (width, height, TextureFormat.RGB24, false);
 
      // Make sure that the texture name exists etc.
      m_Texture.Apply();
 
      // Assign texture to the renderer
      if (renderer){
         renderer.material.mainTexture = m_Texture;
 
      } else if (GetComponent(typeof(GUITexture))) {
         // or gui texture
         GUITexture gui = GetComponent(typeof(GUITexture)) as GUITexture;
         gui.texture = m_Texture;
 
      } else {
         Debug.Log("Game object has no renderer or gui texture to assign the generated texture to!");
      }
 
      if( PlayAtStart ) mPlay();
   }
 
 
   void mPlay(){
      int status = SetMovie(myMovie, m_Texture.width, m_Texture.height, m_Texture.GetInstanceID());
      if (status != 0) {
         Debug.Log("Error " + status + " from SetMovie()");
      } else {
         StartCoroutine("MovieUpdate");
      }
   }
 
   void mEnd(){
      StopCoroutine("MovieUpdate");
      if( PlayLoop ){
         Debug.Log("Starting loop nr: "+ LoopCounter);
         mPlay();
      }
   }
 
 
   IEnumerator MovieUpdate() {
      StartMoviePlaying();
      while (!IsMovieDonePlaying()) {
         UpdateTexture(m_Texture.GetInstanceID());
         // Need a better way to control the playback rate
         yield return new WaitForSeconds(MovieFramerate);
      }
      mEnd();
      yield break;
   }
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools