ParticleLights.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. // =================================
  8. // Define namespace.
  9. // =================================
  10. namespace MirzaBeig
  11. {
  12. namespace Scripting
  13. {
  14. namespace Effects
  15. {
  16. // =================================
  17. // Classes.
  18. // =================================
  19. //[ExecuteInEditMode]
  20. [System.Serializable]
  21. [RequireComponent(typeof(ParticleSystem))]
  22. public class ParticleLights : MonoBehaviour
  23. {
  24. // =================================
  25. // Nested classes and structures.
  26. // =================================
  27. // ...
  28. // =================================
  29. // Variables.
  30. // =================================
  31. // ...
  32. ParticleSystem ps;
  33. List<Light> lights = new List<Light>();
  34. //public LightType type = LightType.Point;
  35. public float scale = 2.0f;
  36. [Range(0.0f, 8.0f)]
  37. public float intensity = 8.0f;
  38. public Color colour = Color.white;
  39. [Range(0.0f, 1.0f)]
  40. public float colourFromParticle = 1.0f;
  41. public LightShadows shadows = LightShadows.None;
  42. GameObject template;
  43. // =================================
  44. // Functions.
  45. // =================================
  46. // ...
  47. void Awake()
  48. {
  49. }
  50. // ...
  51. void Start()
  52. {
  53. ps = GetComponent<ParticleSystem>();
  54. template = new GameObject();
  55. template.transform.SetParent(transform);
  56. template.name = "Template";
  57. }
  58. // ...
  59. void Update()
  60. {
  61. }
  62. // ...
  63. void LateUpdate()
  64. {
  65. ParticleSystem.Particle[] particles =
  66. new ParticleSystem.Particle[ps.particleCount];
  67. int numOfParticles = ps.GetParticles(particles);
  68. if (lights.Count != numOfParticles)
  69. {
  70. for (int i = 0; i < lights.Count; i++)
  71. {
  72. Destroy(lights[i].gameObject);
  73. }
  74. lights.Clear();
  75. for (int i = 0; i < numOfParticles; i++)
  76. {
  77. GameObject go = Instantiate(template, transform) as GameObject;
  78. go.name = "- " + (i + 1).ToString();
  79. lights.Add(go.AddComponent<Light>());
  80. }
  81. }
  82. ParticleSystem.MainModule m = ps.main;
  83. bool worldSpace = m.simulationSpace == ParticleSystemSimulationSpace.World;
  84. for (int i = 0; i < numOfParticles; i++)
  85. {
  86. ParticleSystem.Particle p = particles[i];
  87. Light light = lights[i];
  88. //light.type = type;
  89. //if (type == LightType.Spot)
  90. //{
  91. // light.transform.rotation = Quaternion.Euler(p.rotation3D);
  92. //}
  93. light.range = p.GetCurrentSize(ps) * scale;
  94. light.color = Color.Lerp(colour, p.GetCurrentColor(ps), colourFromParticle);
  95. light.intensity = intensity;
  96. light.shadows = shadows;
  97. light.transform.position = worldSpace ? p.position : ps.transform.TransformPoint(p.position);
  98. }
  99. }
  100. // =================================
  101. // End functions.
  102. // =================================
  103. }
  104. // =================================
  105. // End namespace.
  106. // =================================
  107. }
  108. }
  109. }
  110. // =================================
  111. // --END-- //
  112. // =================================