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 mmangual_83 · Dec 10, 2013 at 03:06 PM · c#guispritesloop

How to loop an array of 2d sprites

I have been working on this code for school that handles animating 2D sprites on the 3D screen, just like a GUI. But I only have one sprite showing on the screen and I want to be able to animate it with the rest of the pictures I have (about 10). Here is the basic class I am working on:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class AnimateSprites : MonoBehaviour {
 
     public Texture2D mSprite;
 
     public Rect r_Sprite;
     
     void OnGUI()
     {
         GUI.DrawTexture(mSprite,r_Sprite);
     }
 }

How can I get this to loop the rest of my images? I know I have to go about making my sprite texture into arrays and also have a method which loops them. Can anyone help direct me on what my step(s) should be? Thank you in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by StormSabotage · Dec 10, 2013 at 03:19 PM

Try this:

 public Texture2D[] mSprites;
 private Texture2D currentSprite;
 private int counter;
 public float switchTime = 0.5f;
     
 public Rect r_Sprite;
 
 void Start(){
     counter = 0;
     StartCoroutine("SwitchSprite");
 }
 
 void OnGUI(){
     GUI.DrawTexture(r_Sprite,currentSprite);
 }
 
 private IEnumerator SwitchSprite(){
     currentSprite = mSprites[counter];
 
     if(counter < mSprites.Length){
         counter++;
     }else{
         counter = 0;
     }
 
     yield return new WaitForSeconds(switchTime);
     StartCoroutine("SwitchSprite");
 }

Just fill mSprites array with your sprites and set switchTime how you want ;)

Comment
Add comment · Show 4 · 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 mmangual_83 · Dec 10, 2013 at 03:29 PM 0
Share

IT WOR$$anonymous$$S!!!

avatar image StormSabotage · Dec 10, 2013 at 03:29 PM 0
Share

btw - it's C#, you can do same in JS if you want

avatar image Spinnernicholas · Dec 10, 2013 at 05:01 PM 1
Share

You shouldn't use OnGUI for this look into Sprites.

avatar image taguados · Oct 18, 2017 at 10:59 AM 0
Share

Thanks, it worked for me too!

The only problem is that it changes sprites way too slow, and it doesn't matter the time I set it seems it always loops at the sime velocity.

I've used SpriteRenderer ins$$anonymous$$d of OnGUI, don't know if that is the problem.

Thanks again!

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

19 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

Related Questions

HorizontalScrollbar not appearing in GUILayout.BeginScrollview C# 3 Answers

Unity editor works but the built game doesnt. 0 Answers

How to remove a GUI element when button is pressed C# 1 Answer

Getting Debug log errors when I load my scenes 2 Answers

Multiple Cars not working 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