UnityEventInvoke.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEditor;
  4. using UnityEditor.Events;
  5. using UnityEngine.TestTools;
  6. using NUnit.Framework;
  7. using System.Collections;
  8. public class UnityEventInvoke
  9. {
  10. class SimpleCounter : MonoBehaviour
  11. {
  12. public int m_Count = 0;
  13. public void Add()
  14. {
  15. ++m_Count;
  16. }
  17. public void NoOp(int i)
  18. {
  19. }
  20. }
  21. GameObject m_CounterObject;
  22. SimpleCounter Counter { get; set; }
  23. [SetUp]
  24. public void TestSetup()
  25. {
  26. m_CounterObject = new GameObject("Counter");
  27. Counter = m_CounterObject.AddComponent<SimpleCounter>();
  28. }
  29. [TearDown]
  30. public void TearDown()
  31. {
  32. GameObject.DestroyImmediate(m_CounterObject);
  33. }
  34. [Test]
  35. [Description("Using a CachedInvokableCall in a UnityEvent should not go re-trigger all the calls stored in the UnityEvent. Case-950588")]
  36. public void UnityEvent_InvokeCallsListenerOnce()
  37. {
  38. var _event = new UnityEvent();
  39. UnityEventTools.AddPersistentListener(_event, new UnityAction(Counter.Add));
  40. _event.SetPersistentListenerState(0, UnityEventCallState.EditorAndRuntime);
  41. _event.Invoke();
  42. Assert.AreEqual(1, Counter.m_Count);
  43. for (int i = 1; i < 5; ++i)
  44. {
  45. UnityEventTools.AddIntPersistentListener(_event, new UnityAction<int>(Counter.NoOp), i);
  46. _event.SetPersistentListenerState(i, UnityEventCallState.EditorAndRuntime);
  47. }
  48. _event.Invoke();
  49. Assert.AreEqual(2, Counter.m_Count);
  50. }
  51. }