Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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
4
Question by Gilles_aerts · Jul 04, 2015 at 12:45 PM · buttonfunctionwhilefalsetrue

Run a function aslong as a button is pressed

Hi

I have a button in my canvas. I know how to make a button run a function whenever its pressed, but how do I make it run the function as long as I hold the button down.

To be more clear i'll give you an example :

I have a bool that needs to be turned on and off ( on when the button is pressed, and off if its not pressed)

e.g. :

public void WhileButtonIsPressed(){ myBool = true;

}

public void WhileButtonIsNotPressed(){ myBool = false;

}

long story short : I want my bool to be true if the button is held down, and false if its not.

Thanks in advance.

Comment
Add comment · Show 3
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 fafase · Jul 04, 2015 at 01:20 PM 0
Share

Have you given a thought on reading the Input documentation?

It is all there, Input.Get$$anonymous$$ey vs Input.Get$$anonymous$$eyUp

avatar image Gilles_aerts fafase · Sep 16, 2015 at 09:47 PM 0
Share

I know what you mean by getkey and getkey up , but unfortunately thats not the answer im looking for. See... if you press a button in an UI element it is a 1-frame action... but I need it to run the function attached to the button aslong as my mouse/ finger is pressing the UI button.

avatar image Dave-Carlile · Jul 04, 2015 at 01:28 PM 0
Share

I think they're asking how do do that with uGUI, as in are there events besides OnClick... maybe.

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by sonu124 · Sep 17, 2015 at 12:14 PM

this code will work..

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

public class ButtonPressed : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

 // Use this for initialization
 void Start () {
 
 }
 void Update()
 {
     if (!ispressed)
         return;
     // DO SOMETHING HERE
     Debug.Log ("Pressed");

 }
 bool ispressed = false;
 public void OnPointerDown(PointerEventData eventData)
 {
     ispressed = true;
 }
 
 public void OnPointerUp(PointerEventData eventData)
 {
     ispressed = false;
 }

}

Comment
Add comment · Show 3 · 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 Gumemura · Feb 07, 2020 at 07:00 AM 0
Share

Works like a charm. Thnks very much!

avatar image YehiOr · Dec 01, 2021 at 05:57 AM 0
Share

Hi, Excuse me, I'm new to unity. How do I use this code? I can't seem to select the OnPointerDown function on the UI button's On Click () options.

avatar image sacredgeometry YehiOr · Dec 01, 2021 at 06:38 AM 0
Share

HI there, welcome:

The GUI api has changed a lot in the last 6 years

https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerDown.html

The documentation says that you need to implement the IPointerDownHandler interface which will be part of what you need to do. Suffice to say you probably want to look at more modern solutions to your problem or ask a new question instead of digging up 6 year old+ posts.

avatar image
0

Answer by Hiten2012 · Jul 04, 2015 at 01:43 PM

Its possible by many different ways but choose any suitable for your case. For UI button than you can refer this http://answers.unity3d.com/questions/857801/46-ui-dynamic-button-event-system-pointerenter-poi.html

If UI it not mandatory in your case You can also do it with GUI button than use Repeat Button http://docs.unity3d.com/ScriptReference/GUI.RepeatButton.html

In case of touch device input http://docs.unity3d.com/ScriptReference/TouchPhase.html

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 holliebuckets · Sep 16, 2015 at 10:30 PM

@Gilles_aerts This thread is a great resource too! ---> http://forum.unity3d.com/threads/touch-and-hold-a-button-on-new-ui.266065/

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

iOS deactivating a script deactivates the gameobject 1 Answer

How can I activate a scripts variable from another gameobject? 1 Answer

Probobly easy fix , int dosnt change issue. 1 Answer

I can't figure out how to enable / disable a trigger. 2 Answers

The public bool will not change unless i set it 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