FXTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UnityEngine;
  2. using UnityEngine.TestTools;
  3. using NUnit.Framework;
  4. using System.Collections;
  5. public class FXTests
  6. {
  7. GameObject spaceshipPrefab;
  8. GameObject asteroidPrefab;
  9. GameObject spaceshipDebrisPrefab;
  10. GameObject explosionPrefab;
  11. [SetUp]
  12. public void Setup()
  13. {
  14. GameManager.InitializeTestingEnvironment(false, false, true, false, false);
  15. spaceshipPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().spaceshipPrefab;
  16. asteroidPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().asteroidPrefab;
  17. spaceshipDebrisPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().spaceshipDebrisPrefab;
  18. explosionPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().explosionPrefab;
  19. }
  20. void ClearScene()
  21. {
  22. Transform[] objects = Object.FindObjectsOfType<Transform>();
  23. foreach (Transform obj in objects)
  24. {
  25. if (obj != null)
  26. Object.DestroyImmediate(obj.gameObject);
  27. }
  28. }
  29. [Test]
  30. public void _01_FXPrefabsExist()
  31. {
  32. Assert.NotNull(spaceshipDebrisPrefab);
  33. Assert.NotNull(spaceshipDebrisPrefab.GetComponent<DebrisController>());
  34. Assert.NotNull(explosionPrefab);
  35. }
  36. [UnityTest]
  37. public IEnumerator _02_DebrisFragmentsHaveVelocityOnStart()
  38. {
  39. ClearScene();
  40. GameObject debris = Object.Instantiate(spaceshipDebrisPrefab, Vector3.zero, Quaternion.identity);
  41. yield return null;
  42. foreach(Rigidbody2D fragment in debris.GetComponentsInChildren<Rigidbody2D>())
  43. {
  44. Assert.IsTrue(fragment.velocity != Vector2.zero);
  45. }
  46. }
  47. [UnityTest]
  48. public IEnumerator _03_DebrisFragmentsAreDestroyedAfterSeconds()
  49. {
  50. ClearScene();
  51. GameObject debris = Object.Instantiate(spaceshipDebrisPrefab, Vector3.zero, Quaternion.identity);
  52. yield return new WaitForSeconds(1.0f); // Debris should be destroyed after 1 sec
  53. Assert.IsTrue(debris == null);
  54. Assert.IsTrue(Object.FindObjectsOfType<Rigidbody2D>().Length == 0);
  55. }
  56. [UnityTest]
  57. public IEnumerator _04_ExplosionParticlesAreSpawnedWithDebris()
  58. {
  59. ClearScene();
  60. GameObject debris = Object.Instantiate(spaceshipDebrisPrefab, Vector3.zero, Quaternion.identity);
  61. yield return null;
  62. ParticleSystem explosion = Object.FindObjectOfType<ParticleSystem>();
  63. Assert.IsTrue(explosion != null);
  64. yield return new WaitForSeconds(explosion.main.duration);
  65. Assert.IsTrue(explosion == null);
  66. }
  67. [UnityTest]
  68. public IEnumerator _05_ExplosionParticlesActivateChildParticlesAndSubEmmitersSuccessfully()
  69. {
  70. ClearScene();
  71. ParticleSystem explosion = Object.Instantiate(spaceshipDebrisPrefab.GetComponent<DebrisController>().explosionParticles, Vector3.zero, Quaternion.identity).GetComponent<ParticleSystem>();
  72. Assert.IsTrue(explosion != null);
  73. Assert.IsTrue(explosion.transform.GetChild(0).GetComponent<ParticleSystem>() != null);
  74. Assert.IsTrue(explosion.transform.GetChild(1).GetComponent<ParticleSystem>() != null);
  75. Assert.IsTrue(explosion.transform.GetChild(2).GetComponent<ParticleSystem>() != null);
  76. yield return null;
  77. Assert.IsTrue(explosion.particleCount == 50); // Main explosion PS burst emits 50 particles on the first frame
  78. Assert.IsTrue(explosion.transform.GetChild(0).GetComponent<ParticleSystem>().particleCount == 1); // Glow emits 1 particle on the first frame
  79. Assert.IsTrue(explosion.transform.GetChild(1).GetComponent<ParticleSystem>().particleCount == 2); // Shockwave emits 2 particles on the first frame
  80. Assert.IsTrue(explosion.subEmitters.GetSubEmitterSystem(0) != null); // Subemitter exists, indexOutOfRange if subemitter cannot be found
  81. yield return new WaitForSeconds(0.2f);
  82. Assert.IsTrue(explosion.subEmitters.GetSubEmitterSystem(0).particleCount > 0); // Subemitter emits particles over time
  83. }
  84. }