Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Question from UnityForum
  • Loading branch information
neuecc committed Dec 8, 2014
1 parent 265cbbb commit 0160cc7
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Assets/ObjectTest/UniRxTestSandbox.cs
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using UniRx;
using System.Threading;
using System.Collections.Generic;
using System;
using System.Text;
using UniRx.Diagnostics;
Expand Down Expand Up @@ -75,6 +76,9 @@ public override void FixedUpdate()

IDisposable yieldCancel = null;

Subscriber subscriber = new Subscriber();


public void OnGUI()
{
var xpos = 0;
Expand Down Expand Up @@ -220,6 +224,26 @@ public void OnGUI()
.Subscribe(x => logger.Debug(x));
}

ypos += 100;
if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Subscribe"))
{
subscriber.InitSubscriptions();
Debug.Log("Subscribe++ : " + subscriber.SubscriptionCount);
}

ypos += 100;
if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Push"))
{
Publisher.foo();
}

ypos += 100;
if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Unsubscriber"))
{
subscriber.RemoveSubscriptions();
Debug.Log("UnsubscribeAll : " + subscriber.SubscriptionCount);
}

// Time

var sb = new StringBuilder();
Expand Down Expand Up @@ -277,6 +301,54 @@ IEnumerator Test()
yield return 1000;
logger.Debug("second");
}

// Question from UnityForum #45

public static class Publisher
{
private static readonly object _Lock = new object();
private static UniRx.Subject<bool> item = new UniRx.Subject<bool>();

public static UniRx.IObservable<bool> Item
{
get
{
return item; // no needs lock
}
}

public static void foo()
{
item.OnNext(true);
}
}

public class Subscriber
{
private CompositeDisposable m_Subscriptions = new CompositeDisposable();

public int SubscriptionCount { get { return m_Subscriptions.Count; } }

public void InitSubscriptions()
{
m_Subscriptions.Add(Publisher.Item.Subscribe(UniRx.Observer.Create<bool>(result => this.HandleItem(result), ex => this.HandleError(ex), () => { })));
}

void HandleItem(bool args)
{
UnityEngine.Debug.Log("Received Item: " + args);
}

void HandleError(Exception ex)
{
UnityEngine.Debug.Log("Exception: " + ex.Message);
}

public void RemoveSubscriptions()
{
m_Subscriptions.Clear();
}
}
}
}

Expand Down

0 comments on commit 0160cc7

Please sign in to comment.