• 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
1
Question by straydogstrut · Apr 02, 2010 at 06:25 PM · guitextureframeratefor-loop

Animate a GUITexture

I have a GUITexture and I want to change its texture to iterate through an array of textures over a given period of time. I found the Texture Swap Animator on the Unify wiki which iterates through the images a set number of frames a second using the following:

[Edits made re. Eric's answer, but still problems, see comments]

var myTextures : Texture2D[]; var framesPerSecond : float = 10.0; var dripPlaying : boolean = false;

 function Start(){

  guiTexture.enabled = false;

 }

  function Update () {

 Debug.Log(guiTexture.texture.name);

     if(dripPlaying == true){

         var i : int = Time.time * framesPerSecond;
         i = i % myTextures.length;

         guiTexture.texture = myTextures[i];

         guiTexture.enabled = true;

         if(i == myTextures.length-1){
             dripPlaying = false;
             guiTexture.enabled = false;
         }
     }

 }


To clarify: I have a simple sphere with an animation that moves it along the y-axis (dropping from a shower head). I created this animation in the animation editor. On the frame in the animation where it would be hitting the floor, there is an animation event which calls the following function. The dripPlaying boolean that is changed by this function should start the animated GUITexture from the above script playing.

var audioVolume = 1.0; var collisionSoundEffect : AudioClip;

function playSound(){ //Debug.Log("Animation event called"); audio.volume = audioVolume; audio.clip = collisionSoundEffect; audio.Play();

 otherscript = GameObject.Find("dripGUI").GetComponent("guiAnim");
 otherscript.dripPlaying = true;
 //GameObject.Find("dripGUI").GetComponent(GUITexture).enabled = true;

}

@script RequireComponent(AudioSource)

But i'm still not seeing every frame. I tried to confirm using Debug.Log() but it doesn't seem to change, despite being in the Update() function. My other question can be found here.

If someone could shed some light on this, i'd appreciate it.

Thanks.

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 yoyo · Apr 21, 2010 at 04:58 AM 0
Share

Are you sure use of Time.time isn't the problem? To me it seems like you'll start at a random interval into the animation depending what the current time is when you begin playing.

It would be better to use a floating point frame count, starting at 0.0 and adding Time.deltaTime * framesPerSecond, then truncate the float to an int with $$anonymous$$athf.FloorToInt to get your integer frame index.

3 Replies

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

Answer by Eric5h5 · Apr 02, 2010 at 08:32 PM

OK, I see now that you just want the frame animation to play once, yes? In this case it would be simpler to use a coroutine function for this, since Update is intended for things that run all the time:

var framesPerSecond = 10.0; var myTextures : Texture2D[];

function PlayAnim () { var waitTime = 1.0/framesPerSecond; for (i = 0; i < myTextures.Length; i++) { guiTexture.texture = myTextures[i]; yield WaitForSeconds(waitTime); } guiTexture.enabled = false; }

Then change

otherscript.dripPlaying = true;

to

otherscript.PlayAnim();
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 straydogstrut · Apr 02, 2010 at 08:40 PM 0
Share

Hi Eric, that works perfectly! Although I got your last version working, I found it was a bit unpredictable depending on the fps I used (I couldn't see the connection between the value and what happened). This method is much better, thank you again=)

avatar image
0
Best Answer

Answer by Eric5h5 · Apr 02, 2010 at 07:02 PM

With your for loop, you're looping through all the textures in one frame, so you will never see any but the last. You can't use yield in Update, because it always runs every frame. Since you already have a variable to start or stop the animation, you could use that to stop the animation when the frame index gets to the end:

if(dripPlaying == true){
    var i : int = Time.time * framesPerSecond;
    i = i % myTextures.length;
    guiTexture.texture = myTextures[i];
    if (i == myTextures.length-1) dripPlaying = false;
}
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 straydogstrut · Apr 02, 2010 at 08:15 PM 0
Share

Hi Eric, at first I thought that was working, with a $$anonymous$$or glitch where the last image would appear at the start of every loop. So I moved the code to show the GUItexture from the other script (it's called on an animation event) into this one, but it doesn't seem quite right. I don't think i'm seeing every frame. When I set the framesPerSecond to 1 I see a couple of frames, but at something like 10 it only shows 1 frame (there should be 4). I'm thinking the next animation event happens before it has time to finish but setting object's anim to once makes no difference. Updated my question.

avatar image straydogstrut · Apr 02, 2010 at 08:26 PM 0
Share

Sorry, scratch that. I increased the framesPerSecond to 20 and it works fine, playing on each 'drip'. I thought using a lower fps would help me see the changes but it must have just got me confused. I've confirmed that all images are shown using print(guitexture.texture.name) so no worries. Just the sound problem left. Thanks

avatar image
0

Answer by Venryx · Mar 03, 2014 at 08:03 PM

You could also try a script like the following, which makes use of Mono's System.Drawing library: http://wiki.unity3d.com/index.php/AnimatedGifDrawer

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

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

(For Loop) Converting Gui Button to GuiText & GuiTexture 1 Answer

I Need a script for a guitexture enableing a spotlight and play a sound when pressed 1 Answer

Detect Mouse Click in a GUITexture 1 Answer

Scrolling gui texture up and down, Android 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