Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: bumblebee on April 12, 2014, 07:22:03 AM

Title: Check if button is pressed every frame
Post by: bumblebee 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();
        }
    }
}
Title: Re: Check if button is pressed every frame
Post by: ArenMook 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.