Author Topic: UIButton message while button is pressed  (Read 5509 times)

HannesB

  • Guest
UIButton message while button is pressed
« on: November 05, 2013, 04:00:40 PM »
hi,

At the moment i am creating my first game with unity and NGUI, where a player can control a ball.
In the Update Method i am using:

  1. if (Input.GetKey(KeyCode.LeftArrow))
  2.    MoveLeft();
  3.  
To move the player object, while the button is pressed. This works the way i want.

I placed a UIButton on the screen and wanted to do the same thing, while the button is pressed.

UIButton offers a "OnClick" event but what i need is some kind of "OnPress" or "WhilePressed".
As long as the button is pressed (touch input) i want to move the player => exactly the same thing that "Input.GetKey(..)" does.

How can i call MoveLeft(), while the UIButton is pressed?

thx for your help,
Hannes

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UIButton message while button is pressed
« Reply #1 on: November 05, 2013, 04:02:46 PM »
OnPress(bool pressed) gives you what you need.

OnPress(true) is called when you press down, OnPress(false) is called when you release.

HannesB

  • Guest
Re: UIButton message while button is pressed
« Reply #2 on: November 05, 2013, 04:59:09 PM »
Thanks!

Fianally i was able to solive it, i am an experienced C# developer but quite new in unity.

This is what it did (maybe it helps someone else):

I created a 'ButtonPressedBehaviour' script and attached it to the button.
The script defines a global "pressedState" variable.

  1. public class ButtonPressedBehaviour : MonoBehaviour
  2. {
  3.         public bool pressedState;
  4.        
  5.         // Use this for initialization
  6.         void Start ()
  7.         {
  8.                 pressedState = false;
  9.         }
  10.        
  11.         // Update is called once per frame
  12.         void Update ()
  13.         {
  14.         }
  15.        
  16.         void OnPress(bool pressed)
  17.         {
  18.                 if (pressed)
  19.                 {
  20.                         Debug.Log("Now is pressed");
  21.                         pressedState = true;
  22.                 }                      
  23.                 else
  24.                 {
  25.                         pressedState = false;
  26.                         Debug.Log("Now is released");
  27.                 }                      
  28.         }
  29. }
  30.  

The "magic" happens here => The OnPress function is called automatically, when the script is attached to the UIButton.
How is this done internally?
Does UIButton (or a base class of it) use reflection and search for attached scripts that define an "OnPressed" method?

In my playerControler script (that controlls the movement of the player) i added two variables for the two UIButtons.
I use "GetComponent<ButtonPressedBehaviour>" to get the script from the assigned UIButtons and depending on the value of "pressedState" i control the movement of the player.

thx
Hannes

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UIButton message while button is pressed
« Reply #3 on: November 08, 2013, 04:01:09 AM »
OnPress is called by the UICamera on the gameobject whose collider it hits with the raycast it makes when you touch the screen.

Look inside UICamera.cs to see other methods that gets called.