Search Unity

video intro

Discussion in 'Immediate Mode GUI (IMGUI)' started by peckinpah, Oct 29, 2007.

  1. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    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):
    1. renderer.material.mainTexture.Play()
    ...but what's the code for accessing a videotexture in a GUI.Box?
     
  2. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    anyone? c'mon, this can't be that hard for a really cool unity guy/girl :)
     
  3. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    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):
    1.  
    2. import UnityEngine.GUILayout;
    3.  
    4. var             skin : GUISkin;
    5. var             windowRect = Rect (20, 20, 400, 450);
    6. private var     btnName = "Play";
    7.  
    8. function OnGUI () {
    9.     GUI.skin = skin;
    10.     DoItGUI();
    11. }
    12.  
    13. function OnSceneGUI() {
    14.     OnGUI();
    15. }
    16.  
    17. private function DoItGUI () {
    18.     BeginArea( windowRect, GUIContent("An Extremely Basic MoviePlayer"), GUI.skin.window );
    19.     Space (5);
    20.     Label("Playback control:");
    21.     Space (5);
    22.     if (guiTexture.texture.isPlaying) btnName = "Pause"; else btnName = "Play";
    23.     if (Button(btnName)) {
    24.         if (guiTexture.texture.isPlaying) guiTexture.texture.Pause();
    25.         else guiTexture.texture.Play();
    26.     }
    27.     Space (5);
    28.     if (Button("Stop")) {
    29.         //this will stop the movie, and next time play is clicked it will start at the beginning again
    30.         guiTexture.texture.Stop();
    31.     }
    32.     EndArea();
    33. }
    34.  
    35. function Start () {
    36.     //if the gameobject has a renderer then use
    37.     //renderer.material.mainTexture.Play();
    38.     //or in this case
    39.     if (guiTexture.texture.isReadyToPlay) {
    40.         //set it to loop
    41.         guiTexture.texture.loop = true;
    42.         guiTexture.texture.Play();
    43.     }
    44.    
    45. }
    46.  
    Cheers.
     
  4. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    Great!

    Thanks a bunch, I'll try it out!

    Sorry for the late reply :-/

    /lars
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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):
    1. var MyVideoTexture : MovieTexture;
    2.  
    3. function OnGUI () {
    4.   if (!MyVideoTexture.isPlaying) { MyVideoTexture.Play(); }
    5.   GUI.Box(Rect(10,10,320,240), MyVideoTexture);
    6. }
    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.
     
  6. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    Well there you go! 8)

    Cheers.
     
  7. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    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.
     
  8. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    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?
     
  9. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    also, the video-area goes completely green for like a second before playing the video, is there any way around that?
     
  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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):
    1. var MyVideoTexture : MovieTexture;
    2.  
    3. function OnGUI () {
    4.    
    5.   if (!MyVideoTexture.isPlaying) { MyVideoTexture.Play(); }
    6.    
    7.   var tOffset = Vector3 (0.0, 0.0, 0.0);
    8.   var tRotation = Quaternion.Euler(0, 0, 0);
    9.   var tScale = Vector3(2.0, 2.0, 1.0);
    10.   var tMatrix = Matrix4x4.TRS(tOffset, tRotation, tScale);
    11.   GUI.matrix = tMatrix;
    12.        
    13.   GUI.Label(Rect(10, 10, 320, 240), MyVideoTexture);
    14.    
    15. }
    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!
     
  11. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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.
     
  12. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    nevermind, I'll make a new thread, and quit my hijackin' :wink:
     
  13. VeganApps

    VeganApps

    Joined:
    Jun 30, 2006
    Posts:
    263
    Great examples Tom! Thanks.. they saved my sunday. :D
     
  14. don_vladimiro

    don_vladimiro

    Joined:
    Jul 21, 2011
    Posts:
    28
    hi all
    a direct question ...
    What options are there for IOS?
    don_vladimiro on iMac G4
     
  15. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    iPhoneUtility.PlayMovie in MOV / M4V iOS compatible format fullscreen
    thats the only option
     
  16. don_vladimiro

    don_vladimiro

    Joined:
    Jul 21, 2011
    Posts:
    28
    What about the orientation of the video?
    I had problems with auto-rotation of the same