F3DParticleScale.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using System.Collections;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. [ExecuteInEditMode]
  7. public class F3DParticleScale : MonoBehaviour
  8. {
  9. [Range(0f, 20f)]
  10. public float ParticleScale = 1.0f; // Particle scale
  11. public bool ScaleGameobject = true; // Should the game be scaled as well
  12. float prevScale; // Previous scale
  13. void Start()
  14. {
  15. // Store previous scale
  16. prevScale = ParticleScale;
  17. }
  18. // Scale Shuriken particle system
  19. void ScaleShurikenSystems(float scaleFactor)
  20. {
  21. #if UNITY_EDITOR
  22. ParticleSystem[] systems = GetComponentsInChildren<ParticleSystem>();
  23. foreach (ParticleSystem system in systems)
  24. {
  25. system.startSpeed *= scaleFactor;
  26. system.startSize *= scaleFactor;
  27. system.gravityModifier *= scaleFactor;
  28. SerializedObject so = new SerializedObject(system);
  29. so.FindProperty("VelocityModule.x.scalar").floatValue *= scaleFactor;
  30. so.FindProperty("VelocityModule.y.scalar").floatValue *= scaleFactor;
  31. so.FindProperty("VelocityModule.z.scalar").floatValue *= scaleFactor;
  32. so.FindProperty("ClampVelocityModule.magnitude.scalar").floatValue *= scaleFactor;
  33. so.FindProperty("ClampVelocityModule.x.scalar").floatValue *= scaleFactor;
  34. so.FindProperty("ClampVelocityModule.y.scalar").floatValue *= scaleFactor;
  35. so.FindProperty("ClampVelocityModule.z.scalar").floatValue *= scaleFactor;
  36. so.FindProperty("ForceModule.x.scalar").floatValue *= scaleFactor;
  37. so.FindProperty("ForceModule.y.scalar").floatValue *= scaleFactor;
  38. so.FindProperty("ForceModule.z.scalar").floatValue *= scaleFactor;
  39. so.FindProperty("ColorBySpeedModule.range").vector2Value *= scaleFactor;
  40. so.FindProperty("SizeBySpeedModule.range").vector2Value *= scaleFactor;
  41. so.FindProperty("RotationBySpeedModule.range").vector2Value *= scaleFactor;
  42. so.ApplyModifiedProperties();
  43. }
  44. #endif
  45. }
  46. // Scale trail renderer
  47. void ScaleTrailRenderers(float scaleFactor)
  48. {
  49. TrailRenderer[] trails = GetComponentsInChildren<TrailRenderer>();
  50. foreach (TrailRenderer trail in trails)
  51. {
  52. trail.startWidth *= scaleFactor;
  53. trail.endWidth *= scaleFactor;
  54. }
  55. }
  56. void Update()
  57. {
  58. #if UNITY_EDITOR
  59. if (prevScale != ParticleScale && ParticleScale > 0)
  60. {
  61. if (ScaleGameobject)
  62. transform.localScale =
  63. new Vector3(ParticleScale, ParticleScale, ParticleScale);
  64. float scaleFactor = ParticleScale / prevScale;
  65. ScaleShurikenSystems(scaleFactor);
  66. ScaleTrailRenderers(scaleFactor);
  67. prevScale = ParticleScale;
  68. }
  69. #endif
  70. }
  71. }