using System.Collections.Generic; using UnityEngine; //Author:ZCG //CreatTime:12/8/2017 public class ParticleControl1 : MonoBehaviour { [SerializeField] /// 所有的粒子系统 protected ParticleSystem[] particleAll; [SerializeField] [Range(0, 10)] /// 控制大小 protected float Scale = 1; [SerializeField] [Range(1f, 10f)] /// 控制大小 protected float ScaleSpeed = 1f; //scale限制 [SerializeField] protected float scaleMin = 0; [SerializeField] protected float scaleMax = 10; //启动变化的参数 [SerializeField] protected bool UseLocaleScale = false; [SerializeField] protected bool UseThisLocaleScale = false; [SerializeField] protected bool UseGravityModifier = true; [SerializeField] protected bool UseSize = true; [SerializeField] protected bool UseStartLifetime = true; [SerializeField] protected bool UseSpeed = true; [SerializeField] protected bool UseRotation = true; [SerializeField] protected bool UseRateOverTime = false; [SerializeField] protected bool UseLocationPosition = false; //改变因子 [SerializeField] [Range(0, 10)] protected float divisorLocaleScale = 1; [SerializeField] [Range(0, 10)] protected float divisorGravityModifier = 1; [SerializeField] [Range(0, 10)] protected float divisorSize = 1; [SerializeField] [Range(0, 10)] protected float divisorStartLifetime = 1; [SerializeField] [Range(0, 10)] protected float divisorSpeed = 1; [SerializeField] [Range(0, 10)] protected float divisorRotation = 1; [SerializeField] [Range(0, 10)] protected float divisorRateOverTime = 1; private void Awake() { Init(); if (particleAll.Length == 0) particleAll = GetParticleAll(this.transform); InitValue(); ParticleScaleControl(); } //private void Start() //{ //} private void Update() { //InputManage(); } /// /// 初始化参数 /// protected virtual void Init() { //初始化启动变化的参数和改变因子 } /// /// 输入控制 /// protected virtual void InputManage() {//测试用 #if UNITY_EDITOR if (Input.GetKey(KeyCode.Equals))//换成自己的输入方式 Up(); if (Input.GetKey(KeyCode.Minus)) Down(); #endif } /// /// 大 /// protected virtual void Up() { Scale += ScaleSpeed * Time.deltaTime; ParticleScaleControl(); } /// /// 小 /// protected virtual void Down() { Scale -= ScaleSpeed * Time.deltaTime; ParticleScaleControl(); } /// /// 设置粒子大小 /// /// protected virtual void SetScale(float value) { Scale = value; ParticleScaleControl(); } /// /// 初始值 /// protected ParticleData[] particleInitValus; /// /// 初始化记录所有粒子系统的数值 /// private void InitValue() { particleInitValus = new ParticleData[particleAll.Length];//声明数组长度(还需要对每个单独的元素new一次) for (int i = 0; i < particleAll.Length; i++) { particleInitValus[i] = new ParticleData();//每个元素new一次才能使用 //缩放 particleInitValus[i].scale = particleAll[i].transform.localScale; //重力 var gravityModifier = particleAll[i].main.gravityModifier; if (gravityModifier.mode == ParticleSystemCurveMode.TwoConstants) { particleInitValus[i].gravityModifier_constantMin = gravityModifier.constantMin; particleInitValus[i].gravityModifier_constantMax = gravityModifier.constantMax; } else if (gravityModifier.mode == ParticleSystemCurveMode.Constant) particleInitValus[i].gravityModifier_constant = gravityModifier.constant; //大小 var startSize = particleAll[i].main.startSize; if (startSize.mode == ParticleSystemCurveMode.TwoConstants) { particleInitValus[i].size_constantMin = startSize.constantMin; particleInitValus[i].size_constantMax = startSize.constantMax; } else if(startSize.mode == ParticleSystemCurveMode.Constant) particleInitValus[i].size_constant = startSize.constant; //生命周期 var startLifetime = particleAll[i].main.startLifetime; if (startLifetime.mode == ParticleSystemCurveMode.TwoConstants) { particleInitValus[i].startLifetime_constantMin = startLifetime.constantMin; particleInitValus[i].startLifetime_constantMax = startLifetime.constantMax; } else if (startLifetime.mode == ParticleSystemCurveMode.Constant) particleInitValus[i].startLifetime_constant = startLifetime.constant; //速度 var startSpeed = particleAll[i].main.startSpeed; if(startSpeed.mode== ParticleSystemCurveMode.TwoConstants) { particleInitValus[i].speed_constantMin = startSpeed.constantMin; particleInitValus[i].speed_constantMax = startSpeed.constantMax; } else if(startSpeed.mode == ParticleSystemCurveMode.Constant) particleInitValus[i].speed_constant = startSpeed.constant; //旋转 var startRotation = particleAll[i].main.startRotation; if(startRotation.mode== ParticleSystemCurveMode.TwoConstants) { particleInitValus[i].rotation_constantMin = startRotation.constantMin; particleInitValus[i].rotation_constantMax = startRotation.constantMax; } else if(startRotation.mode == ParticleSystemCurveMode.Constant) particleInitValus[i].rotation_constant = startRotation.constant; //发射数量 var rate = particleAll[i].emission.rateOverTime; if (rate.mode == ParticleSystemCurveMode.TwoConstants) { particleInitValus[i].rate_constantMin = rate.constantMin; particleInitValus[i].rate_constantMax = rate.constantMax; } else if (rate.mode == ParticleSystemCurveMode.Constant) particleInitValus[i].rate_constant = rate.constant; //周期内粒子最大发射数量 particleInitValus[i].maxParticles = particleAll[i].main.maxParticles; } } private float oldScale;//记录上一次缩放数值 /// /// 粒子控制 /// protected void ParticleScaleControl() { Scale = Mathf.Clamp(Scale, scaleMin, scaleMax); if(Scale == scaleMin) { for (int i = 0; i < particleAll.Length; i++) { if (particleAll[i].isPlaying) { //Stop方法执行时,执行完没有立即改变isPlaying和isStoped属性值,导致可能某个子粒子特效或全部的isPlaying还为true //从而造成再打开粒子系统时(及下面的else逻辑),不能正常打开 //particleAll[i].Stop(); //Debug.Log("Stop:" + particleAll[i].name + particleAll[i].isPlaying); } } } else { for (int i = 0; i < particleAll.Length; i++) { if (!particleAll[i].isPlaying) { //particleAll[i].Play(); //Debug.Log("Play:" + particleAll[i].name); } } } if (Scale==0 || Scale == oldScale) return; oldScale = Scale; for (int i = 0; i < particleAll.Length; i++) { if (UseLocaleScale)//缩放 { if (!UseThisLocaleScale)//启用自身的缩放 { if (particleAll[i].gameObject.name != this.name) particleAll[i].transform.localScale = particleInitValus[i].scale * Scale * divisorLocaleScale; } else particleAll[i].transform.localScale = particleInitValus[i].scale * Scale * divisorLocaleScale; } if (UseGravityModifier)//重力 { //particleAll[i].gravityModifier *= Scale; var main = particleAll[i].main; var mainModule = main.gravityModifier; if (mainModule.mode == ParticleSystemCurveMode.TwoConstants) { //mainModule.constant *= Scale;//其实改变的就是constantMax等同于main.startSizeMultiplier mainModule.constantMin = particleInitValus[i].gravityModifier_constantMin * Scale * divisorGravityModifier; mainModule.constantMax = particleInitValus[i].gravityModifier_constantMax * Scale * divisorGravityModifier; } else if (mainModule.mode == ParticleSystemCurveMode.Constant) mainModule.constant = particleInitValus[i].gravityModifier_constant * Scale * divisorGravityModifier; main.gravityModifier = mainModule; } if (UseSize)//大小 { var main = particleAll[i].main; var mainModule = main.startSize; if (mainModule.mode == ParticleSystemCurveMode.TwoConstants) { mainModule.constantMin = particleInitValus[i].size_constantMin * Scale * divisorSize; mainModule.constantMax = particleInitValus[i].size_constantMax * Scale * divisorSize; } else if (mainModule.mode == ParticleSystemCurveMode.Constant) mainModule.constant = particleInitValus[i].size_constant * Scale * divisorSize; main.startSize = mainModule; } if (UseStartLifetime)//生命周期 { var main = particleAll[i].main; var mainModule = main.startLifetime; if (mainModule.mode == ParticleSystemCurveMode.TwoConstants) { mainModule.constantMin = particleInitValus[i].startLifetime_constantMin * Scale * divisorStartLifetime; mainModule.constantMax = particleInitValus[i].startLifetime_constantMax * Scale * divisorStartLifetime; } else if (mainModule.mode == ParticleSystemCurveMode.Constant) mainModule.constant = particleInitValus[i].startLifetime_constant * Scale * divisorStartLifetime; main.startLifetime = mainModule; } if (UseSpeed)//速度 { var main = particleAll[i].main; var mainModule = main.startSpeed; if (mainModule.mode == ParticleSystemCurveMode.TwoConstants) { mainModule.constantMin = particleInitValus[i].speed_constantMin * Scale * divisorSpeed; mainModule.constantMax = particleInitValus[i].speed_constantMax * Scale * divisorSpeed; } else if (mainModule.mode == ParticleSystemCurveMode.Constant) mainModule.constant = particleInitValus[i].speed_constant * Scale * divisorSpeed; main.startSpeed = mainModule; } if (UseRotation)//旋转 { var main = particleAll[i].main; var mainModule = main.startRotation; if (mainModule.mode == ParticleSystemCurveMode.TwoConstants) { mainModule.constantMin = particleInitValus[i].rotation_constantMin * Scale * divisorRotation; mainModule.constantMax = particleInitValus[i].rotation_constantMax * Scale * divisorRotation; } else if (mainModule.mode == ParticleSystemCurveMode.Constant) mainModule.constant = particleInitValus[i].rotation_constant * Scale * divisorRotation; main.startRotation = mainModule; } if (UseRateOverTime)//粒子数量 {//设置最大数量 var main = particleAll[i].main; main.maxParticles = (int)(particleInitValus[i].maxParticles * Scale * divisorRateOverTime); //发射数量 var emission = particleAll[i].emission; var rateMode = emission.rateOverTime; if (rateMode.mode == ParticleSystemCurveMode.TwoConstants) { rateMode.constantMin = particleInitValus[i].rate_constantMin * Scale * divisorRateOverTime; rateMode.constantMax = particleInitValus[i].rate_constantMax * Scale * divisorRateOverTime; } else if (rateMode.mode == ParticleSystemCurveMode.Constant) rateMode.constant = particleInitValus[i].rate_constant * Scale * divisorRateOverTime; emission.rateOverTime = rateMode; } if (UseLocationPosition)//粒子位置 { transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y,Scale*0.15f); } } } /// 获取所有的粒子系统 /// 父物体位置 /// 所有所有包括本身粒子数组 private ParticleSystem[] GetParticleAll(Transform parent) { List particles = new List(); if (this.GetComponent()) particles.Add(this.GetComponent()); for (int i = 0; i < parent.childCount; i++) { var child = parent.GetChild(i); if (child.GetComponent()) { particles.Add(child.GetComponent()); } } return particles.ToArray(); } }