• 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 shubhamshetkar25 · Apr 09, 2018 at 10:17 PM · uiguibuttonshootingfire

How to make UI Button Fire Continuously ?

how can I make UI Button Fire Continuously when clicked and hold ?

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

3 Replies

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

Answer by davidcox70 · Apr 09, 2018 at 10:32 PM

Have a look at InvokeRepeating and Cancel Invoke.

Where you would normally handle the button press, use something like;

 InvokeRepeating("FireMe",0, 0.1f);


This will continuously trigger a function called "FireMe" every one tenth of a second. Of course, at some point you will want to stop firing! So test when the user has stopped pressing the fire button (maybe OnMouseUp or Input.GetMouseButton(0)==false if the mouse is the fire button), and execute;

 CancelInvoke("FireMe");

Hope that helps.

DC

  • edit. Detecting a UI button press isn't as simple as detecting a "click" (which is a press down followed by a release.). So just in case, this is how you detect it with a script attached to the button itself;

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;

         public class myAutoFire : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
         
         
             public void OnPointerDown(PointerEventData eventdata){
                 InvokeRepeating ("FireMe", 0, 0.1f);
             }
         
             public void OnPointerUp(PointerEventData eventdata){
                 CancelInvoke ("FireMe");
             }
         
             void FireMe(){
                 Debug.Log ("firing"+Time.time);
             }
         }
 
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 shubhamshetkar25 · Apr 10, 2018 at 12:11 AM 0
Share

and which function should i select in OnClick section of my button in inspector ?

my script is located on other gameobject

avatar image
1

Answer by suleymanbucuk · Apr 30, 2020 at 08:33 PM

There is my solution for the situation; When you press a UI Button continuously, shooting or another function is called repeatedly:

Create a button in UI menu, remove or disable Button script from that button. Create a new sscript (my script's name is LongButtonPress). And implement the code below for shoot repeatedly when you press and hold the button.

using UnityEngine; using UnityEngine.EventSystems;

public class LongButtonPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { public bool isPressed = false;

 public void OnPointerDown(PointerEventData eventData)
 {
     isPressed = true;
 }

 public void OnPointerUp(PointerEventData eventData)
 {
     isPressed = false;
 }

 private void Update()
 {
     if(isPressed)
     {
         // implement the code here whatever you want to be when you press the button.
     }
 }

}

This code sets isPressed boolean true when you press the button, and in Update method if isPressed is true, applies the code in if statement.

Works pretty well for my game. Be care when you copy the code, some lines are not shown in code block in my comment, such as use UnityEngine etc. They are upperside of code block shown by forum.

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 LicketyCut · Jan 15, 2021 at 01:13 AM

Here is a Class that I wrote that does just what you are looking for.

Attach it to the Button GameObject and assign the same OnClick event to the ButtonRepeater class that you attach to the Button Class.

If you don't need a delay between clicks then leave delay at 0.

 using System.Collections;
 using UnityEngine;

 using UnityEngine.EventSystems;
 using static UnityEngine.UI.Button;
 
 public class ButtonRepeater : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
 {
     public float delay;
     public ButtonClickedEvent onClick;

     bool pressed;
 
     public void OnPointerDown(PointerEventData eventdata)
     {
         pressed = true;
         StartCoroutine(Repeat());
     }
 
     public void OnPointerUp(PointerEventData eventdata)
     {
         StopAllCoroutines();
         pressed = false;
     }
 
     IEnumerator Repeat()
     {
         yield return new WaitForSeconds(delay);
         while (pressed)
         {
             onClick?.Invoke();
             yield return new WaitForSeconds(delay);
         }
         
     }
 }
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 gralbayrak · Apr 09 at 08:19 PM 0
Share

GREAT JOB..

avatar image LicketyCut gralbayrak · Apr 09 at 09:16 PM 0
Share

Thanks. Glad I could help. If you would up vote my answer it will gain visibility to help others too.

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

140 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 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 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 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 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

Find GUI Button and Assign Text 1 Answer

Trying to put a 3D object above GUI for Hololens 1 Answer

How to have correct color on imGUI Buttons (as dynamic textures) avoiding multiply effect ? 1 Answer

UI: Mouse events not working 1 Answer

Making an object rotate towards touch 3 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