123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
-
- // =================================
- // Namespaces.
- // =================================
- using UnityEngine;
- using System.Collections.Generic;
- // =================================
- // Define namespace.
- // =================================
- namespace MirzaBeig
- {
- namespace Scripting
- {
- namespace Effects
- {
- // =================================
- // Classes.
- // =================================
- //[ExecuteInEditMode]
- [System.Serializable]
- [RequireComponent(typeof(ParticleSystem))]
- public class ParticleLights : MonoBehaviour
- {
- // =================================
- // Nested classes and structures.
- // =================================
- // ...
- // =================================
- // Variables.
- // =================================
- // ...
- ParticleSystem ps;
- List<Light> lights = new List<Light>();
- //public LightType type = LightType.Point;
- public float scale = 2.0f;
- [Range(0.0f, 8.0f)]
- public float intensity = 8.0f;
- public Color colour = Color.white;
- [Range(0.0f, 1.0f)]
- public float colourFromParticle = 1.0f;
- public LightShadows shadows = LightShadows.None;
- GameObject template;
- // =================================
- // Functions.
- // =================================
- // ...
- void Awake()
- {
- }
- // ...
- void Start()
- {
- ps = GetComponent<ParticleSystem>();
- template = new GameObject();
- template.transform.SetParent(transform);
- template.name = "Template";
- }
- // ...
- void Update()
- {
- }
- // ...
- void LateUpdate()
- {
- ParticleSystem.Particle[] particles =
- new ParticleSystem.Particle[ps.particleCount];
- int numOfParticles = ps.GetParticles(particles);
- if (lights.Count != numOfParticles)
- {
- for (int i = 0; i < lights.Count; i++)
- {
- Destroy(lights[i].gameObject);
- }
- lights.Clear();
- for (int i = 0; i < numOfParticles; i++)
- {
- GameObject go = Instantiate(template, transform) as GameObject;
- go.name = "- " + (i + 1).ToString();
- lights.Add(go.AddComponent<Light>());
- }
- }
- ParticleSystem.MainModule m = ps.main;
- bool worldSpace = m.simulationSpace == ParticleSystemSimulationSpace.World;
- for (int i = 0; i < numOfParticles; i++)
- {
- ParticleSystem.Particle p = particles[i];
- Light light = lights[i];
- //light.type = type;
- //if (type == LightType.Spot)
- //{
- // light.transform.rotation = Quaternion.Euler(p.rotation3D);
- //}
- light.range = p.GetCurrentSize(ps) * scale;
- light.color = Color.Lerp(colour, p.GetCurrentColor(ps), colourFromParticle);
- light.intensity = intensity;
- light.shadows = shadows;
- light.transform.position = worldSpace ? p.position : ps.transform.TransformPoint(p.position);
- }
- }
- // =================================
- // End functions.
- // =================================
- }
- // =================================
- // End namespace.
- // =================================
- }
- }
- }
- // =================================
- // --END-- //
- // =================================
|