Search Unity

Movie Texture change by clicking

Discussion in 'Immediate Mode GUI (IMGUI)' started by momo, May 29, 2009.

  1. momo

    momo

    Joined:
    Apr 21, 2009
    Posts:
    13
    Hi people, need help urgently
    I try to change a movie texture on my GUI, I try to put my movie texture into an array the try to change the movie texture with every click, below is the click i try but does not work, can anyone point out my mistake for me? thanks a lot..



    Code (csharp):
    1. var arr = new Array();
    2. var arrayno;
    3. var movieTexture1 : MovieTexture;
    4. var movieTexture2 : MovieTexture;
    5. var no = 1;
    6. boolchange = false;
    7.  
    8. function OnGUI () {
    9.     arr[1] = movieTexture1;
    10.     arr[2] = movieTexture2;
    11.    
    12.     if(!movieTexture1.isPlaying){
    13.         movieTexture1.Play();
    14.         movieTexture1.loop = true;
    15.     }
    16.    
    17.     if(GUI.Button(Rect (400,300,250,250),"Poke me")){
    18.         arrayno = arr[no];
    19.         no += 1;
    20.         boolchange = true;
    21.     }
    22.    
    23.     if(boolchange == true){
    24.         GUI.Box (Rect (25,25,250,250),arr[no]);
    25.         print ("Change");
    26.     }
    27.    
    28.     GUI.Label(Rect (10,10,250,250), movieTexture1);
    29. }
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    In the future, please try to offer more than "it does not work". When saying that do you mean:

    1. It produces an error?
    2. It swaps in the incorrect texture/movie texture?
    3. Nothing happens at all
    4. Other?

    A simple "it does not work" leaves us with little information as to what exactly is or isn't happening on your end and thus we might burn valuable time chasing down wrong/inappropriate possible issues.


    Other tip, please wrap scripts you include in code tags, either use the button at the top of the page when making a post or manually wrap code in [ code ] and [ /code ] (remove those spaces) so it formats it nicely. I've edited your post to use code tags as an example, give it a look!


    Looking at your code:

    1. Why do you create 'arr' each time OnGUI is called (happens multiple times per frame)? You should instead declare an built-in array variable (for movie textures) that's an array to begin with (see my sample code below).

    2. Why are you potentially drawing both movie textures at the same time? Is that intended? (if the user presses poke me you'll show both)

    3. You might possibly display movieTexture2 but you never tell it to play, why not?

    4. You increment 'no' when the user presses the poke me button, but you never error check that against the length of your video texture array, that's likely to cause problems.

    5. What is arrayno and why do you have it there at all?


    Here is my simple example, it simply cycles between the movie textures with each button press:

    Code (csharp):
    1. // Untested forum code :)
    2. var arr : MovieTexture[];
    3. var no = 0;
    4.  
    5. function Update () {
    6.   if(!arr[no].isPlaying){
    7.     arr[no].Play();
    8.     arr[no].loop = true;
    9.   }
    10. }
    11.  
    12. function OnGUI () {
    13.   if(GUI.Button(Rect (400,300,250,250),"Poke me")){
    14.     no += 1;
    15.     if (no >= arr.length) { no = 0; }
    16.   }
    17.   GUI.Label(Rect (10,10,250,250), arr[no]);
    18. }
    The button press should just cycle the index variable you track, wrapping its value if necessary to stay within your array. Then you simple draw the movie texture at that array position. Separately, move the "if it's not playing poke it so it does play" code to an Update function and make sure it's always tracking the current movie texture instead of having it always check only one.

    Make sense? If not then let us know what's confusing!
     
  3. momo

    momo

    Joined:
    Apr 21, 2009
    Posts:
    13
    Sorry for not stating the problem when I am asking the question.

    thx for your help, I think i understand what you are saying.
    thanks a lot
     
  4. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    Just wondering if you can get the audio to work with this script
     
  5. mellovely

    mellovely

    Joined:
    Jan 31, 2017
    Posts:
    21
    I have a similar problem, but I have several buttons and one raw image which has a movie texture on it. I'm trying to make it so that every time a different button is clicked the corresponding movie texture is played. So button A plays video A, button B, video B and so on (and they play in the same raw image in the canvas).

    I made a video reaction editor, so that I can add it to each button and choose a video for each button, but currently only the texture that was originally on the raw image plays, the texture doesn't change when I press any other button.
    Code (CSharp):
    1.  public class VideoReaction : Reaction {
    2.      public MovieTexture video;
    3.  
    4.      public float delay;
    5.  
    6.      protected override void ImmediateReaction()
    7.      {
    8.    
    9.          if (!video.isPlaying)
    10.          {
    11.              video.Stop();
    12.          }
    13.          video.Play();
    14.      }
    15. }
    Code (CSharp):
    1.  [CustomEditor(typeof(VideoReaction))]
    2. public class VideoReactionEditor : ReactionEditor
    3. {
    4.      protected override string GetFoldoutLabel()
    5.      {
    6.          return "Video Reaction";
    7.      }
    8. }