贵港中石油多角色网上演练
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

352 lines
14 KiB

using System.Collections.Generic;
using UnityEngine;
//Author:ZCG
//CreatTime:12/8/2017
public class ParticleControl1 : 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]
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();
}
/// <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()
{
Scale += ScaleSpeed * Time.deltaTime;
ParticleScaleControl();
}
/// <summary>
/// 小
/// </summary>
protected virtual void Down()
{
Scale -= ScaleSpeed * Time.deltaTime;
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];//声明数组长度(还需要对每个单独的元素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;//记录上一次缩放数值
/// <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方法执行时,执行完没有立即改变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);
}
}
}
/// <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();
}
}