Arcant
August 13, 2020, 9:05pm
1
Hi Everyone! i am looking for an elegant way to display a popup or something like that after the user places the mouse cursor over something for X seconds straight.
i am looking something like this
var startFrame = Time.frameCount;
var printer = this.OnMouseOverAsObservable();
printer
.Buffer(printer.Throttle(TimeSpan.FromSeconds(2f)))
.Where(list => list.Count == Time.frameCount - startFrame)
.Subscribe(_ => print("hello!"));
counting the frames in which the OnMouseOverAsObservable was called
this, btw, doesn’t work properly.
Any thoughts?
Arcant
August 27, 2020, 8:45am
2
using UnityEngine;
using UniRx;
using UniRx.Triggers;
using System;
using UnityEngine.EventSystems;
public class ObservableLongPointerDownTrigger : ObservableTriggerBase, IPointerEnterHandler, IPointerExitHandler
{
public float IntervalSecond = 1f;
Subject<Unit> onLongPointerDown;
float? raiseTime;
private void Start()
{
var hover = this.UpdateAsObservable()
.Where(_ => raiseTime != null && raiseTime <= Time.realtimeSinceStartup)
.Subscribe(_ =>
{
if (onLongPointerDown != null) onLongPointerDown.OnNext(Unit.Default);
raiseTime = null;
});
var enter = this.OnMouseEnterAsObservable()
.Subscribe(_ =>
{
raiseTime = Time.realtimeSinceStartup + IntervalSecond;
});
var exit = this.OnMouseExitAsObservable()
.Subscribe(_ =>
{
raiseTime = null;
});
}
void Update()
{
if (raiseTime != null && raiseTime <= Time.realtimeSinceStartup)
{
if (onLongPointerDown != null) onLongPointerDown.OnNext(Unit.Default);
raiseTime = null;
}
}
public IObservable<Unit> OnLongPointerDownAsObservable()
{
return onLongPointerDown ?? (onLongPointerDown = new Subject<Unit>());
}
protected override void RaiseOnCompletedOnDestroy()
{
if (onLongPointerDown != null)
{
onLongPointerDown.OnCompleted();
}
}
public void OnPointerEnter(PointerEventData eventData)
{
raiseTime = Time.realtimeSinceStartup + IntervalSecond;
}
public void OnPointerExit(PointerEventData eventData)
{
raiseTime = null;
}
}
public class Hoverable : MonoBehaviour
{
void Awake()
{
var trigger = gameObject.AddComponent<ObservableLongPointerDownTrigger>();
trigger.OnLongPointerDownAsObservable().Subscribe(_ => print("HOVERING"));
}
}