Author Topic: Check if button is pressed every frame  (Read 2568 times)

bumblebee

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 6
    • View Profile
Check if button is pressed every frame
« on: April 12, 2014, 07:22:03 AM »
I need to check whether the specific widget is pressed or not each frame. What is the best way of doing this? Seems like a trivial task but I cannot figure out a perfect solution.

What i have now is this code, but the problem is that the FLY button is not always released when multiple different buttons are pressed at the same time if I use Unity Remote. It seems to work OK on device. Is my solution OK then?

using UnityEngine;
using System.Collections;

public class FlyButton_NGUI : MonoBehaviour
{
    public bool matchToInputSettings;
    public string buttonName;
    private CrossPlatformInput.VirtualButton _flyVirtualButton;

    private bool _isFlyPressed;

    void OnEnable()
    {
        _flyVirtualButton = new CrossPlatformInput.VirtualButton(buttonName, matchToInputSettings);
    }

    void OnDisable()
    {
        _flyVirtualButton.Remove();
    }

    void OnPress(bool isDown)
    {
        print("OnPress : " + isDown);
        _isFlyPressed = isDown;
    }

    void Update()
    {
        if (_isFlyPressed)
        {
            _flyVirtualButton.Pressed();
        } else {
            _flyVirtualButton.Released();
        }
    }
}
« Last Edit: April 12, 2014, 08:04:22 AM by bumblebee »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Check if button is pressed every frame
« Reply #1 on: April 13, 2014, 12:38:11 AM »
UICamera.IsPressed(targetObject) tells you whether something is pressed. Just check it in Update, no need to set anything.