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
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): var imageArray : Texture[]; var currentImage : int; var imageRect : Rect; var buttonRect : Rect; function Start() { currentImage = 0; imageRect = Rect(0, 0, Screen.width, Screen.height); buttonRect = Rect(0, Screen.height - Screen.height / 10, Screen.width, Screen.height / 10); } function OnGUI() { GUI.Label(guiRect, imageArray[currentImage]); if(GUI.Button(buttonRect, "Next")) currentImage++; } And that should do the trick.
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): if(currentImage > 1) currentImage = 0; Thank you, sir!
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): if(currentImage >= imageArray.Length) currentImage = 0;
Making a simple slideshow updated to work in unity 2019 LTS Thx to all the above for saving me a lot of time. next step is to add a timer instead of a button. (might try and make the button invisible or something to fill screen so its just mouse click to change slide) Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class slideshow1 : MonoBehaviour { // var imageArray : Texture[]; // var currentImage : int; // var imageRect : Rect; // var buttonRect : Rect; public Texture[] imageArray; private int currentImage; //private Rect imageRect; //private Rect buttonRect; void OnGUI() { int w = Screen.width, h = Screen.height; //Rect imageRect = new Rect (0, 0, w, h * 2 / 100); Rect imageRect = new Rect(0, 0, Screen.width, Screen.height); Rect buttonRect = new Rect(0, Screen.height - Screen.height / 10, Screen.width, Screen.height / 10); // GUI.Label(guiRect, imageArray[currentImage]); GUI.Label(imageRect, imageArray[currentImage]); if(GUI.Button(buttonRect, "Next")) currentImage++; if(currentImage >= imageArray.Length) currentImage = 0; } // Start is called before the first frame update void Start() { currentImage = 0; // imageRect = Rect(0, 0, Screen.width, Screen.height); // buttonRect = Rect(0, Screen.height - Screen.height / 10, Screen.width, Screen.height / 10); } // Update is called once per frame void Update() { } }
Here is a more advanced version with a timer, keyboard control, mouse control added some ergonomic functionality, escape key to exit, p key or right mouse to pause the timer1 left mouse or spacebar to skip to next slide video tutorial: used GUI.DrawTexture(imageRect, imageArray[currentImage]); Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class slideshow1 : MonoBehaviour { public Texture[] imageArray; private int currentImage; float deltaTime = 0.0f; public float timer1 = 5.0f; public float timer1Remaining = 5.0f; public bool timer1IsRunning = true; public string timer1Text; // added ergonomic functionality, // escape key to exit, // p key or right mouse to pause the timer1 // left mouse or spacebar to skip to next slide void OnGUI() { int w = Screen.width, h = Screen.height; Rect imageRect = new Rect(0, 0, Screen.width, Screen.height); //dont need to make button transparent but would be cool to know how to. //Rect buttonRect = new Rect(0, Screen.height - Screen.height / 10, Screen.width, Screen.height / 10); //GUI.Label(imageRect, imageArray[currentImage]); //Draw texture seems more elegant GUI.DrawTexture(imageRect, imageArray[currentImage]); //if(GUI.Button(buttonRect, "Next")) //currentImage++; if(currentImage >= imageArray.Length) currentImage = 0; } // Start is called before the first frame update void Start() { currentImage = 0; bool timer1IsRunning = true; timer1Remaining = timer1; } // Update is called once per frame void Update() { Cursor.visible= false; Screen.lockCursor = true; deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f; if (Input.GetKey(KeyCode.Escape)) { #if UNITY_EDITOR if (EditorApplication.isPlaying) { EditorApplication.isPlaying = false; } #else Application.Quit(); #endif } if (Input.GetMouseButtonDown(0)) { UnityEngine.Debug.Log("Pressed primary button."); currentImage++; if(currentImage >= imageArray.Length) currentImage = 0; } if (Input.GetKey(KeyCode.Space)) { UnityEngine.Debug.Log("Pressed space bar."); currentImage++; if(currentImage >= imageArray.Length) currentImage = 0; } if (Input.GetMouseButtonDown(1)) { UnityEngine.Debug.Log("Pressed secondary button."); timer1IsRunning = !timer1IsRunning; } if (Input.GetKey (KeyCode.P)) { //ispaused = !ispaused; timer1IsRunning = !timer1IsRunning; } if (timer1IsRunning) { if (timer1Remaining > 0) { timer1Remaining -= Time.deltaTime; } else { UnityEngine.Debug.Log("Time has run out!"); currentImage++; if(currentImage >= imageArray.Length) currentImage = 0; timer1Remaining = timer1; } } } }
New THIS IS SO COOL. I HAVE BEEN TRY THIS OUT AND ITS PERFECT!! THIS IS SIMILAR LIKE WHAT I NEED. BUT DO YOU KNOW HOW TO MAKE SLIDESHOW LIKE THIS BUT THE IMAGE IS CALLING THROUGH THE FILE FROM HARD DISK? BECAUSE I HAVE THE CODING ( CALLING IMAGE FROM FILE) BUT I DONT KNOW WHERE PART I NEED TO CHANGE YOUR CODING? CAN YOU HELP ME OUT? HERE I ATTACH THE CODING Texture2D thisTexture; byte[] bytes; string fileName; public GameObject[] ImageHolder = new GameObject[4]; // Start is called before the first frame update void Start() { var imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png"); for (int i = 0; i < imagesToLoad.Length; i++) { thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP fileName = imagesToLoad; bytes = File.ReadAllBytes(fileName); thisTexture.LoadImage(bytes); thisTexture.name = fileName; ImageHolder[i++].GetComponent<RawImage>().texture = thisTexture; #IM SO SORRY FOR MY BROKEN ENGLISH. I HOPE YOU UNDERSTAND AND HELP ME OUT. THANKYOU SO MUCH!!
You may also need this asset with advanced customization features for slideshow. It helps lot.. https://assetstore.unity.com/packages/tools/animation/ui-slideshow-133545