- Home /
About removing the backlight of a button after a set of time
Hello, I'm working on an on screen keyboard. Currently my problem is that I want to remove the backlight of a button that's been selected after 1 or 2 second(s). I've tried the "Destroy" function and "Yield", but neither works well. I know it's my own problem but can somebody give me some hints? Really no ideas now...... Thanks very very very very much~!
Answer by Owen-Reynolds · May 28, 2013 at 02:50 PM
Should the highlight on button 1 immediately snap off if someone quickly presses button 2? In other words, will there only ever be one highlight? Or will quickly pressing buttons 1,2,3,4 give me a delayed effect "wink out 1", "wink out 2" ... ? Are the buttons all the same size?
If only 1 highlight ever and all same size, can make a single traveling highlight. A cheap way to wink out is to store "lastMovedtime," reset it to current time at each move, and check in Update if the lastMovedTime was over 2 seconds ago. Instead of destroying it, can set it inactive, or disable the renderer (or teleport it to -999.)
If you need a highlight for each button, you could also use that trick (every single button would have its own lastClickedTime and a script checking it.) Or, if you go the yield route, look up Coroutines. Yields can only be used in them. They let you say fire&forget things like "turn this off in 2 seconds." But, you'll have to worry about someone clicking a button again after 1.8 seconds (method#1 will just bump up lastClickedTime, so will work.)
First, thanks anyway. $$anonymous$$y situation is like the first one, but still I'm a little confused... Can you explain more to me, please? THX!~
What do you have so far? Break it into parts and test each one.
How to make a highlight (plane w/trans texture, or new button texture.) How to move or turn on/off things. How a script can "know about" other objects. Using Time.time
as a stopwatch.
Actually, I use GUI.DrawTexture to make a blue background as a backlight. I also use if (i == selectedButton && Input.mousePosition.x > Screen.width/2 - OnScreen$$anonymous$$eyboard.maxWidth2/2 && Input.mousePosition.x < Screen.width/2 + OnScreen$$anonymous$$eyboard.maxWidth2/2 && Input.mousePosition.y < OnScreen$$anonymous$$eyboard.maxHeight2) to judge if the mouse is on the button or not.
DrawTexture is funny -- it's not persistent, so you don't turn it off. Ins$$anonymous$$d, you just stop saying to draw it. Typically with if(drawHighlight) GUI.DrawTexture...
. Then drawHighlight
controls whether you show it this frame, or not.
But again, "How do I not draw a GUI-item" is a common topic.
I see... Do you mean I have to "draw" it again so I can cover the previous one and get a new one?
Answer by llSalvationll · May 28, 2013 at 04:07 PM
Can't you just use Invoke? Setup a function to have invoked after a few seconds that will turn off the lit key.
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html
Thanks, is works like "Yield". But "Invoke" can only count the time immediately after running the project (is that right?), while "Yield" seems more "free". I'll try the one with "Yield" again.
Yield requires a co-routine, invoke does not. In the logic that handles your button press use something like this: invoke("removeBackLight", 2);. Then in your removeBackLight script (or whatever you call it) disable the light. If a new/different button is pressed, change the back light to the new key, cancelInvoke and then re-invoke to start the timer over. There's a million ways to get the effect you want, so use whatever you find most appealing.
That's really inspiring! I'll try it immediately, THX again!
Yes, Invoke is a coroutine shortcut. It has a single built-in yield.
Your answer
Follow this Question
Related Questions
Instantiate an object as soon as another object is destroyed 2 Answers
Yeild and WaitForSeconds and Instantiate 1 Answer
Destroy then Clone Help please? 2 Answers
Obj calls function, on another obj and get's destroyed. Function stops 1 Answer
Why Is yield return new WaitForSeconds() not working 2 Answers