Categories
API Articles Blog C# Coders Codex Creative Computing Development Featured Games Development Graphic Displays IDE Installation Interactive Installation Languages Library London Network OpenGL OpenGL Programming Portfolio Programming Science & Technology Solution Technology Video Visual Studio 2012 Visuals

Coders Codex::Decoding Video Streams to Textures in Unity 3d

INTRODUCTION

Here we will decode an Ogg Theora file streaming from the internet onto a plane surface scripted in Unity 3d.

DISCUSSION

The fastest method to do this as of writing is to look here and then attach this to the camera in a new unity 3d scene.

FINDINGS

The Unity docs do not seem to show a C# source example, so in full Blue Peter (UK) style: Here is one I made earlier.

//============================================================================
// Name        : WWWVideoTexture (Unity 3d)
// Author      : Divide To Infinity
// Version     : V1.0
// Copyright   : GNU GPL v2
// Description : C# Monobehaviour Script for Decoding Video Streams to Texture
//============================================================================

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(GUITexture))]
[RequireComponent(typeof(AudioSource))]
public class WWWVideoTexture : MonoBehaviour
{
    public string url = "http://playground.html5rocks.com/samples/html5_misc/chrome_japan.ogv";
    IEnumerator Start()
    {
        WWW www = new WWW(url);
        MovieTexture movieTexture = www.movie;
        while (!movieTexture.isReadyToPlay)
        {
            yield return null;
        }
        
        GUITexture gt = GetComponent();
        gt.texture = movieTexture;
        transform.localScale = new Vector3(0, 0, 0);
        transform.position = new Vector3(0.1f, 0.25f, 0);

        gt.pixelInset = new Rect(0, 0, movieTexture.width / 2, movieTexture.height / 2);

        AudioSource aud = GetComponent();
        aud.clip = movieTexture.audioClip;
        movieTexture.Play();
        aud.Play();
    }
}

And the screenshot:

WWWVideoTexture.cs in Unity 3d

LINKS

WWW.movie in Unity 3d Scripting API
Video Streaming solutions for Unity3d on Stack Overflow
C# pixelInset problem on UnityAnswers
Streaming Textures in Unity3d
Video Playback Speed Discussion on Unity 3d Forums

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.