Skip to content

Commit 1767ad0

Browse files
committed
Import UniRX package
1 parent 2233e18 commit 1767ad0

File tree

518 files changed

+45502
-61
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

518 files changed

+45502
-61
lines changed

Assets/Plugins.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/UniRx.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/UniRx/Examples.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#if !(UNITY_METRO || UNITY_WP8)
2+
3+
using UnityEngine;
4+
5+
namespace UniRx.Examples
6+
{
7+
// sample script, attach your object.
8+
public class Sample01_ObservableWWW : MonoBehaviour
9+
{
10+
void Start()
11+
{
12+
// Basic: Download from google.
13+
{
14+
ObservableWWW.Get("http://google.co.jp/")
15+
.Subscribe(
16+
x => Debug.Log(x.Substring(0, 100)), // onSuccess
17+
ex => Debug.LogException(ex)); // onError
18+
}
19+
20+
// Linear Pattern with LINQ Query Expressions
21+
// download after google, start bing download
22+
{
23+
var query = from google in ObservableWWW.Get("http://google.com/")
24+
from bing in ObservableWWW.Get("http://bing.com/")
25+
select new { google, bing };
26+
27+
var cancel = query.Subscribe(x => Debug.Log(x.google.Substring(0, 100) + ":" + x.bing.Substring(0, 100)));
28+
29+
// Call Dispose is cancel downloading.
30+
cancel.Dispose();
31+
}
32+
33+
// Observable.WhenAll is for parallel asynchronous operation
34+
// (It's like Observable.Zip but specialized for single async operations like Task.WhenAll of .NET 4)
35+
{
36+
var parallel = Observable.WhenAll(
37+
ObservableWWW.Get("http://google.com/"),
38+
ObservableWWW.Get("http://bing.com/"),
39+
ObservableWWW.Get("http://unity3d.com/"));
40+
41+
parallel.Subscribe(xs =>
42+
{
43+
Debug.Log(xs[0].Substring(0, 100)); // google
44+
Debug.Log(xs[1].Substring(0, 100)); // bing
45+
Debug.Log(xs[2].Substring(0, 100)); // unity
46+
});
47+
}
48+
49+
// with Progress
50+
{
51+
// notifier for progress
52+
var progressNotifier = new ScheduledNotifier<float>();
53+
progressNotifier.Subscribe(x => Debug.Log(x)); // write www.progress
54+
55+
// pass notifier to WWW.Get/Post
56+
ObservableWWW.Get("http://google.com/", progress: progressNotifier).Subscribe();
57+
}
58+
59+
// with Error
60+
{
61+
// If WWW has .error, ObservableWWW throws WWWErrorException to onError pipeline.
62+
// WWWErrorException has RawErrorMessage, HasResponse, StatusCode, ResponseHeaders
63+
ObservableWWW.Get("http://www.google.com/404")
64+
.CatchIgnore((WWWErrorException ex) =>
65+
{
66+
Debug.Log(ex.RawErrorMessage);
67+
if (ex.HasResponse)
68+
{
69+
Debug.Log(ex.StatusCode);
70+
}
71+
foreach (var item in ex.ResponseHeaders)
72+
{
73+
Debug.Log(item.Key + ":" + item.Value);
74+
}
75+
})
76+
.Subscribe();
77+
}
78+
}
79+
}
80+
}
81+
82+
#endif

Assets/Scripts/ILoggerObserver.cs.meta renamed to Assets/Plugins/UniRx/Examples/Sample01_ObservableWWW.cs.meta

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using UnityEngine;
2+
using UniRx.Triggers; // Triggers Namepsace
3+
using System;
4+
5+
namespace UniRx.Examples
6+
{
7+
public class Sample02_ObservableTriggers : MonoBehaviour
8+
{
9+
void Start()
10+
{
11+
// Get the plain object
12+
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
13+
14+
// Add ObservableXxxTrigger for handle MonoBehaviour's event as Observable
15+
cube.AddComponent<ObservableUpdateTrigger>()
16+
.UpdateAsObservable()
17+
.SampleFrame(30)
18+
.Subscribe(x => Debug.Log("cube"), () => Debug.Log("destroy"));
19+
20+
// destroy after 3 second:)
21+
GameObject.Destroy(cube, 3f);
22+
}
23+
}
24+
}

Assets/Plugins/UniRx/Examples/Sample02_ObservableTriggers.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
2+
3+
using UnityEngine;
4+
using UniRx.Triggers; // for enable gameObject.EventAsObservbale()
5+
6+
namespace UniRx.Examples
7+
{
8+
public class Sample03_GameObjectAsObservable : MonoBehaviour
9+
{
10+
void Start()
11+
{
12+
// All events can subscribe by ***AsObservable if enables UniRx.Triggers
13+
this.OnMouseDownAsObservable()
14+
.SelectMany(_ => this.gameObject.UpdateAsObservable())
15+
.TakeUntil(this.gameObject.OnMouseUpAsObservable())
16+
.Select(_ => Input.mousePosition)
17+
.RepeatUntilDestroy(this)
18+
.Subscribe(x => Debug.Log(x), ()=> Debug.Log("!!!" + "complete"));
19+
}
20+
}
21+
}
22+
23+
#endif

Assets/Plugins/UniRx/Examples/Sample03_GameObjectAsObservable.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace UniRx.Examples
5+
{
6+
public class Sample04_ConvertFromUnityCallback : MonoBehaviour
7+
{
8+
// This is about log but more reliable log sample => Sample11_Logger
9+
10+
private class LogCallback
11+
{
12+
public string Condition;
13+
public string StackTrace;
14+
public UnityEngine.LogType LogType;
15+
}
16+
17+
static class LogHelper
18+
{
19+
// If static register callback, use Subject for event branching.
20+
21+
#if (UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7)
22+
static Subject<LogCallback> subject;
23+
24+
public static IObservable<LogCallback> LogCallbackAsObservable()
25+
{
26+
if (subject == null)
27+
{
28+
subject = new Subject<LogCallback>();
29+
30+
// Publish to Subject in callback
31+
32+
33+
UnityEngine.Application.RegisterLogCallback((condition, stackTrace, type) =>
34+
{
35+
subject.OnNext(new LogCallback { Condition = condition, StackTrace = stackTrace, LogType = type });
36+
});
37+
}
38+
39+
return subject.AsObservable();
40+
}
41+
42+
#else
43+
// If standard evetns, you can use Observable.FromEvent.
44+
45+
public static IObservable<LogCallback> LogCallbackAsObservable()
46+
{
47+
return Observable.FromEvent<Application.LogCallback, LogCallback>(
48+
h => (condition, stackTrace, type) => h(new LogCallback { Condition = condition, StackTrace = stackTrace, LogType = type }),
49+
h => Application.logMessageReceived += h, h => Application.logMessageReceived -= h);
50+
}
51+
#endif
52+
}
53+
54+
void Awake()
55+
{
56+
// method is separatable and composable
57+
LogHelper.LogCallbackAsObservable()
58+
.Where(x => x.LogType == LogType.Warning)
59+
.Subscribe(x => Debug.Log(x));
60+
61+
LogHelper.LogCallbackAsObservable()
62+
.Where(x => x.LogType == LogType.Error)
63+
.Subscribe(x => Debug.Log(x));
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)