Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darkskxcher · Jun 02, 2014 at 12:22 PM · c#texturesimages

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerevar · Jun 02, 2014 at 12:37 PM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darkskxcher · Jun 02, 2014 at 02:05 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darkskxcher · Jun 03, 2014 at 04:40 PM 0
Share

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;
                 }
         }
avatar image Nerevar · Jun 04, 2014 at 08:51 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Comparing Textures on 2 Objects 1 Answer

Renderer on object disabled after level reload 1 Answer

iPhone- import and use images from device 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges