Esempio n. 1
0
        public IEnumerator TestMultipleEntities()
        {
            int value1 = 0;
            int value2 = 0;

            Action <EvSimpleEvent> callback1 = x => { value1 += 1; };
            Action <EvSimpleEvent> callback2 = x => { value2 += 1; };

            EventTarget entity1 = EventTarget.CreateTarget();
            EventTarget entity2 = EventTarget.CreateTarget();

            EventManager.Subscribe(entity1, callback1, EventUpdateTick.Update);
            EventManager.Subscribe(entity2, callback2, EventUpdateTick.Update);

            EventManager.SendEvent(entity1, new EvSimpleEvent(), EventUpdateTick.Update);

            yield return(null);

            Assert.IsTrue(value1 == 1);
            Assert.IsTrue(value2 == 0);

            EventManager.SendEvent(entity2, new EvSimpleEvent(), EventUpdateTick.Update);

            yield return(null);

            Assert.IsTrue(value1 == 1);
            Assert.IsTrue(value2 == 1);
        }
Esempio n. 2
0
        public IEnumerator TestMultipleEntitiesStandardVsJob()
        {
            EventTarget entity1 = EventTarget.CreateTarget();
            EventTarget entity2 = EventTarget.CreateTarget();

            int value = 0;

            Action <TestJob>       callback1 = x => { Assert.IsTrue(x.result == 10); };
            Action <EvSimpleEvent> callback2 = x => { value += x.value; };

            EventManager.SubscribeWithJob <TestJob, EvSimpleEvent>(entity1, new TestJob(), callback1, EventUpdateTick.Update);
            EventManager.Subscribe(entity2, callback2, EventUpdateTick.Update);

            EventManager.SendEvent(entity1, new EvSimpleEvent(10), EventUpdateTick.Update);

            yield return(null);

            Assert.IsTrue(value == 0);

            EventManager.SendEvent(entity2, new EvSimpleEvent(20), EventUpdateTick.Update);

            yield return(null);

            Assert.IsTrue(value == 20);
        }
        public void UseCustomSystem()
        {
            // Event systems are associated with an EventTarget, this is how an event system can know who to send
            // an event to but keep all events together for performance.
            //
            // For example the global sim event system is its own EventTarget. As is the Global UI system and each
            // GameObject is converted to an EventTarget.
            //
            // It is better to use a single UnityEventSystem with multiple entities than an UnityEventSystem for each
            // target. For example if there are multiple 'global' systems that all get processed at the same time then
            // it is more performant to have a single UnityEventSystem and an EventTarget for each 'global system' that
            // talk to the same UnityEventSystem.
            //
            // GlobalEventSystem, GameObject systems, and TickEventSystem all use EventManager which uses one UnityEventSystem
            // for each update tick.
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            // Can subscribe a listener to both entities
            _system.Subscribe <EvExampleEvent>(target1, OnExampleEvent);
            _system.Subscribe <EvExampleEvent>(target2, OnExampleEventDoublePrint);

            // We queue up events, avoided the 'send' verb here since we have to manually process the events.
            _system.QueueEvent(target1, new EvExampleEvent(1));

            // Now we process the queued events. Since we only sent an event to the entity1 system, the listener to entity2
            // will NOT invoke.
            _system.ProcessEvents();

            // Each listener needs to unsubscribe from the appropriate event entity
            _system.Unsubscribe <EvExampleEvent>(target1, OnExampleEvent);
            _system.Unsubscribe <EvExampleEvent>(target2, OnExampleEventDoublePrint);

            // To have a global system that you control just store an EventEntity and use that.
        }
Esempio n. 4
0
        public void TestMultipleEvents()
        {
            EventTarget target = EventTarget.CreateTarget();

            int value = -1;

            Action <TestJob> callback = x => { Assert.IsTrue(x.result == value); };

            _handler.Subscribe(target, new TestJob(value), callback);

            // Should warn!
            _handler.QueueEvent(target, new EvSimpleEvent(10));
            _handler.QueueEvent(target, new EvSimpleEvent(10));
            _handler.QueueEvent(target, new EvSimpleEvent(10));
            _handler.QueueEvent(target, new EvSimpleEvent(10));

            // Only one event is accepted
            value = 9;
            _handler.ProcessEvents();

            _handler.Unsubscribe(target, callback);
            _handler.VerifyNoSubscribers();

            _handler.QueueEvent(target, new EvSimpleEvent(10));
            _handler.ProcessEvents();
        }
Esempio n. 5
0
        public IEnumerator TestUpdateEvents()
        {
            EventTarget target = EventTarget.CreateTarget();

            Action <EvSimpleEvent> updateCallback = x =>
            {
                string stacktrace = Environment.StackTrace;
                Assert.IsTrue(stacktrace.Contains("EventManager.Update ()"));
            };

            Action <EvSimpleEvent> fixedUpdateCallback = x =>
            {
                string stacktrace = Environment.StackTrace;
                Assert.IsTrue(stacktrace.Contains("EventManager.FixedUpdate ()"));
            };

            Action <EvSimpleEvent> lateUpdateCallback = x =>
            {
                string stacktrace = Environment.StackTrace;
                Assert.IsTrue(stacktrace.Contains("EventManager.LateUpdate ()"));
            };

            EventManager.Subscribe(target, updateCallback, EventUpdateTick.Update);
            EventManager.Subscribe(target, fixedUpdateCallback, EventUpdateTick.FixedUpdate);
            EventManager.Subscribe(target, lateUpdateCallback, EventUpdateTick.LateUpdate);

            EventManager.SendEvent(target, new EvSimpleEvent(), EventUpdateTick.Update);
            EventManager.SendEvent(target, new EvSimpleEvent(), EventUpdateTick.FixedUpdate);
            EventManager.SendEvent(target, new EvSimpleEvent(), EventUpdateTick.LateUpdate);

            yield return(new WaitForFixedUpdate());

            yield return(null);
        }
Esempio n. 6
0
        public void TestMultipleSubscribes()
        {
            EventTarget target = EventTarget.CreateTarget();

            Action <EvSimpleEvent> callback = x => { };

            _system.Subscribe(target, callback);

            Assert.Throws <MultipleSubscriptionsException <EvSimpleEvent> >(() => _system.Subscribe(target, callback));
        }
Esempio n. 7
0
        public void TestMultipleSubscribes()
        {
            EventTarget target = EventTarget.CreateTarget();

            Action <TestJob> callback = x => { };

            _handler.Subscribe(target, new TestJob(), callback);

            Assert.Throws <MultipleSubscriptionsException <TestJob> >(() =>
                                                                      _handler.Subscribe(target, new TestJob(), callback));
        }
Esempio n. 8
0
        public IEnumerator TestSimpleValueJob()
        {
            EventTarget target = EventTarget.CreateTarget();

            Action <TestJob> callback = x => { Assert.IsTrue(x.result == 10); };

            EventManager.SubscribeWithJob <TestJob, EvSimpleEvent>(target, new TestJob(), callback, EventUpdateTick.Update);
            EventManager.SendEvent(target, new EvSimpleEvent(10), EventUpdateTick.Update);

            yield return(null);
        }
Esempio n. 9
0
        public IEnumerator TestSimpleValue()
        {
            EventTarget target = EventTarget.CreateTarget();

            int value = 0;
            Action <EvSimpleEvent> callback = x => { value += x.value; };

            EventManager.Subscribe(target, callback, EventUpdateTick.Update);

            EventManager.SendEvent(target, new EvSimpleEvent(10), EventUpdateTick.Update);
            Assert.IsTrue(value == 0);

            yield return(null);

            Assert.IsTrue(value == 10);
        }
Esempio n. 10
0
        public IEnumerator TestStandardVsJob()
        {
            int         value  = 0;
            EventTarget target = EventTarget.CreateTarget();

            Action <TestJob>       callback1 = x => { Assert.IsTrue(x.result == 10); };
            Action <EvSimpleEvent> callback2 = x => { value += x.value; };

            EventManager.SubscribeWithJob <TestJob, EvSimpleEvent>(target, new TestJob(), callback1, EventUpdateTick.Update);
            EventManager.Subscribe(target, callback2, EventUpdateTick.Update);

            EventManager.SendEvent(target, new EvSimpleEvent(10), EventUpdateTick.Update);

            yield return(null);

            Assert.IsTrue(value == 10);
        }
Esempio n. 11
0
        public void TestGameObjectRange()
        {
            int minId   = int.MinValue;
            int maxId   = int.MaxValue;
            int allBits = unchecked ((int)0xffffffff);

            // This represents the range for gameobjects, should only be the first 32 bits of a ulong
            EventTarget minTarget     = new EventTarget(minId);
            EventTarget maxTarget     = new EventTarget(maxId);
            EventTarget allBitsTarget = new EventTarget(allBits);

            ulong zeroedBits = 0xffffffff;

            zeroedBits <<= 32;

            Assert.IsTrue((minTarget.id & zeroedBits) == 0);
            Assert.IsTrue((maxTarget.id & zeroedBits) == 0);
            Assert.IsTrue((allBitsTarget.id & zeroedBits) == 0);
            Assert.IsTrue((EventTarget.CreateTarget().id & zeroedBits) != 0);
        }
Esempio n. 12
0
        public void TestOtherUnsubscribe()
        {
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            int value = 5;

            Action <TestJob> callback  = x => { Assert.IsTrue(x.result == value); };
            Action <TestJob> callback2 = x => { Assert.Fail(); };

            _handler.Subscribe(target1, new TestJob(value), callback);
            _handler.Subscribe(target2, new TestJob(value), callback2);
            _handler.Unsubscribe(target2, callback2);
            _handler.QueueEvent(target1, new EvSimpleEvent(10));

            value = 15;
            _handler.ProcessEvents();
            _handler.Unsubscribe(target1, callback);
            _handler.VerifyNoSubscribers();
        }
Esempio n. 13
0
        public void TestSimpleEvent()
        {
            EventTarget target = EventTarget.CreateTarget();

            int value = 10;

            Action <TestJob> callback = x => { Assert.IsTrue(x.result == value); };

            _handler.Subscribe(target, new TestJob(value), callback);

            _handler.QueueEvent(target, new EvSimpleEvent(10));
            value = 20;
            _handler.ProcessEvents();

            _handler.Unsubscribe(target, callback);
            _handler.VerifyNoSubscribers();

            _handler.QueueEvent(target, new EvSimpleEvent(10));
            _handler.ProcessEvents();
        }
Esempio n. 14
0
        public void TestMeUnsubscribe()
        {
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            int value = 0;

            Action <EvSimpleEvent> callback = x => value += x.value;

            _system.Subscribe(target1, callback);
            _system.Subscribe(target2, callback);
            _system.Unsubscribe(target1, callback);
            _system.QueueEvent(target1, new EvSimpleEvent(10));

            _system.ProcessEvents();
            _system.Unsubscribe(target2, callback);
            _system.VerifyNoSubscribers();

            Assert.IsTrue(value == 0);
        }
Esempio n. 15
0
        public void TestMeUnsubscribe()
        {
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            int value = -1;

            Action <TestJob> callback = x => { Assert.Fail(); };

            _handler.Subscribe(target1, new TestJob(value), callback);
            _handler.Subscribe(target2, new TestJob(value), callback);
            _handler.Unsubscribe(target1, callback);
            _handler.QueueEvent(target1, new EvSimpleEvent(10));

            _handler.ProcessEvents();
            _handler.Unsubscribe(target2, callback);
            _handler.VerifyNoSubscribers();

            Assert.IsTrue(value == -1);
        }
Esempio n. 16
0
        public void TestMultipleEntities()
        {
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            int value1 = 0;
            int value2 = 0;

            Action <EvSimpleEvent> callback  = x => value1 += x.value;
            Action <EvSimpleEvent> callback2 = x => value2 += x.value;

            _system.Subscribe(target1, callback);
            _system.Subscribe(target2, callback2);
            _system.QueueEvent(target1, new EvSimpleEvent(10));
            _system.QueueEvent(target2, new EvSimpleEvent(30));

            _system.ProcessEvents();
            _system.Unsubscribe(target1, callback);
            _system.Unsubscribe(target2, callback2);
            _system.VerifyNoSubscribers();

            Assert.IsTrue(value1 == 10);
            Assert.IsTrue(value2 == 30);
        }
Esempio n. 17
0
        public void TestMultipleEntities()
        {
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            int value1 = 1;
            int value2 = 11;

            Action <TestJob> callback  = x => { Assert.IsTrue(x.result == value1); };
            Action <TestJob> callback2 = x => { Assert.IsTrue(x.result == value2); };

            _handler.Subscribe(target1, new TestJob(value1), callback);
            _handler.Subscribe(target2, new TestJob(value2), callback2);
            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));

            value1 = 11;
            value2 = 41;

            _handler.ProcessEvents();
            _handler.Unsubscribe(target1, callback);
            _handler.Unsubscribe(target2, callback2);
            _handler.VerifyNoSubscribers();
        }
Esempio n. 18
0
        public void StandardHandlerExample()
        {
            // Handlers ultimately are what store the subscriptions and process events. Ideally they shouldn't need
            // to be used directly and TickEventSystem and UnityEventSystem would be sufficient in most cases.

            // See ExampleControlledEventSystem.cs for a description on EventEntities.
            EventTarget target = EventTarget.CreateTarget();

            // Subscribe, Unsubscribe, and Events will seem familiar but can ONLY use EvExampleEvent. Doesn't work for
            // all events.
            _standardHandler.Subscribe(target, OnExampleEvent);

            // Handlers have to be told to process events so we queue an event and process it later.
            _standardHandler.QueueEvent(target, new EvExampleEvent(7777));
            _standardHandler.ProcessEvents();

            _standardHandler.Unsubscribe(target, OnExampleEvent);

            // There's a job handler as well, they are separate and won't fire on the same events
            _jobHandler.Subscribe(target, new ExampleJob(), OnJobFinished);
            _jobHandler.QueueEvent(target, new EvExampleEvent(111));
            _jobHandler.ProcessEvents();
            _jobHandler.Unsubscribe(target, OnJobFinished);
        }
Esempio n. 19
0
        public void TestMultipleSubscribeUnsubscribesEvents()
        {
            EventTarget target1 = EventTarget.CreateTarget();
            EventTarget target2 = EventTarget.CreateTarget();

            int value1 = 0;
            int value2 = 0;

            Action <TestJob> callback  = x => { Assert.IsTrue(x.result == value1); };
            Action <TestJob> callback2 = x => { Assert.IsTrue(x.result == value2); };

            _handler.Subscribe(target1, new TestJob(value1), callback);
            _handler.Subscribe(target2, new TestJob(value2), callback2);
            _handler.Unsubscribe(target2, callback2);

            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));

            value1 = 10;
            Assert.IsTrue(value2 == 0);
            _handler.ProcessEvents();

            _handler.Subscribe(target2, new TestJob(value2), callback2);

            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));

            value1 = 20;
            value2 = 30;
            _handler.ProcessEvents();

            _handler.Unsubscribe(target1, callback);

            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));

            Assert.IsTrue(value1 == 20);
            value2 = 60;
            _handler.ProcessEvents();

            _handler.Subscribe(target1, new TestJob(value1), callback);

            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));

            value1 = 30;
            value2 = 90;
            _handler.ProcessEvents();

            _handler.Unsubscribe(target2, callback2);

            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));

            value1 = 40;
            Assert.IsTrue(value2 == 90);
            _handler.ProcessEvents();

            _handler.Unsubscribe(target1, callback);

            _handler.QueueEvent(target1, new EvSimpleEvent(10));
            _handler.QueueEvent(target2, new EvSimpleEvent(30));
            _handler.ProcessEvents();

            _handler.VerifyNoSubscribers();

            Assert.IsTrue(value1 == 40);
            Assert.IsTrue(value2 == 90);
        }