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
5
Question by 07Mr07 · Feb 09, 2014 at 11:16 AM · buttoneffectcooldown

Cooldown effect in a button

Hi! I'm having problems searching tutorials or information about that, I read the famous post of Eric5h5 link text but not exactly what I want to do. My idea is something like this

alt text

I'm a bit lost, some help would be awesome!

Thanks in advance ;)

cdbutton.png (24.4 kB)
Comment
Add comment · Show 4
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 whydoidoit · Feb 09, 2014 at 11:22 AM 0
Share

Yeah you need a transparent cutout shader that actual renders something transparent when not cutting out to use Eric's method in this case. I don't have the time right now to write that shader for you - but it is something that isn't too hard if you follow one of the shader tutorials.

avatar image whydoidoit · Feb 09, 2014 at 11:24 AM 0
Share

A "quick and dirty" method would be to draw "n" sprites and render the correct one (16 perhaps) or use NGUI which has a circular fill method on its widgets.

avatar image 07Mr07 · Feb 09, 2014 at 04:12 PM 0
Share

Seen this tutorial link text I get do that alt text attaching the image to a cube, and controlling from "Alpha Cutoff", but I dont know how do this in a gui.button and how control it from script.

If someone could help me, I'd really appreciate :)

4554.jpg (22.6 kB)
avatar image RealSoftGames · Aug 05, 2015 at 07:30 AM 0
Share

cheers bud this is exactly what i was looking for, and it slipped my $$anonymous$$d xD

4 Replies

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

Answer by Scribe · Feb 12, 2014 at 12:03 AM

EDIT: This method is obsolete! See the answer below for the updated solution.


This is probably not the most efficient method, but I (probably like many) have no idea about making shader code.

So a short description of how this works/what it does to help those trying to understand what's happening:

Firstly the texture set to the variable 'tex' has to be set to read/writable. To do that you need to select the texture -> set 'Texture Type' to advanced and tick the 'Read/Write Enabled' tick box. I would suggest also changing 'Format' to 'ARGB 32' else you may get some warnings in the console.

As this is not a particularly efficient process I have put it in its own function which is only called from update if the variable 'progress' has changed, this avoids unnecessary getting and setting of pixels. I would suggest you eventually try to avoid calling it as much as possibly and possibly put have it as an invoke repeat which is started when an ability is used and is cancelled when the progress reaches 1.

Also to boost efficiency, as the pixels of the textures are iterated through, make sure your textures are the smallest pixel size possible to begin with as a 16x16 texture needs to make 256 calculations compared to a 64x64 which would need 4096 calculations for the same function call (this will also save file memory)

I would suggest that for ever instance of the progress button you use 2 textures as I have done rather than 1 as this will avoid you overwriting the original texture data.




Code




 var tex : Texture2D;
 var color : Color;
 var progress : float = 0.0;
 private var oldProg : float = 0.0;
 private var progTex : Texture2D;
 
 function ProgressUpdate(progress : float, overlayColor : Color){
     var thisTex = new Texture2D(tex.width, tex.height);
     var centre = Vector2(Mathf.Ceil(thisTex.width/2), Mathf.Ceil(thisTex.height/2)); //find the centre pixel
     for(var y : int = 0; y < thisTex.height; y++){
         for(var x : int = 0; x < thisTex.width; x++){
             var angle = Mathf.Atan2(x-centre.x, y-centre.y)*Mathf.Rad2Deg; //find the angle between the centre and this pixel (between -180 and 180)
             if(angle < 0){
                 angle += 360; //change angles to go from 0 to 360
             }
             var pixColor = tex.GetPixel(x, y);
             if(angle <= progress*360.0){ //if the angle is less than the progress angle blend the overlay colour
                 pixColor = Color(
                     (pixColor.r*pixColor.a*(1-overlayColor.a))+(overlayColor.r*overlayColor.a),
                     (pixColor.g*pixColor.a*(1-overlayColor.a))+(overlayColor.g*overlayColor.a),            
                     (pixColor.b*pixColor.a*(1-overlayColor.a))+(overlayColor.b*overlayColor.a)            
                                 );
                 thisTex.SetPixel(x, y, pixColor);
             }else{
                 thisTex.SetPixel(x, y, pixColor);
             }
         }
     }
     thisTex.Apply(); //apply the cahnges we made to the texture
     return thisTex;
 }
 
 function Start(){
     progTex = ProgressUpdate(progress, color);
 }
 
 function Update(){
     if(oldProg != progress){
         progTex = ProgressUpdate(progress, color);
         oldProg = progress;
     }
 }
 
 function OnGUI(){
     var guiStyle : GUIStyle = new GUIStyle("button");
     guiStyle.padding = RectOffset(0, 0, 0, 0);
     GUI.Button(Rect(10, 10, 32, 32), progTex, guiStyle);
 }

