Hi all, I want to make a nice video intro to a small game I'm working on. I've imported my video-file to Unity 2.0, but I can't figure out how to show my videotexture in a GUI-element. 1. I'd like to use the new fancy schmancy GUI-system (making a GUI.Box and then loading the videotexture into that), but then how do I play the audio? 2. And more importantly, how do I play the videotexture? The manual only mentions playing a videotexture using this code: Code (csharp): renderer.material.mainTexture.Play() ...but what's the code for accessing a videotexture in a GUI.Box?
Stepping out on a limb here hoping to be corrected by UT! :wink: But the short answer is that you can't use a movie texture in the new GUI system. You need to use movie textures attached to a GUITexture, or anything else that takes a material. You could probably hack together a solution of using a movie texture with the new GUI system by using another camera and a render to texture if you are using Pro. If not you are stuck with using something else other then the new GUI stuff. Attach script below to a GUITexture that has a movie as it's texture. Code (csharp): import UnityEngine.GUILayout; var skin : GUISkin; var windowRect = Rect (20, 20, 400, 450); private var btnName = "Play"; function OnGUI () { GUI.skin = skin; DoItGUI(); } function OnSceneGUI() { OnGUI(); } private function DoItGUI () { BeginArea( windowRect, GUIContent("An Extremely Basic MoviePlayer"), GUI.skin.window ); Space (5); Label("Playback control:"); Space (5); if (guiTexture.texture.isPlaying) btnName = "Pause"; else btnName = "Play"; if (Button(btnName)) { if (guiTexture.texture.isPlaying) guiTexture.texture.Pause(); else guiTexture.texture.Play(); } Space (5); if (Button("Stop")) { //this will stop the movie, and next time play is clicked it will start at the beginning again guiTexture.texture.Stop(); } EndArea(); } function Start () { //if the gameobject has a renderer then use //renderer.material.mainTexture.Play(); //or in this case if (guiTexture.texture.isReadyToPlay) { //set it to loop guiTexture.texture.loop = true; guiTexture.texture.Play(); } } Cheers.
Wrong, consider yourself corrected. You can most definitely play a movie texture within the new GUI structure and here is an example chunk of code that works like a charm: Code (csharp): var MyVideoTexture : MovieTexture; function OnGUI () { if (!MyVideoTexture.isPlaying) { MyVideoTexture.Play(); } GUI.Box(Rect(10,10,320,240), MyVideoTexture); } Where I've attached the above script to a Game Object in the scene, then drag-assigned the video I want to play to the variable MyVideoTexture in the Inspector. If you want the audio to be heard then add an Audio Source component and drag assign the video's audio clip (expand the movie texture in the Project view and you'll see its audio clip shown) as the audio clip you'd like to play. Done.
HiggyB, now I have your attention... how do you stretch that MovieTexture to be full screen? That's the issue I was having before I think. Couldn't figure out how to make the Movie scale with the GUI.Box, if I made it's Rect(0,0,Screen.width,Screen.height) and the Movie was only 320x240. That's why I used a GUITexture. Please enlighten me. :wink: Cheers.
Hot damn, HiggyB - your code is a lot better and works like a charm, thanks a lot! But I'm having the same problem with scaling the video, how do you get the video to expand to the width (not height) of the screen, but still maintain it's aspect ratio?
also, the video-area goes completely green for like a second before playing the video, is there any way around that?
If you want to scale your video then you'll have to set the GUI.matrix to a scaled (and potentially offset and rotated ) matrix. For example, here is an example that doubles the size of the playing video: Code (csharp): var MyVideoTexture : MovieTexture; function OnGUI () { if (!MyVideoTexture.isPlaying) { MyVideoTexture.Play(); } var tOffset = Vector3 (0.0, 0.0, 0.0); var tRotation = Quaternion.Euler(0, 0, 0); var tScale = Vector3(2.0, 2.0, 1.0); var tMatrix = Matrix4x4.TRS(tOffset, tRotation, tScale); GUI.matrix = tMatrix; GUI.Label(Rect(10, 10, 320, 240), MyVideoTexture); } Of course if you want it to fill the screen then you'll need to look at the source size and the current screen size and scale along X and Y appropriately (as opposed to the example above just doubling on both X and Y). In doing that if you want the aspect ratio maintained then you need to be the one to do that. Also notice the offset and rotation abilities! Feel free to experiment with offset values other than a zero vector (offset on X/Y and not Z) and non-zero rotation values (rotate on Z and not X/Y). There are other ways to do this with Texture2D elements, but for videos in particular this is how you do it. Have fun!
That may simply be the initial frame draw or a placeholder for your video while it's first getting streamed into memory so you may need to cover it for an instant with a static Texture2D of the first "real" frame, or have it play offscreen for one frame (so that green bit is hidden from the user) then right after move it back into the normal visible region.