No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnityEventInvoke.cs 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using NUnit.Framework;
  2. using UnityEditor.Events;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class UnityEventInvoke
  6. {
  7. class SimpleCounter : MonoBehaviour
  8. {
  9. public int m_Count = 0;
  10. public void Add()
  11. {
  12. ++m_Count;
  13. }
  14. public void NoOp(int i)
  15. {
  16. }
  17. }
  18. GameObject m_CounterObject;
  19. SimpleCounter Counter { get; set; }
  20. [SetUp]
  21. public void TestSetup()
  22. {
  23. m_CounterObject = new GameObject("Counter");
  24. Counter = m_CounterObject.AddComponent<SimpleCounter>();
  25. }
  26. [TearDown]
  27. public void TearDown()
  28. {
  29. GameObject.DestroyImmediate(m_CounterObject);
  30. }
  31. [Test]
  32. [Description("Using a CachedInvokableCall in a UnityEvent should not go re-trigger all the calls stored in the UnityEvent. Case-950588")]
  33. public void UnityEvent_InvokeCallsListenerOnce()
  34. {
  35. var _event = new UnityEvent();
  36. UnityEventTools.AddPersistentListener(_event, new UnityAction(Counter.Add));
  37. _event.SetPersistentListenerState(0, UnityEventCallState.EditorAndRuntime);
  38. _event.Invoke();
  39. Assert.AreEqual(1, Counter.m_Count);
  40. for (int i = 1; i < 5; ++i)
  41. {
  42. UnityEventTools.AddIntPersistentListener(_event, new UnityAction<int>(Counter.NoOp), i);
  43. _event.SetPersistentListenerState(i, UnityEventCallState.EditorAndRuntime);
  44. }
  45. _event.Invoke();
  46. Assert.AreEqual(2, Counter.m_Count);
  47. }
  48. [Test]
  49. [Description("Using a CachedInvokableCall in a UnityEvent should not go re-trigger all the calls stored in the UnityEvent. Case-950588")]
  50. public void UnityEvent_EditMode_InvokeDoesNotCallRuntimeListener()
  51. {
  52. var _event = new UnityEvent();
  53. UnityEventTools.AddPersistentListener(_event, new UnityAction(Counter.Add));
  54. Assert.AreEqual(UnityEventCallState.RuntimeOnly, _event.GetPersistentListenerState(0));
  55. Assert.False(Application.isPlaying);
  56. _event.Invoke();
  57. Assert.AreEqual(0, Counter.m_Count, "Expected Event to not be called when not in play mode and event is marked as Runtime only");
  58. for (int i = 1; i < 5; ++i)
  59. {
  60. UnityEventTools.AddIntPersistentListener(_event, new UnityAction<int>(Counter.NoOp), i);
  61. }
  62. _event.Invoke();
  63. Assert.AreEqual(0, Counter.m_Count, "Expected Event to not be called when not in play mode and event is marked as Runtime only");
  64. }
  65. }