Create a gist now

Instantly share code, notes, and snippets.

using UnityEngine;
using System.Collections;
using UniRx;
using UniRx.Triggers;
using System.Linq;
using System;
public class LongPressSample : MonoBehaviour
{
public float chargePower = 1;
public float chargeBase = 1;
public float chargeMax = 6;
public GameObject spawner;
// Use this for initialization
void Start()
{
spawner = GameObject.Find("TestSpawner");
var fire1Stream = this.UpdateAsObservable()
.Where(_ => Input.GetButton("Fire1"));
var fire1DownStream = this.UpdateAsObservable()
.Where(_ => Input.GetButtonDown("Fire1"));
var fire1UpStream = this.UpdateAsObservable()
.Where(_ => Input.GetButtonUp("Fire1"));
fire1DownStream.Subscribe(_ => chargePower = 1);
var fire1ChargeStream = fire1Stream.Select(_ => Time.deltaTime)
.Scan((prev, next) => prev + next)
.TakeUntil(fire1UpStream)
.RepeatUntilDestroy(this)
.Skip(1)
.Where(x => x > 1);
fire1ChargeStream.Subscribe(x => chargePower = Mathf.Min(chargeBase + x, chargeMax));
fire1UpStream.Subscribe(_ => {
spawner.SendMessage("SpawnObject", chargePower);
chargePower = 1f;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment