Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[HOW-TO] Play Videos Without Pro

Discussion in 'Scripting' started by LaytProducts, Aug 10, 2011.

  1. LaytProducts

    LaytProducts

    Joined:
    Aug 5, 2011
    Posts:
    54
    So I am new here and have been looking for little while to see if its possible to play a video (splash screen) without having to get pro (really don't have $1500 lying around at the moment).

    So I made my own method! I thought I would share it (JavaScript).

    The code can be found below, but you might need to know how exactly to get the videos into the proper format and what not.

    So what you need to do is get images that will represent your video (literally frames). I used Photoshop.

    PHOTOSHOP INSTRUCTIONS:
    Get the animation/make the animation that you will use for your video. Now go to File>Export>Render Video. Pick an image (like JPEG or PNG). Pay attention to the FPS you pick (I use 30 fps) because you will need to use that in your code.

    AAE INSTRUCTION:
    So now that you have the images (should be 30 pictures per each second it will run if you picked 30fps) put them in a folder named what they are (examples are best: if you have a file named this, "MySplashScreen00001.JPEG" then save it into a folder named, "MySplashScreen").

    Go into your Project folder in Unity and press Create>Folder and name it Resources. Now import the folder with all the images into the Resources folder. You are basically done!

    Add my script to your MainCamera in your scene (an EmptyObject might also be possible). Edit the script to fit your resources.

    Example:
    If I have saved my video with an FPS of 27 and there are 7 digits after the name (MyVideo0000001) then I would go like this:
    Code (csharp):
    1.  
    2. PlayVideo(27/*the number of frames*/,"MyVideo"/*Name/path of the file and folder*/,7/*7 digits after MyVideo*/,"MyNextScene/*Scene that will open after the video is done playing*/");
    3.  
    The FPS is not extremely important, it will still function if it is incorrect but it may seem faster or slower than you initially made it.

    This can also be used to play videos In-Game if you put it in your Update function and if you Overload the function so it also doesn't need a Scene (it won't load a new scene after).

    Code (csharp):
    1.  
    2. var mTex : Texture2D;
    3.  
    4. function Start(){
    5.     PlayVideo(30,"LaytProductsSplash",4,"MainMenu");
    6. }
    7.  
    8. function Update(){
    9.  
    10. }
    11.  
    12. function PlayVideo(fps : int, path : String, numberDigits : int, levelAfter : String){
    13.     var i : int = 0;
    14.     var done : boolean = false;
    15.     var allImages : int = Resources.LoadAll(path,Texture2D).length - 1;
    16.     print(allImages.ToString());
    17.     while(!done){
    18.         var digits : String;
    19.         if(i > allImages){
    20.             done = true;
    21.             break;
    22.         }
    23.         if(i < 10  i >= 0){
    24.             for(var w : int = 0; w < numberDigits-1; w++){
    25.                 digits = digits + "0";
    26.             }
    27.             digits = digits + i;
    28.         }
    29.         if(i < 100  i >= 10){
    30.             for(var x : int = 0; x < numberDigits-2; x++){
    31.                 digits = digits + "0";
    32.             }
    33.             digits = digits + i;
    34.         }
    35.         if (i < 1000  i >= 100){
    36.             for(var y : int = 0; y < numberDigits-3; y++){
    37.                 digits = digits + "0";
    38.             }
    39.             digits = digits + i;
    40.         }
    41.         if (i < 10000  i >= 1000){
    42.             for(var z : int = 0; z < numberDigits-4; z++){
    43.                 digits = digits + "0";
    44.             }
    45.             digits = digits + i;
    46.         }
    47.         var currentFile : String = path+"/"+path+digits;
    48.         print(currentFile);
    49.         var videoTexture : Texture2D = Resources.Load(currentFile);
    50.    
    51.         mTex = videoTexture;
    52.         i++;
    53.         yield WaitForSeconds(1/fps);
    54.     }
    55.     Application.LoadLevel(levelAfter);
    56. }
    57.  
    58. function OnGUI(){
    59.     GUI.DrawTexture(Rect(0,0,480,360),mTex);//480x360 is size of game in PlayerSettings.
    60.         //check resolution by going to Edit>Project Settings>Player and look under Resolution
    61. }
    62.  
    You can easily edit out the LoadLevel part if you don't want it to be something like a SplashScreen.

    Post here if you guys enjoy it ;)

    Any suggestions? Post here.

    My first public code so please post fixes
     
    Last edited: Aug 11, 2011
  2. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Wow, looks awesome! Thank you so much, I'm going to have to try this out!
     
  3. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Hmm. So making individual images rather than exporting it as a video texture huh. But i think this is feasible only for short videos. For videos that are longer than say 1 minute, wouldn't it be lots of image you have to import it?

    Edit: Maybe you could post a mini project/package than posting the whole script.
     
    Last edited: Aug 11, 2011
  4. LaytProducts

    LaytProducts

    Joined:
    Aug 5, 2011
    Posts:
    54
    if you do 30fps (which is average) a minute long video would be 1800 images. Kinda big unless you have only a few.

    I think I will take that advice (plus I might further expand on this in the future)
     
  5. Fierce Waffle

    Fierce Waffle

    Joined:
    Dec 23, 2010
    Posts:
    93
    Assuming you have Adobe After Effects, you can import whatever footage you want. Then add that composition to your render que by pressing Ctrl + Shift + / Then where it says something like lossless click that then click the dropdown button and you should be able to choose JPEG Sequence. It will then export each frame as its own jpeg image.
     
  6. LaytProducts

    LaytProducts

    Joined:
    Aug 5, 2011
    Posts:
    54
    Fierce I am going to add that quote to the main post to help AAE users.
    Thanks for the information
     
  7. pixelsteam

    pixelsteam

    Joined:
    May 1, 2009
    Posts:
    924
    This is great work, I have been looking for something just like this.

    Thanks for sharing!
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,090
    You could just spend a few dollars for prime31's movie playback stuff too and much more sensible than this approach. Don't mean to be negative but its kind of insane how many files you'll need :)
     
  9. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Wow. Didn't really know prime31 have something like this. Guess i i didn't really need to use it atm. But thanks. :D
     
  10. LaytProducts

    LaytProducts

    Joined:
    Aug 5, 2011
    Posts:
    54
    I agree, it is insane. But I really don't want to have to spend some of my tiny budget for movie playback stuff when I could just use this to play a 5 second splash screen. :)
     
  11. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,278
    Actually, you can take the end result and make a single animated gif....
     
  12. LaytProducts

    LaytProducts

    Joined:
    Aug 5, 2011
    Posts:
    54
    Yes, but what if you need to play something after something else is played? You can't tell if an animated gif is finished or not (unless you yield for the exact amount of time the gif is supposed to play, which I don't think is a good idea at all).
     
  13. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,128
    honestly its probably a better idea than using thousands of raw images.
     
  14. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    I thought Unity did not support animated .gif files? If you can use animated .gif, can you point me to a tutorial, because I can't find one...
     
  15. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    I was thinking just the same thing when reading the thread. Maybe it's a new feature of Unity?
     
  16. suapollaaa

    suapollaaa

    Joined:
    Jul 30, 2014
    Posts:
    1
    The idea is great ty man!
    I only had to change 2 things:
    - Path formula, that code is adding path 2 times when taking the pictures
    - Fps formula for waitforseconds, now is doing 1/fps, thats not correct, I made fps *0.001 and now goes perfect

    I read all comments and I din't see anyone commenting this, have I been the only one with this problem?
     
unityunity