TestParticleEffect.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEditor;
  2. using UnityEngine;
  3. #if UNITY_EDITOR
  4. /// <summary>
  5. /// 给选中的特效添加脚本的
  6. /// </summary>
  7. [InitializeOnLoad]
  8. public static class TestParticleEffect
  9. {
  10. private const string RequestTestKey = "TestParticleEffectRquestTest";
  11. private static bool _hasPlayed;
  12. static bool isRestart = false;
  13. [MenuItem("GameObject/特效/测试", false, 11)]
  14. private static void Test()
  15. {
  16. var go = Selection.activeGameObject;
  17. var particleSystemRenderer = go.GetComponentsInChildren<ParticleSystemRenderer>(true);
  18. if (particleSystemRenderer.Length == 0)
  19. {
  20. Debug.LogError("不是特效无法测试!");
  21. return;
  22. }
  23. EditorPrefs.SetBool(RequestTestKey, true);
  24. //已经在播放状态,使其重新开始
  25. if (EditorApplication.isPlaying)
  26. {
  27. EditorApplication.isPlaying = false;
  28. isRestart = true;
  29. }
  30. else
  31. {
  32. EditorApplication.isPlaying = true;
  33. }
  34. var particleEffectScript = go.GetComponentsInChildren<ParticleEffectScript>(true);
  35. if (particleEffectScript.Length == 0)
  36. {
  37. go.AddComponent<ParticleEffectScript>();
  38. }
  39. }
  40. static TestParticleEffect()
  41. {
  42. EditorApplication.update += Update;
  43. EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
  44. }
  45. private static void Update()
  46. {
  47. if (EditorPrefs.HasKey(RequestTestKey) && !_hasPlayed &&
  48. EditorApplication.isPlaying &&
  49. EditorApplication.isPlayingOrWillChangePlaymode)
  50. {
  51. EditorPrefs.DeleteKey(RequestTestKey);
  52. _hasPlayed = true;
  53. }
  54. }
  55. private static void PlaymodeStateChanged()
  56. {
  57. if (!EditorApplication.isPlaying)
  58. {
  59. _hasPlayed = false;
  60. }
  61. if (isRestart)
  62. {
  63. EditorApplication.isPlaying = true;
  64. isRestart = false;
  65. }
  66. }
  67. }
  68. #endif