FPlayParticleEventInspector.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using UnityEditor;
  3. using Flux;
  4. namespace FluxEditor
  5. {
  6. [CustomEditor(typeof(FPlayParticleEvent))]
  7. public class FPlayParticleEventInspector : FEventInspector {
  8. private FPlayParticleEvent _playParticleEvent = null;
  9. private SerializedProperty _randomSeed = null;
  10. protected override void OnEnable()
  11. {
  12. base.OnEnable();
  13. _playParticleEvent = (FPlayParticleEvent)target;
  14. _randomSeed = serializedObject.FindProperty("_randomSeed");
  15. }
  16. public override void OnInspectorGUI()
  17. {
  18. base.OnInspectorGUI();
  19. serializedObject.Update();
  20. EditorGUILayout.BeginHorizontal();
  21. EditorGUILayout.PropertyField(_randomSeed);
  22. if( GUILayout.Button(new GUIContent("R", "Randomize Seed"), GUILayout.Width(25)) )
  23. _randomSeed.intValue = Random.Range(0, int.MaxValue);
  24. EditorGUILayout.EndHorizontal();
  25. if( _randomSeed.intValue == 0 )
  26. {
  27. EditorGUILayout.BeginHorizontal();
  28. EditorGUILayout.HelpBox("A random seed of 0 means that it always randomizes the particle system, which breaks backwards play of particle systems", MessageType.Warning);
  29. if( GUILayout.Button("Fix", GUILayout.Width(40), GUILayout.Height(40)) )
  30. _randomSeed.intValue = Random.Range(0, int.MaxValue);
  31. EditorGUILayout.EndHorizontal();
  32. }
  33. serializedObject.ApplyModifiedProperties();
  34. ParticleSystem particleSystem = _playParticleEvent.Owner.GetComponentInChildren<ParticleSystem>();
  35. if( particleSystem == null )
  36. {
  37. EditorGUILayout.HelpBox(string.Format("GameObject {0} doesn't have a ParticleSystem attached.", _playParticleEvent.Owner.name), MessageType.Warning);
  38. return;
  39. }
  40. if( _playParticleEvent.LengthTime > particleSystem.main.duration || _playParticleEvent.LengthTime > particleSystem.main.startLifetime.constant )
  41. {
  42. EditorGUILayout.BeginHorizontal();
  43. EditorGUILayout.HelpBox("Particle system's duration and/or startLifetime is smaller than the length of the event, which will make backwards play of the particle system broken.", MessageType.Warning);
  44. if( GUILayout.Button("Fix", GUILayout.Width(40), GUILayout.Height(40)) )
  45. {
  46. int selectedOption = EditorUtility.DisplayDialogComplex("Fix Particle Event?", "Unfortunately, Unity's particle system doesn't fully support playing backwards. " +
  47. "How would you like to correct this?" +
  48. "\n - Increase Particle Duration: make particle duration the same as the length of the event" +
  49. "\n - Shrink Event: resize event to be the length of the particle system", "Increase Particle Duration", "Cancel", "Shrink Event");
  50. switch( selectedOption )
  51. {
  52. case 0: // Increase duration
  53. SerializedObject particleSystemSO = new SerializedObject(particleSystem);
  54. particleSystemSO.FindProperty("lengthInSec").floatValue = _playParticleEvent.LengthTime;
  55. particleSystemSO.FindProperty("InitialModule.startLifetime.scalar").floatValue = _playParticleEvent.LengthTime;
  56. particleSystemSO.ApplyModifiedProperties();
  57. break;
  58. case 1: // Cancel
  59. break;
  60. case 2: // Shrink event
  61. FUtility.Rescale( _playParticleEvent,
  62. new FrameRange(_playParticleEvent.FrameRange.Start,
  63. Mathf.RoundToInt( Mathf.Min(particleSystem.main.duration, particleSystem.main.startLifetime.constant) * _playParticleEvent.Sequence.FrameRate)) );
  64. break;
  65. }
  66. }
  67. EditorGUILayout.EndHorizontal();
  68. }
  69. }
  70. }
  71. }