ViktorijaSml / UIFlow-Virtual

Blockily coding simulator for M5stickC Plus microcontroller in Unity, C#
https://roboaquaria-project.eu
Apache License 2.0
0 stars 0 forks source link

Events functions #7

Closed ViktorijaSml closed 8 months ago

ViktorijaSml commented 10 months ago

image

Antonio-Gorisek commented 10 months ago

Evo vam skripta za detekciju klikova (Double click / long press / wasPressed / wasRelesed) Također sadži UnityEvent što znači da možete u sve to dodati koje klase, objekti itd će se pokretati kad se pokrene neki od tih evenata.

NPR: Screenshot_42

public UnityEvent wasPressedEvent, wasReleasedEvent, longPressEvent, wasDoublePressedEvent;
private bool isPressed = false;
private float pressTime = 0f, longPressDuration = 1f, doublePressDelay = 0.3f;

void Start()
{
    Button button = GetComponent<Button>();
    if (button != null)
    {
        button.onClick.AddListener(OnButtonClicked);
    }
    else
    {
        Debug.LogError("Button component not found on GameObject.");
    }
}

void OnButtonClicked()
{
    if (!isPressed)
    {
        isPressed = true;
        pressTime = Time.time;
        wasPressedEvent.Invoke();
    }
    else if (Time.time - pressTime < doublePressDelay)
    {
        wasDoublePressedEvent.Invoke();
        isPressed = false;
    }
}

void Update()
{
    if (isPressed)
    {
        // Do something
    }
}

void OnButtonReleased()
{
    isPressed = false;
    wasReleasedEvent.Invoke();

    if (Time.time - pressTime > longPressDuration)
    {
        longPressEvent.Invoke();
    }
}