Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bulalord609 · Feb 16, 2017 at 09:30 AM · guitexturechanging

Changing GUI texture every 3 seconds?

How do you cycle through images in a panel? When my GO stops an Emoji icon appears on top of it and I would like it to change every other second until it gets moving again. Any tips?

 public class Emotion : MonoBehaviour {
     public GameObject Sphere;
     public Canvas Emoji;
     private bool hide = false;
     private bool show = true;
     private bool  Tigil = false;
     float CarSpeed;
     Stop stop;
 
     void PopUp()
     {
         if (stop.SPEED == 0) {
             Emoji.GetComponent<Canvas> ().enabled = show;
             Tigil = true;
         } else if (Tigil) 
         {
             Emoji.GetComponent<Canvas> ().enabled = hide;
             Tigil = false;
         }    
     }
     // Use this for initialization
     void Start () {
         stop = Sphere.GetComponent<Stop>();
         Emoji.GetComponent<Canvas> ().enabled = hide;
 
     }
     
     // Update is called once per frame
     void Update () {
 
         PopUp ();
 
     }
 }




Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ShyRec · Feb 16, 2017 at 09:58 AM

If I understand you right, you need to create Image like so alt text

and then write something like this (I write this in browser, so it may contains a bunch of mistakes)

 public class Emotion : MonoBehaviour {
      public Image image; // Image that we just created
      public Sprite[] sprites; // your sprites to cycle 
      public int spriteIndex = 0;
      public float timer = 3;
      public GameObject Sphere;
      public Canvas Emoji;
      private bool hide = false;
      private bool show = true;
      private bool  Tigil = false;
      float CarSpeed;
      Stop stop;
  
      void PopUp()
      {
          if (stop.SPEED == 0) {
              Emoji.GetComponent<Canvas> ().enabled = show;
              Tigil = true;
              InvokeRepeat("CycleThroughSprites", timer, timer);
          } else if (Tigil) 
          {
              Emoji.GetComponent<Canvas> ().enabled = hide;
              Tigil = false;
              CancelInvoke("CycleThroughSprites");
          }    
      }
      // Use this for initialization
      void Start () {
          stop = Sphere.GetComponent<Stop>();
          Emoji.GetComponent<Canvas> ().enabled = hide;
  
      }
 
      void CycleThroughSprites()
      {
            image.sprite = sprites[currentIndex];
            currentIndex++;
            currentIdex %= sprites.Length;
      }
      
      // Update is called once per frame
      void Update () {
  
          PopUp ();
  
      }
  }

Hope it helps Cheers


снимок-экрана-2017-02-16-в-133818.png (146.0 kB)
Comment
Add comment · Show 5 · 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 Bulalord609 · Feb 16, 2017 at 10:24 AM 0
Share

What image do I put in "public Image image;"? The first image to be show? I'm fairly new to unity so still getting the hang of it.

avatar image ShyRec Bulalord609 · Feb 16, 2017 at 10:31 AM 0
Share

In screenshot I added a component called "Image". Do the same thing. This Image component you need to assign to this variable is Inspector. Your pictures, that you want to be cycled, you need to add to sprite[] array in Inspector. Basically any amount of pictures. Also, you need to add "using UnityEngine.UI" to your script before class declaration.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Emotion : $$anonymous$$onoBehaviour {
       public Image image; // Image that we just created
       public Sprite[] sprites; // your sprites to cycle 
       public int spriteIndex = 0;
 ...............
avatar image Bulalord609 · Feb 16, 2017 at 10:52 AM 0
Share

I doesn't appear to work sadly. I think it's because nothing is returning it? I was thinking of using resources.LoadAll("folder");

avatar image Bulalord609 · Feb 16, 2017 at 10:59 AM 0
Share

The compiler keeps saying "Trying to invoke method: TRY.CycleThroughSprites couldnt be called

avatar image ShyRec Bulalord609 · Feb 16, 2017 at 11:11 AM 0
Share

Okay, I finally open Unity :) Here's example of working code. Press "space" button to show or hide canvas

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Emotion : $$anonymous$$onoBehaviour {
       public Image image; // Image that we just created
       public Sprite[] sprites; // your sprites to cycle 
       public int spriteIndex = 0;
       public float timer = 3;
       public GameObject Sphere;
       public Canvas Emoji;
       private bool hide = false;
       private bool show = false;
       private bool  Tigil = false;
       float CarSpeed;
   
       void PopUp()
       {  
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             show = !show;
             if (show)
             {
                 InvokeRepeating("CycleThroughSprites", timer, timer);
             }
             else
             {
                 CancelInvoke("CycleThroughSprites");
             }
             Emoji.GetComponent<Canvas> ().enabled = show;
         }
       }
       // Use this for initialization
       void Start () {
           //stop = Sphere.GetComponent<Stop>();
           Emoji.GetComponent<Canvas> ().enabled = hide;
   
       }
  
       void CycleThroughSprites()
       {
             image.sprite = sprites[spriteIndex];
             spriteIndex++;
             spriteIndex %= sprites.Length;
       }
       
       // Update is called once per frame
       void Update () {
   
           PopUp ();
   
       }
   }

This is setup http://i.imgur.com/bJw4$$anonymous$$wc.png

Just add to sprite array images you want to show

avatar image
0

Answer by HarshadK · Feb 16, 2017 at 09:54 AM

You can use a float to count the time remaining to show next image and then when time remaining reaches zero, show next image. This needs to be done only when your character is at rest. Something like:

 // Set your images to in this array.
 public Sprite[] images;
 private int m_CurrentImageIndex;
 private float m_TimeRemaingNextImage = 0f;
 
 // Cache your Image component in this in Start() method or assign it from Inspector.
 private Image image;
 
 void PopUp()
 {
     //-- Your current code goes here.
 
     if(Tigil) {
         m_TimeRemainingNextImage -= Time.deltaTime;
         if(m_TimeRemainingNextImage <= 0f) {
             image.sprite = images[GetImageIndex()];
             m_TimeRemainingNextImage = 3f;
         }
     }
 }
 
 private int GetImageIndex()
 {
     m_CurrentImageIndex += 1;
     return (m_CurrentImageIndex >= images.length) ? 0 : m_CurrentImageIndex;
 }

Comment
Add comment · 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

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

66 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 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 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

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Disable a object 1 Answer

iPhone GUI Texture (TouchPad prefab) not working 2 Answers

ReadPixels requires an offset while in the Editor? 2 Answers

Renderer Texture on GUITexture 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