Search Unity

Simple Image Slideshow

Discussion in 'Scripting' started by MirrorsEdge, Feb 17, 2011.

  1. MirrorsEdge

    MirrorsEdge

    Joined:
    Feb 14, 2011
    Posts:
    8
    Good day gentlemen,

    I'm trying to implement a simple image slideshow on a GUI window using labels. I can get a single image to display using a label but having some difficulties creating a button that switches to the next image. What I want is a button named 'next' that when clicked on displays another image. And again, clicking on next displays the next image.

    I think I need to use an image array but my coding is mediocre to implement it. Can I get some assistance or an example, preferably in JavaScript?

    Thank you
     
  2. Myx

    Myx

    Joined:
    Nov 29, 2010
    Posts:
    196
    Hello there!

    Right then, lets see if we can sort your problem out.
    An array of textures would indeed be a way to solve it.
    Now I'm a C# coder so I can't be certain the following script will compile, but it should give you the general idea at the very least.

    Code (csharp):
    1.  
    2. var imageArray : Texture[];
    3. var currentImage : int;
    4. var imageRect : Rect;
    5. var buttonRect : Rect;
    6.  
    7. function Start()
    8. {
    9.     currentImage = 0;
    10.     imageRect = Rect(0, 0, Screen.width, Screen.height);
    11.     buttonRect = Rect(0, Screen.height - Screen.height / 10, Screen.width, Screen.height / 10);
    12. }
    13.  
    14. function OnGUI()
    15. {
    16.     GUI.Label(guiRect, imageArray[currentImage]);
    17.     if(GUI.Button(buttonRect, "Next"))
    18.         currentImage++;
    19. }
    20.  
    And that should do the trick.
     
  3. MirrorsEdge

    MirrorsEdge

    Joined:
    Feb 14, 2011
    Posts:
    8
    Fantastic, just what I wanted.

    A little typo on your code; guiRect should be imageRect. And I had a problem with the next button closing the window once it came to the last image. A little if statement fixed that.

    Code (csharp):
    1. if(currentImage > 1)
    2. currentImage = 0;
    Thank you, sir! :)
     
    Last edited: Feb 18, 2011
  4. RodrigoSeVeN

    RodrigoSeVeN

    Joined:
    Jul 10, 2010
    Posts:
    13
    In case someone sees this, what you want to do is change that if statement to check the actual length of the array of images as such:

    Code (csharp):
    1. if(currentImage >= imageArray.Length)
    2. currentImage = 0;
     
    Last edited: Oct 7, 2014