|
|
using System.Collections.Generic; |
|
|
using UnityEngine; |
|
|
//Author:ZCG |
|
|
//CreatTime:12/8/2017 |
|
|
public class ParticleControl : MonoBehaviour |
|
|
{ |
|
|
[SerializeField] |
|
|
/// <summary>鎵€鏈夌殑绮掑瓙绯荤粺</summary> |
|
|
protected ParticleSystem[] particleAll; |
|
|
[SerializeField] |
|
|
[Range(0, 10)] |
|
|
/// <summary>鎺у埗澶у皬</summary> |
|
|
protected float Scale = 1; |
|
|
[SerializeField] |
|
|
[Range(1f, 10f)] |
|
|
/// <summary>鎺у埗澶у皬</summary> |
|
|
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] |
|
|
[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(); |
|
|
} |
|
|
/// <summary> |
|
|
/// 鍒濆鍖栧弬鏁? |
|
|
/// </summary> |
|
|
protected virtual void Init() |
|
|
{ |
|
|
//鍒濆鍖栧惎鍔ㄥ彉鍖栫殑鍙傛暟鍜屾敼鍙樺洜瀛? |
|
|
} |
|
|
/// <summary> |
|
|
/// 杈撳叆鎺у埗 |
|
|
/// </summary> |
|
|
protected virtual void InputManage() |
|
|
{//娴嬭瘯鐢? |
|
|
#if UNITY_EDITOR |
|
|
//if (Input.GetKey(KeyCode.Equals))//鎹㈡垚鑷繁鐨勮緭鍏ユ柟寮? |
|
|
// Up(); |
|
|
//if (Input.GetKey(KeyCode.Minus)) |
|
|
// Down(); |
|
|
#endif |
|
|
} |
|
|
/// <summary> |
|
|
/// 澶? |
|
|
/// </summary> |
|
|
protected virtual void Up(float weight) |
|
|
{ |
|
|
Scale += ScaleSpeed * weight; |
|
|
ParticleScaleControl(); |
|
|
} |
|
|
/// <summary> |
|
|
/// 灏? |
|
|
/// </summary> |
|
|
protected virtual void Down(float weight) |
|
|
{ |
|
|
Scale -= ScaleSpeed * weight; |
|
|
ParticleScaleControl(); |
|
|
} |
|
|
/// <summary> |
|
|
/// 璁剧疆绮掑瓙澶у皬 |
|
|
/// </summary> |
|
|
/// <param name="value"></param> |
|
|
protected virtual void SetScale(float value) |
|
|
{ |
|
|
Scale = value; |
|
|
ParticleScaleControl(); |
|
|
} |
|
|
/// <summary> |
|
|
/// 鍒濆鍊? |
|
|
/// </summary> |
|
|
protected ParticleData[] particleInitValus; |
|
|
/// <summary> |
|
|
/// 鍒濆鍖栬褰曟墍鏈夌矑瀛愮郴缁熺殑鏁板€? |
|
|
/// </summary> |
|
|
private void InitValue() |
|
|
{ |
|
|
particleInitValus = new ParticleData[particleAll.Length];//澹版槑鏁扮粍闀垮害锛堣繕闇€瑕佸姣忎釜鍗曠嫭鐨勫厓绱爊ew涓€娆★級 |
|
|
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;//璁板綍涓婁竴娆$缉鏀炬暟鍊? |
|
|
/// <summary> |
|
|
/// 绮掑瓙鎺у埗 |
|
|
/// </summary> |
|
|
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鏂规硶鎵ц鏃讹紝鎵ц瀹屾病鏈夌珛鍗虫敼鍙榠sPlaying鍜宨sStoped灞炴€у€硷紝瀵艰嚧鍙兘鏌愪釜瀛愮矑瀛愮壒鏁堟垨鍏ㄩ儴鐨刬sPlaying杩樹负true |
|
|
//浠庤€岄€犳垚鍐嶆墦寮€绮掑瓙绯荤粺鏃讹紙鍙婁笅闈㈢殑else閫昏緫锛夛紝涓嶈兘姝e父鎵撳紑 |
|
|
//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;//鍏跺疄鏀瑰彉鐨勫氨鏄痗onstantMax绛夊悓浜巑ain.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; |
|
|
} |
|
|
} |
|
|
} |
|
|
/// <summary>鑾峰彇鎵€鏈夌殑绮掑瓙绯荤粺</summary> |
|
|
/// <param name="parent">鐖剁墿浣撲綅缃?/param> |
|
|
/// <returns>鎵€鏈夋墍鏈夊寘鎷湰韬矑瀛愭暟缁?/returns> |
|
|
private ParticleSystem[] GetParticleAll(Transform parent) |
|
|
{ |
|
|
List<ParticleSystem> particles = new List<ParticleSystem>(); |
|
|
if (this.GetComponent<ParticleSystem>()) |
|
|
particles.Add(this.GetComponent<ParticleSystem>()); |
|
|
for (int i = 0; i < parent.childCount; i++) |
|
|
{ |
|
|
var child = parent.GetChild(i); |
|
|
if (child.GetComponent<ParticleSystem>()) |
|
|
{ |
|
|
particles.Add(child.GetComponent<ParticleSystem>()); |
|
|
} |
|
|
} |
|
|
return particles.ToArray(); |
|
|
} |
|
|
}
|
|
|
|