using UnityEngine; using System.Collections; using AX.TrackRecord; //Author: ZCG public class ParticleSystemControl : MonoBehaviour { private void Start() { switch (gameObject.name) { case "WaterStraight(Clone)": waterStaight = FindChild(this.transform, "Water").GetComponent(); if (this.transform.parent.parent.name != "shuiqiang")//消防车初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 1.5f; waterStraightMinLength = 0.6f; } } else//消防员初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 1f; waterStraightMinLength = 0.4f; } } break; case "WaterFog(Clone)": waterParticle = FindChild(this.transform, "kuai (3)").GetComponent(); fogParticle = FindChild(this.transform, "Particle System").GetComponent(); waterLifetime = waterParticle.startLifetime;//初始化水的存活时间 fogLifetime = fogParticle.startLifetime;//初始化雾的存活时间 if (this.transform.parent.parent.name != "shuiqiang")//消防车初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 1.5f; waterFogMinLength = 0.6f; } } else//消防员初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 0.9f; waterFogMinLength = 0.9f; } } break; case "WaterFlower(Clone)": var flower = FindChild(this.transform, "ParticleChange");// 需要控制的粒子系统放在ParticleChange的物体下 waterFlower = new ParticleSystem[flower.childCount]; //获取所有粒子系统 for (int i = 0; i < waterFlower.Length; i++) waterFlower[i] = flower.GetChild(i).GetComponent(); //获取初始声明周期 waterFlowerLifetimeInit = new float[waterFlower.Length]; for (int i = 0; i < waterFlower.Length; i++) waterFlowerLifetimeInit[i] = waterFlower[i].startLifetime; if (this.transform.parent.parent.name != "shuiqiang")//消防车初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 1.5f; waterFlowerMinLength = 0.6f; } } else//消防员初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 1f; waterFlowerMinLength = 0.4f; } } break; case "Froth(Clone)": frothWaterColumn = FindChild(this.transform, "frothWaterColumn"); //获取水柱物体 frothWaterColumnInit = frothWaterColumn.localScale;//记录水柱初始缩放 var paomo = FindChild(this.transform, "ParticleChange");// 需要控制的粒子系统放在ParticleChange的物体下 froth = new ParticleSystem[paomo.childCount]; //获取所有粒子系统 for (int i = 0; i < froth.Length; i++) froth[i] = paomo.GetChild(i).GetComponent(); //记录初始生命周期 frothLifetimeInit = new float[froth.Length]; for (int i = 0; i < froth.Length; i++) frothLifetimeInit[i] = froth[i].startLifetime; //记录初始速度 frothSpeedInit = new float[froth.Length]; for (int i = 0; i < froth.Length; i++) frothSpeedInit[i] = froth[i].startSpeed; //记录初始大小 frothSizeInit = new float[froth.Length]; for (int i = 0; i < froth.Length; i++) frothSizeInit[i] = froth[i].startSize; //记录初始重力 frothGravityInit = new float[froth.Length]; for (int i = 0; i < froth.Length; i++) frothGravityInit[i] = froth[i].gravityModifier; //记录缩放比例scale FrothScaleInit = new Vector3[froth.Length]; for (int i = 0; i < froth.Length; i++) FrothScaleInit[i] = froth[i].gameObject.transform.localScale; if (this.transform.parent.parent.name != "shuiqiang")//消防车初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 3.5f; frothMixLength = 1f; } } else//消防员初始射程 { if (!LoadManager.Instance.IsPlayBacking) { particleScaleControl = 0.5f; frothMixLength = 0.3f; } } break; case "DryPowder(Clone)": var ganfen = FindChild(this.transform, "ParticleChange");// 需要控制的粒子系统放在ParticleChange的物体下 dryPowder = new ParticleSystem[ganfen.childCount]; //获取所有粒子系统 for (int i = 0; i < dryPowder.Length; i++) dryPowder[i] = ganfen.GetChild(i).GetComponent(); //记录初始重力 dryPowerGravityInit = new float[dryPowder.Length]; for (int i = 0; i < dryPowder.Length; i++) dryPowerGravityInit[i] = dryPowder[i].gravityModifier; //记录初始缩放 dryPowerScaleInit = new Vector3[dryPowder.Length]; for (int i = 0; i < dryPowder.Length; i++) dryPowerScaleInit[i] = dryPowder[i].gameObject.transform.localScale; if (this.transform.parent.parent.name == "shuiqiang")//消防员初始射程 dryPowerMinlength = 1; break; } } //只控制一个字段即可 /// 控制所有粒子系统大小/射程/缩放 public float particleScaleControl = 1; private void Update() { if (gameObject.activeSelf) { #if UNITY_EDITOR if (Input.GetKey(KeyCode.X)) particleScaleControl += 0.01f; if (Input.GetKey(KeyCode.C)) particleScaleControl -= 0.01f; #endif switch (gameObject.name) { case "WaterStraight(Clone)": waterStraightScale = particleScaleControl; WaterStaightControl(); break; case "WaterFog(Clone)": waterFogScale = particleScaleControl; WaterFog(); break; case "WaterFlower(Clone)": waterFlowerScale = particleScaleControl; WaterFlower(); break; case "Froth(Clone)": frothScale = particleScaleControl; Froth(); break; case "DryPowder(Clone)": dryPowerScale = particleScaleControl; DryPowder(); break; } } } #region 控制直流水大小 /// 直流水粒子特效(Water) private ParticleEmitter waterStaight; /// 控制直流水大小的参数 public float waterStraightScale = 1; /// 直流水射程 public float waterStraightLength = 10; /// 直流水初始射程 public float waterStraightMinLength; private float minSize = 0.2f; private float maxSize = 0.4f; private Vector3 localVelocityZ = new Vector3(0f, 0f, 10f); private Vector3 rndVelocity = new Vector3(0.3f, 0.3f, 0.3f); private Vector3 localScale = new Vector3(1f, 1f, 1f); //private Vector3 vector3Temp = new Vector3(0.01f, 0.01f, 0.01f); /// 控制直流水大小 private void WaterStaightControl() { //#if UNITY_EDITOR // if (Input.GetKey(KeyCode.X) && waterStraightScale < waterStraightLength) // waterStraightScale += 0.01f; // if (Input.GetKey(KeyCode.C) && waterStraightScale > 1) // waterStraightScale -= 0.01f; //#endif if (waterStraightScale >= waterStraightMinLength && waterStraightScale < waterStraightLength) { waterStaight.minSize = minSize * waterStraightScale; waterStaight.maxSize = maxSize * waterStraightScale; waterStaight.localVelocity = localVelocityZ * waterStraightScale; waterStaight.rndVelocity = rndVelocity * waterStraightScale; waterStaight.transform.localScale = localScale * waterStraightScale; waterStaight.emit = true; } else { if (particleScaleControl <= waterStraightMinLength) particleScaleControl = waterStraightMinLength; else particleScaleControl = waterStraightLength; } } #endregion #region 控制喷雾水大小 /// 控制喷雾特效的大小 public float waterFogScale = 0.8f; /// 控制喷雾的射程 public float waterFogLength =4f; /// 喷雾初始射程 public float waterFogMinLength; private ParticleSystem waterParticle;//水 private ParticleSystem fogParticle;//雾 private float waterLifetime/* = 0.28f*/;//水的存活时间 private float fogLifetime /*= 1.09f*/;//雾存活时间 /// 控制喷雾大小 private void WaterFog() { //#if UNITY_EDITOR // if (Input.GetKey(KeyCode.X) && waterFogScale < waterFogLength) // waterFogScale += 0.01f; // if (Input.GetKey(KeyCode.C) && waterFogScale > 1) // waterFogScale -= 0.01f; //#endif if (waterFogScale >= waterFogMinLength && waterFogScale < waterFogLength) { waterParticle.startLifetime = waterLifetime * waterFogScale; fogParticle.startLifetime = fogLifetime * waterFogScale; } else { if (particleScaleControl <= waterFogMinLength) particleScaleControl = waterFogMinLength; else particleScaleControl = waterFogLength; } } #endregion #region 控制开花水大小 /// 控制开花特效大小 public float waterFlowerScale = 1; /// 控制开花的射程 public float waterFlowerLength = 4; /// 开花水初始射程 public float waterFlowerMinLength = 1; /// 所有的开花水粒子系统 private ParticleSystem[] waterFlower; /// 记录初始生命周期 private float[] waterFlowerLifetimeInit; /// 控制开花水大小 private void WaterFlower() { //#if UNITY_EDITOR // if (Input.GetKey(KeyCode.X)) // waterFlowerScale += 0.01f; // if (Input.GetKey(KeyCode.C)) // waterFlowerScale -= 0.01f; //#endif if (waterFlowerScale >= waterFlowerMinLength && waterFlowerScale < waterFlowerLength) { for (int i = 0; i < waterFlower.Length; i++) { waterFlower[i].startLifetime = waterFlowerLifetimeInit[i] * waterFlowerScale; } } else { if (particleScaleControl <= waterFlowerMinLength) particleScaleControl = waterFlowerMinLength; else particleScaleControl = waterFlowerLength; } } #endregion #region 控制泡沫大小 /// 所有的泡沫粒子系统 private ParticleSystem[] froth; /// 记录泡沫初始scale比例 private Vector3[] FrothScaleInit; /// 记录粒子初始生命周期系统 private float[] frothLifetimeInit; /// 记录粒子初始速度 private float[] frothSpeedInit; /// 记录粒子初始大小 private float[] frothSizeInit; /// 记录重力初始大小 private float[] frothGravityInit; /// 水柱粒子(单独控制) private Transform frothWaterColumn; /// 记录水柱粒子系统的初始scale比例 private Vector3 frothWaterColumnInit; /// 控制泡沫大小 public float frothScale = 1; /// 泡沫射程 public float frothLength = 10; /// 泡沫初始射程 public float frothMixLength; /// 控制泡沫大小 private void Froth() { //#if UNITY_EDITOR // if (Input.GetKey(KeyCode.X)) // frothScale += 0.01f; // if (Input.GetKey(KeyCode.C)) // frothScale -= 0.01f; //#endif if (frothScale >= frothMixLength && frothScale < frothLength) { //控制泡沫缩放比例 for (int i = 0; i < froth.Length; i++) { // froth[i].startLifetime = frothLifetimeInit[i] * frothScale;//控制生命周期 //froth[i].startSpeed = frothSpeedInit[i] * frothScale;//控制速度 //froth[i].startSize = frothSizeInit[i] * frothScale;//控制大小 froth[i].gravityModifier = frothGravityInit[i] * frothScale * 2 * 2;//控制重力大小 froth[i].transform.localScale = FrothScaleInit[i] * frothScale * 2;//控制transform缩放 } frothWaterColumn.localScale = new Vector3(frothWaterColumnInit.x * frothScale, frothWaterColumnInit.y * frothScale, frothWaterColumnInit.z * frothScale * 2);//控制水柱缩放 } else { if (particleScaleControl <= frothMixLength) particleScaleControl = frothMixLength; else particleScaleControl = frothLength; } } #endregion #region 控制干粉大小 /// 所有的干粉粒子系统 private ParticleSystem[] dryPowder; /// 记录干粉初始scale比例 private Vector3[] dryPowerScaleInit; /// 记录干粉初始重力大小 private float[] dryPowerGravityInit; /// 控制干粉大小 public float dryPowerScale = 1; /// 干粉长度 public float dryPowerLength = 5; /// 干粉初始射程 public float dryPowerMinlength; /// 控制干粉大小 private void DryPowder() { //#if UNITY_EDITOR // if (Input.GetKey(KeyCode.X)) // dryPowerScale += 0.01f; // if (Input.GetKey(KeyCode.C)) // dryPowerScale -= 0.01f; //#endif if (dryPowerScale >= dryPowerMinlength && dryPowerScale < dryPowerLength) { for (int i = 0; i < dryPowder.Length; i++) { dryPowder[i].gravityModifier = dryPowerGravityInit[i] * dryPowerScale * 2;//控制重力大小 dryPowder[i].transform.localScale = dryPowerScaleInit[i] * dryPowerScale * 2;//控制transform缩放 } } else { if (particleScaleControl <= dryPowerMinlength) particleScaleControl = dryPowerMinlength; else particleScaleControl = dryPowerLength; } } #endregion /// 查找子物体 /// 开始查找的位置 /// 子物体名字 /// private Transform FindChild(Transform trans, string goName) { Transform child = trans.Find(goName); if (child != null) return child; Transform go = null; for (int i = 0; i < trans.childCount; i++) { child = trans.GetChild(i); go = FindChild(child, goName); if (go != null) return go; } return null; } }