Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created November 8, 2018 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/06e079368c57a7df6689497767dc1755 to your computer and use it in GitHub Desktop.
Save guitarrapc/06e079368c57a7df6689497767dc1755 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UniRx.Async;
using UnityEngine.Networking;
using UniRx;
using System.Collections;
namespace Assets.Scripts.Views
{
public class TestAsync : MonoBehaviour
{
private readonly Subject<string> subscription = new Subject<string>();
private readonly Subject<Unit> oncomplete_subscription = new Subject<Unit>();
private async void Start()
{
Debug.Log("hoge");
await Task.Run(() =>
{
foreach (var item in Enumerable.Range(0, 10))
{
Debug.Log(item);
}
});
Debug.Log("fuga");
Debug.Log("yoti");
#region 期待する動きではない
//// Merge : after task / after task ..... / task1 / task2 / task3
//// Concat : task1 / after task / task2 / after task
//Observable.WhenAll(
////Observable.Merge(
////Observable.Concat(
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task1")),
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task2")),
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task3")),
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task4")),
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task5")),
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task6")),
// Observable.Return(Unit.Default).Select(async _ => await HogeAsync("task7"))
//)
//.Do(_ => Debug.Log("after task"))
//.Subscribe();
#endregion
// IE<T> -> IO<T>[1]
Enumerable.Range(1, 10).ToObservable().Subscribe(x => Debug.Log(x));
// FireAndForget
Debug.Log("1. Before FF");
subscription.Subscribe(x => Debug.Log($"4. {x}")).AddTo(this);
Enumerable.Range(1, 10).ToObservable()
.Do(_ => Debug.Log("2. During FF"))
.Do(x => FireAndForget($"{x}"))
.SelectMany(oncomplete_subscription)
.Subscribe(_ => Debug.Log("5. AfterFireAndForget"))
.AddTo(this);
Debug.Log("3. After FF");
// これをしたいだけの人生だった (2017 ではエラー)
//await ToaruCotoutine(UnityWebRequest.Get("https://google.com"));
}
private async Task HogeAsync(string text)
{
var random = new System.Random();
await Task.Delay(Mathf.Clamp(random.Next(), 100, 5000));
Debug.Log($"{text}");
}
private async void FireAndForget(string text)
{
var random = new System.Random();
await Task.Delay(Mathf.Clamp(random.Next(), 100, 5000));
subscription.OnNext(text);
if (text == "10")
{
oncomplete_subscription.OnNext(Unit.Default);
}
}
private IEnumerator ToaruCotoutine(UnityWebRequest req)
{
var op = req.SendWebRequest();
var text = req.downloadHandler.text;
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment