Skip to content

Instantly share code, notes, and snippets.

@enpel
Last active August 29, 2015 14:21
Show Gist options
  • Save enpel/dc1c8e839a74a899151c to your computer and use it in GitHub Desktop.
Save enpel/dc1c8e839a74a899151c to your computer and use it in GitHub Desktop.
ボタンのロングタップ対応UniRx版 v0.3 [パフォーマンスに難有り] ref: http://qiita.com/enpel/items/1897a9d02d42c6089e80
using UnityEngine;
using System.Collections;
using System;
using UniRx;
using System.Collections.Generic;
[RequireComponent(typeof(UIButton))]
public class UIButtonLongTapComponent : ObservableMonoBehaviour
{
[SerializeField]
float intervalAction = 3.0f;
public UIScrollView scroll = null;
float firstTapTime = 0f;
bool isHoldAction = true;
// 押しっぱなし時に呼び出すAction
public Action OnLongTap = null;
public Action OnShortTap = null;
Vector3 tapPosition = Vector3.zero;
BoolReactiveProperty isPressing = new BoolReactiveProperty();
bool IsPressing { get { return isPressing.Value; } set { isPressing.Value = value; } }
void Start()
{
scroll = this.transform.GetComponentInParents<UIScrollView> ();
IsPressing = false;
var tapInStream = this.UpdateAsObservable().Select(_=>IsPressing).Where (_ => _);
var tapOutStream = this.UpdateAsObservable().Select(_=>IsPressing).Where (_ => !_);
var tapInOutStream = this.UpdateAsObservable ().SkipUntil (tapInStream).TakeUntil (tapOutStream).Repeat ();
var moveStream = tapInOutStream
.Select(x=>scroll.currentMomentum.magnitude)
.Scan((sum,current)=>sum+current)
// .Do(x=>Debug.Log("currentMomentum: "+x))
.FirstOrDefault(x=>x>0.1f)
.Select(_=> "Move");
var longTapStream =
tapInOutStream.TakeUntil(moveStream).SelectMany(_ => Observable.Timer(TimeSpan.FromSeconds(intervalAction))).Select(_ => "LongTap");
longTapStream.Subscribe (_ => IsPressing = false);
moveStream.Subscribe (_ => IsPressing = false);
tapInStream.TakeUntil(moveStream).Timestamp()
.SelectMany(_ => tapOutStream.Timestamp(), (s, e) => (e.Timestamp - s.Timestamp).Seconds >= intervalAction)
.Select(x => x ? "LongTap" : "ShortTap")
.Merge(longTapStream)
.FirstOrDefault()
.RepeatUntilDestroy(gameObject)
.Subscribe(x =>{
Debug.Log(x);
if (x == "LongTap")
LongTap();
if (x == "ShortTap")
ShortTap();
});
}
void OnPress (bool pressed)
{
IsPressing = pressed;
}
public void SetButtonAction(Action shortTap, Action longTap, bool isHoldLongTap) {
isHoldAction = isHoldLongTap;
OnLongTap = longTap;
OnShortTap = shortTap;
}
public void CleanAction()
{
OnLongTap = null;
}
public void LongTap()
{
Debug.Log ("LongTap!!");
if (OnLongTap != null)
OnLongTap ();
}
public void ShortTap()
{
Debug.Log ("ShortTap!!");
if (OnShortTap != null)
OnShortTap ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment