- Home /
Make a "Blinking/Moving/Animating" GUI
Hi, what I mean by "blinking" is that I want that a GUI button appears on the screen showing the spacebar being pressed and unpressed, showing that the player has to tap the spacebar very fast. How could I make it so that a GUI change itself with other so that an "animation" occurs?
Call a function that toggles a boolean variable after a certain amount of seconds and display the GUI depending on the variable state. e.g.:
if(displayGUI)
{
GUI.Button(....);
}
You can use InvokeRepeating to call a function at a set interval
Answer by Professor Snake · Mar 29, 2013 at 06:35 AM
You can use a MovieTexture in a GUI.DrawTexture function and call myTexture.Play() to make it play. Alternatively, you can have an array with all the keyframes and use something like this
var keyframePointer:int=0;
var delay:float=0.1;
var timePointer:float=0;
var myKeyframes:Texture2D[];
function OnGUI(){
if(showTexture){
GUI.DrawTexture(myRect,myKeyframes[keyframePointer];
if(timePointer<Time.time){
keyframePointer++;
timePointer=Time.time+delay;
}
}
}
I apologise for not being able to format the code properly. Doing so on mobile is a pain.
Thanks for the quick response, but in a part of the code you worte %, is i used for something or is it a mistake?
It is the operator for the remainder of the division (although now that you mention it, $$anonymous$$athf.Floor(Time.time-startTime+1) might be better than just (Time.time-startTime)). The idea behind it is this: We want to have a number that keeps increasing until it reaches array.length-1, after which point it should return to 0. Let's assume that your array has 5 keyframes, and you call the ShowTexture function when Time.time=10 (whick makes startTime=10). At that time, (Time.time-startTime+1) will have a value of 1, which makes the remainder of the division 1/5 be 1. In the next second, it will be 2 etc etc. When Time.time reaches 14, (Time.time-startTime+1) will have a value of 5. 5%5 equals 0. So, (Time.time-startTime+1) will go up to 5 and return to 0 every 5 frames. The +1 there is added to prevent a division with 0.
Of course, this is a somewhat complex way of incrementing the value. Here is a more simple way that also has a delay variable:
var keyframePointer:int=0;
var delay:float=0.1;
var timePointer:float=0;
var my$$anonymous$$eyframes:Tezture2D[];
function OnGUI(){
if(showTexture){
GUI.DrawTexture(myRect,my$$anonymous$$eyframes[keyframePointer];
if(timePointer<Time.time){
keyframePointer++;
timePointer=Time.time+delay;
}
}
}
Edit: Updated my answer with this piece of code ins$$anonymous$$d of the other one.
Thanks for all the time that you put into explaining this to me, I already tested it and it works! I really appreciate that you explained every bit and in a very detailed way. Thanks once again and have a nice day.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095305im_/https://answers.unity.com/themes/thub/images/avi.jpg)