- Home /
Show an image array in a loop
Think in a slot machine.
In one column, I got an array of X images ("Slide", in the image below, has 3 elements as example). I got a window (a GUILayout). I want that the images of the array are shown in that window, then the image moves down, and the next image of the array moves in the window. I got that. The thing is that when I reach the end of the array, there're no more images to show. I need to put back the 1st element of the array on the top of the last image. How can I do that??
I leave an image for better explanation.
Code: Here the array has 13 elements. Only can be shown 3 elements at time (it's a 3x5 slot machine, the image was illustrative, to get the idea).
function drawCol(col : int, movement :float)
{
var x : int = 0 + (col * images[0].width);
var y : float = 0;
var image : Texture2D;
GUILayout.BeginArea(Rect(slotX, slotY, images[0].width * 5, images[0].height * 3));
for(var i : int = 0; i < 13; i++)
{
if(oneTime == false)
{
// Fills the array with random symbols
var randomImage : int = Random.Range(0,images.length);
image = images[randomImage];
slotGrid[col,i] = randomImage;
}
else
{
image = images[slotGrid[col,i]]; //Gets the already stored symbols
}
// Sets y based on the height of the image (uses 0 index because all images must have the same size)
// rests images[0].height * 10 because if not, the slot grid is drawn from 0 to 1536. We need that is
// drawn from -1536 to 384 (slot grid area, where the Layout is)
y = images[0].height * i + movement - (images[0].height * 10);
if((y < images[0].height * 3) && (y >= slotY - images[0].height )) // Only draws a texture if it's in visible area
{
GUI.DrawTexture(Rect(x, y, image.width, image.height), image);
}
}
GUILayout.EndArea();
}
how do you track what your current position in the array is?
do you only maintain a current?
or do you maintain a next, and a previous?
I don't track, I just got t he y position. I have the whole array. I just display the elements that are in the window. Im posting the source code for a better understanding!
Answer by Seizure · Sep 13, 2013 at 07:32 PM
If I'm reading this correctly it sounds like you need a temp array, I would suggest using this temp array as the display array, once you reach the end of the listArray start filling the temp array with listArray 1; I'll work on a script real quick and see if it helps you.
EDIT: Hope code works for you.
using UnityEngine;
using System.Collections;
public class arrayDisplay : MonoBehaviour {
public int[] displayHolder;
public int[] tempArray;
public int counter;
// Use this for initialization
void Start () {
for (int i = 0; i < 3; i++)
{
tempArray[i] = displayHolder[i]; //Initialize tempArray
}
StartCoroutine(arrayDisplayer());
}
IEnumerator arrayDisplayer ()
{
Debug.Log ("tempArray" + tempArray[0]);
Debug.Log ("tempArray" + tempArray[1]);
Debug.Log ("tempArray" + tempArray[2]);
yield return new WaitForSeconds (1);
if (counter >= 9)
counter = 0;
else
counter++;
tempArray[0] = displayHolder[counter]; //Initialize tempArray
if (counter >= 9)
counter = 0;
else
counter++;
tempArray[1] = displayHolder[counter]; //Initialize tempArray
if (counter >= 9)
counter = 0;
else
counter++;
tempArray[2] = displayHolder[counter]; //Initialize tempArray
StartCoroutine(arrayDisplayer());
}
}
Thanks for taking the time to code that! I tried but, as Im displaying the images of the array in a smooth movement, I can't use that, because it switchs the position from 9 to 0 in an instant.
But I took your advice of using and temp array! So I created a 3x1 temp array. When the last image of the regular array appears in the screen, I just put the temp array above y that image. When the last regular array image moves down, also does the 3 temp array images. When the last temp array image shows in the screen (that means that the regular array images are all out of the screen) I reset the timer, so the regular array images moves on top of the temp array images!
Thanks!! :)
Following your advice, I got this, but the draw calls are much more than excepted... 40 at the begging (ins$$anonymous$$d of the 15 expected) and 100 in the switch between the temp array and the regular array! If I move just one column, the draw calls peak doesn't affect the performance (I still get 58 fps) but if I uncomment the lines and I roll the 5 columns, the draw calls peak leaves me with a 5 fps performance!! I checked the draw calls and still get that stat... any idea?? This is my code: https://dl.dropboxusercontent.com/u/7059378/code.js
I put the code in this question, just for the drawcall, because the way of showing the image array is solved :)
http://answers.unity3d.com/questions/538011/too-many-drawtexture-calls.html
Your answer
Follow this Question
Related Questions
How Do I Add An Instantiated Object To An Array? 3 Answers
How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers
My array does not update when object is destroyed. How do I fix it? (java) 2 Answers
How do I use array's to spawn gameobjects(C#)? 3 Answers
Copy Array to a Stack 0 Answers