Hopefully this helps answer your problem,

Scribe

P.S.

emember to give credit for the graphics used if they are not yours, I realise this might have just been an example and not from your own game, but in-case anyone is looking for the graphics they are by J. W. Bjerk

Comment
Add comment · Show 7 · 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 07Mr07 · Feb 12, 2014 at 04:07 PM 0
Share

Thanks for your answer and your time Scribe!! Good job man! Works awesomealt text Is possible to do this with 2 images (one darker) to try imitate the first image effect? Or other way to do more beautiful? Congrats for your job.

cdbut.png (15.3 kB)
avatar image Scribe · Feb 12, 2014 at 06:35 PM 0
Share

on line 17 of my code, changing angle <= progress*360.0 to angle >= progress*360.0 should mean the rotation goes the otherway. Then when you choose a colour set the alpha to be ~0.5 and it should have a similar effect to the first image you posted! Once you've got it working remember to mark your question as answered, so others know where to look!

avatar image 07Mr07 · Feb 13, 2014 at 02:22 PM 0
Share

Oh thats it!! I got what I want now, its perfect! Thank you so much! :D

avatar image Scribe · Feb 14, 2014 at 03:12 AM 0
Share

glad I could be of assistance! :)

avatar image 07Mr07 · Feb 15, 2014 at 12:00 PM 0
Share

Sorry for disturb you again, one last thing. I've done this: alt text

When I click, it do the effect. Im trying to do now: -if button cost 20 gold and I have more I want it shows without this shadow like this:

alt text

else, if I have less than 20 like first pic. I tried with your code, but I cant get it :/

1.png (12.9 kB)
2.png (16.5 kB)
Show more comments
avatar image
23
Wiki

Answer by antosdaniel · Sep 01, 2014 at 05:19 AM

I know this is old but google leads here :)

With a new UI in Unity 4.6 which is available in beta (for the time of writing) you can do this much, much easier.

  1. Create Image as a child for an icon, stretch it so it covers your icon.

  2. Add sprite with alpha.

  3. Set image type to filled.

  4. Set fill method to Radial 360.

  5. Play with fill origin, fill amount and clockwise.

If you change fill method, then you can differend cooldown types (vertical, horizontal etc.).

I hope I saved someone's time :)

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
avatar image
0

Answer by Bill_y_bob · Nov 16, 2018 at 09:08 AM

This is a shabby but working way:

 float timer;
 bool firstTimePressed;

 //set Cooldown Time in seconds here
 public float cooldownTime;
 
 void Start()
 {
 firstTimePressed = true;
 }
 
 void Update()
 {
 timer += Time.DeltaTime;
 }
 
 public void OnButtonPress()
 {
 if (firstTimePressed = true)
 {
  WhatButtonWillDo ();
 firstTimePressed = false;
 timer = 0;
 }
 if (firstTimePressed = false && timer >= cooldownTime)
 {
 WhatButtonWillDo ();
 timer = 0;
 }
 }
 
 void WhatButtonWillDo ()
 {
 // Enter button function here
 }



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
avatar image
0

Answer by tikali23 · Mar 02, 2021 at 08:21 AM

This post was the first to show up on google, so for the sake of completeness:

The easiest way to accomplish a cooldown effect is by combining the image settings from antosdaniel's post with a slider, like this.

You can then use the normal slider controls (maxValue, value) and the fill image will automatically fill in a circular shape.

alt text


cooldown-slider.png (121.2 kB)
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

28 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

Related Questions

PlayerPrefs for Mute/Unmute button 1 Answer

Cooldown effect in GUI 0 Answers

How to make a beating/pulsating button effect 1 Answer

How to detect a button being released? 1 Answer

[Unity 4.6] UI Button problem null reference exception [Js] 0 Answers


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