- Home /
Cycle through images on click c#
I have made some buttons for a character creator. When you click for example the 'Head' button, I want a head to appear on a fixed position on the screen. When I click the 'Head' button again, I want a different head to appear on the exact same position as the first one and so on. I have tried some things now but I am completely lost.
hey
something like this?
int TexIterator;
Texture CycleTexture(Texture[] tex){
if(TexIterator+1 < tex.Length)TexIterator++;
else TexIterator = 0;
return tex[TexIterator];
}
call this function everytime you click on the button to get the next texture (I am assu$$anonymous$$g you have your textures preloaded on an array)
Answer by pumpkinszwan · Jun 02, 2014 at 12:53 PM
I would just create an object with a head sprite and change the sprite image when the button is clicked.
You change a sprite like this:
void SetHeadSprite (Transform head)
{
Sprite newSprite = Resources.Load("Textures/heads/head_01", typeof(Sprite)) as Sprite;
head.GetComponent<SpriteRenderer>().sprite = newSprite;
}
You can easily jig this to pass a sprite name (as a string) into the method from wherever you handle your button clicks.
I am getting the error: No overload for method SetHeadSprite' takes
0' arguments. Removing the argument gives me the error: The name `head' does not exist in the current context
Answer by Nerevar · Jun 02, 2014 at 03:04 PM
Ok as you seem quite a beginner in coding, I post you a simple example algorithm you can adapt to your project
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public Texture[] heads;
Texture current = null;
int TexIterator;
Texture CycleTexture(Texture[] tex){
if(TexIterator+1 < tex.Length)TexIterator++;
else TexIterator = 0;
return tex[TexIterator];
}
void OnGUI(){
if(GUILayout.Button("Next Head"))current = CycleTexture(heads);
if(current)GUILayout.Label(current);
}
}
make new scene, Attach this script to camera , make the size you want for the texture array and assign the textures with the heads from the inspector.
If you have any questions about how to adapt it to your project maybe show us some of your code
see you
Thanks for the response! The array is working great, but could you explain this part a little more? I can't really seem to connect it to my button.
void OnGUI(){
if(GUILayout.Button("Next Head"))current = CycleTexture(heads);
if(current)GUILayout.Label(current);
}
The code for my buttons looks like this:
void OnGUI ()
{
for (int i = 0; i < parts.Length; ++i) {
if (GUI.Button (new Rect (x - 35, i * (height + 97), width + 300, height + 90), parts [i]))
ProcessLeftInput (i);
}
}
And then I want to cycle through the heads when I click on the first case button:
void ProcessLeftInput (int i)
{
switch (i) {
case 0:
Debug.Log ("Button 1");
monsterhandler.headRenderer.sprite = monsterhandler.beerhoofd;
break;
case 1:
Debug.Log ("Button 2");
monsterhandler.headRenderer.sprite = monsterhandler.haaihoofd;
break;
}
}
About this part:
if(current)GUILayout.Label(current);
It just displays the texture but that would not be the way you want to use it.
void OnGUI ()
{
for (int i = 0; i < parts.Length; ++i) {
if (GUI.Button (new Rect (x - 35, i * (height + 97), width + 300, height + 90), parts [i]))
ProcessLeftInput (i);
}
// if(current)GUILayout.Label(current);
}
and
void ProcessLeftInput (int i)
{
switch (i) {
case 0:
Debug.Log ("Button 1");
current = CycleTexture(heads);
monsterhandler.headRenderer.sprite = monsterhandler.beerhoofd;
break;
case 1:
Debug.Log ("Button 2");
monsterhandler.headRenderer.sprite = monsterhandler.haaihoofd;
break;
}
}
By the way, what monsterhandler.beerhoofd and monsterhandler.headRenderer.sprite refers to? may be that line will do what you want:
monsterhandler.headRenderer.sprite = current;
The variable "current" contains the texture of an image, you can then assign "current" to the dedicated sprite(you can replace all the occurence of Texture type by Texture2D in the CycleTexture() function if you need to).
you still need to have this part of code somewhere in your script though.
public Texture[] heads;
Texture current = null;
int TexIterator;
Texture CycleTexture(Texture[] tex){
if(TexIterator+1 < tex.Length)TexIterator++;
else TexIterator = 0;
return tex[TexIterator];
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Comparing Textures on 2 Objects 1 Answer