备份CatanBuilding瘦身独立工程
This commit is contained in:
79
Assets/Scripts/UIExtend/UIButton.cs
Normal file
79
Assets/Scripts/UIExtend/UIButton.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using UniRx;
|
||||
using UniRx.Triggers;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UIButton : Button
|
||||
{
|
||||
[SerializeField]private float _holdThresh = 0.5f;
|
||||
private ButtonClickedEvent m_OnDown = new ButtonClickedEvent();
|
||||
private ButtonClickedEvent m_OnUp = new ButtonClickedEvent();
|
||||
private ButtonClickedEvent m_OnLongPress = new ButtonClickedEvent();
|
||||
private ButtonClickedEvent m_OnShortClick = new ButtonClickedEvent();
|
||||
private IDisposable clickStream, longPressStream;
|
||||
private DateTimeOffset _lastDownTime, _lastUpTime;
|
||||
//private CompositeDisposable _pressTimer;
|
||||
//private float ci;
|
||||
PointerEventData pointerEventData;
|
||||
protected override void Start()
|
||||
{
|
||||
var pointerDownStream = this.OnPointerDownAsObservable().Timestamp();
|
||||
var pointerUpStream = this.OnPointerUpAsObservable().Timestamp();
|
||||
//Logic of long press: delayed.Timestamp - delayThreshold - lastButtonUpTime > 0
|
||||
longPressStream = pointerDownStream
|
||||
.SelectMany(_ => Observable.Timer(TimeSpan.FromSeconds(_holdThresh)))
|
||||
.Timestamp()
|
||||
.Where(_ => (_.Timestamp - _lastUpTime ).TotalSeconds > _holdThresh)
|
||||
.Subscribe(_ => m_OnLongPress.Invoke())
|
||||
.AddTo(this);
|
||||
clickStream = pointerUpStream
|
||||
.CombineLatest(pointerDownStream, (eUp, eDown) => eUp.Timestamp - eDown.Timestamp)
|
||||
.Where(interval => interval.TotalSeconds > 0 && interval.TotalSeconds <= _holdThresh)
|
||||
.Subscribe(_ => m_OnShortClick.Invoke())
|
||||
.AddTo(this);
|
||||
}
|
||||
public ButtonClickedEvent onDown
|
||||
{
|
||||
get { return m_OnDown; }
|
||||
set { m_OnDown = value; }
|
||||
}
|
||||
|
||||
public ButtonClickedEvent onUp
|
||||
{
|
||||
get { return m_OnUp; }
|
||||
set { m_OnUp = value; }
|
||||
}
|
||||
|
||||
public ButtonClickedEvent onLongPress
|
||||
{
|
||||
get { return m_OnLongPress; }
|
||||
set { m_OnLongPress = value; }
|
||||
}
|
||||
|
||||
public new ButtonClickedEvent onClick
|
||||
{
|
||||
get { return m_OnShortClick; }
|
||||
set { m_OnShortClick = value; }
|
||||
}
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
base.OnPointerDown(eventData);
|
||||
m_OnDown.Invoke();
|
||||
pointerEventData = eventData;
|
||||
_lastDownTime = DateTimeOffset.Now;
|
||||
}
|
||||
public override void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
base.OnPointerUp(eventData);
|
||||
m_OnUp.Invoke();
|
||||
_lastUpTime = DateTimeOffset.Now;
|
||||
}
|
||||
public void Up()
|
||||
{
|
||||
OnPointerUp(pointerEventData);
|
||||
}
|
||||
public bool IsDowning => IsPressed();
|
||||
}
|
||||
Reference in New Issue
Block a user