- Home /
MovieTexture in UI Panel
I'm trying to play a video in the UI, with my canvas in Screen Space Camera Render Mode.
I've something similar in the past with the old UI system, but I'm trying to do this with the Unity 5's new UI system.
Alternatively, I can set the render mode to World Space and place a Quad where I want to display the video, the only problem with this is that I would need to resize the size of my orthographic camera based on the size of the canvas. Is there a built in way to this?
Answer by moxiewhimsy · Apr 05, 2015 at 04:49 AM
You can do this with a UI > RawImage component. Set the Movie Texture as the Texture of the RawImage component. The Material field can be used to dress up the playback.
No problem. :) Also note you can use the same method to add Render Textures to the new UI.
Oh snap, just remembered render textures are free now. Will definitely play with this in the future :)
Answer by Fattie · Jul 03, 2015 at 08:09 AM
In conjunction to Moxie's answer and to save someone some typing, here's a handy script - just attach it to the RawImage and play
Use space, and use WADX, QE, ZC and S keys.
/*
(C) 2015
your R&D lab
*/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Events;
public class MovieDev:MonoBehaviour
{
MovieTexture mt;
RectTransform rt;
Vector2 origPos;
void Awake()
{
rt = GetComponent<RectTransform>();
origPos = rt.anchoredPosition;
RawImage rim = GetComponent<RawImage>();
mt = (MovieTexture)rim.mainTexture;
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
if (mt.isPlaying)
{
mt.Stop();
}
else
{
mt.Stop();
mt.Play();
}
}
if (Input.GetKeyDown(KeyCode.W))
rt.Translate(0f, 1f, 0f);
if (Input.GetKeyDown(KeyCode.X))
rt.Translate(0f, -1f, 0f);
if (Input.GetKeyDown(KeyCode.A))
rt.Translate(-1f, 0f, 0f);
if (Input.GetKeyDown(KeyCode.D))
rt.Translate(1f, 0f, 0f);
if (Input.GetKeyDown(KeyCode.S))
{
rt.anchoredPosition = origPos;
rt.eulerAngles = Vector3.zero;
rt.localScale = new Vector3( 1f, 1f, 1f );
}
if (Input.GetKeyDown(KeyCode.Q))
rt.Rotate(0f, 0f, .25f);
if (Input.GetKeyDown(KeyCode.E))
rt.Rotate(0f, 0f, -.25f);
if (Input.GetKeyDown(KeyCode.Z))
{
float scaleNow = rt.localScale.x;
if (scaleNow < 0.5f ) return;
scaleNow = scaleNow - 0.01f;
rt.localScale = new Vector3( scaleNow, scaleNow, 1f );
}
if (Input.GetKeyDown(KeyCode.C))
{
float scaleNow = rt.localScale.x;
if (scaleNow > 1.5f ) return;
scaleNow = scaleNow + 0.01f;
rt.localScale = new Vector3( scaleNow, scaleNow, 1f );
}
}
}