119 lines
3.4 KiB
C#
119 lines
3.4 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class AttachGameObjectsToParticles : MonoBehaviour
|
|||
|
{
|
|||
|
public ParticleSystem LeftPar;
|
|||
|
public ParticleSystem CenterPar;
|
|||
|
public ParticleSystem RightPar;
|
|||
|
|
|||
|
//[Header("粒子数量限制")]
|
|||
|
//public Vector2Int EmissionClamp;
|
|||
|
|
|||
|
//[Header("粒子大小限制")]
|
|||
|
//public Vector2 SizeClamp;
|
|||
|
|
|||
|
[Header("粒子生成区间设置")]
|
|||
|
public List<ParticleStep> ParticleSteps; // 自定义阶梯区间列表
|
|||
|
private int BeforeCount;//之前的粒子数量
|
|||
|
private void Start()
|
|||
|
{
|
|||
|
GameProcedureManager.Instance.onFaithCountChange += CreatCenterParticle;
|
|||
|
}
|
|||
|
|
|||
|
public void OnEnable()
|
|||
|
{
|
|||
|
SetParticleEmission(CenterPar, 20); // 使用默认值
|
|||
|
CenterPar.Play();
|
|||
|
}
|
|||
|
|
|||
|
public void OnDisable()
|
|||
|
{
|
|||
|
CenterPar.Stop();
|
|||
|
}
|
|||
|
|
|||
|
// 设置发射粒子数量
|
|||
|
private void SetParticleEmission(ParticleSystem particleSystem, int particleCount)
|
|||
|
{
|
|||
|
var emission = particleSystem.emission;
|
|||
|
emission.rateOverTime = particleCount;
|
|||
|
}
|
|||
|
|
|||
|
// 设置粒子大小
|
|||
|
private void SetParticleSize(ParticleSystem particleSystem, float size)
|
|||
|
{
|
|||
|
var mainModule = particleSystem.main;
|
|||
|
mainModule.startSize = size;
|
|||
|
}
|
|||
|
|
|||
|
private void SetParticleSpeed(ParticleSystem particleSystem, float speed)
|
|||
|
{
|
|||
|
var mainModule = particleSystem.main;
|
|||
|
mainModule.startSpeed = speed;
|
|||
|
}
|
|||
|
|
|||
|
// 基于传入的Count来计算粒子数量和大小,按照区间规则阶梯式增长
|
|||
|
public void CreatCenterParticle(int Count)
|
|||
|
{
|
|||
|
|
|||
|
if (Count<=BeforeCount)//小于或者等于之前数值
|
|||
|
{
|
|||
|
Debug.LogWarning("小于" + BeforeCount + "---" + Count);
|
|||
|
BeforeCount = Count;
|
|||
|
return;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Count-=BeforeCount;
|
|||
|
}
|
|||
|
|
|||
|
ParticleSystem particle = CenterPar;
|
|||
|
|
|||
|
// 找到当前Count所属的阶梯
|
|||
|
ParticleStep currentStep = ParticleSteps.Find(step => Count >= step.MinCount && Count < step.MaxCount);
|
|||
|
|
|||
|
if (currentStep == null)
|
|||
|
{
|
|||
|
Debug.LogWarning("Count 超出了定义的阶梯范围。");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// 按照区间中的比例计算粒子大小和数量
|
|||
|
float stepFactor = Mathf.InverseLerp(currentStep.MinCount, currentStep.MaxCount, Count);
|
|||
|
float Size = Mathf.Lerp(currentStep.MinSize, currentStep.MaxSize, stepFactor);
|
|||
|
int Emission = Mathf.FloorToInt(Mathf.Lerp(currentStep.MinEmission, currentStep.MaxEmission, stepFactor));
|
|||
|
|
|||
|
// 设置粒子速度
|
|||
|
float speed = Size * 15f;
|
|||
|
|
|||
|
Debug.LogWarning("粒子大小: " + Size + " -- 粒子数量: " + Emission);
|
|||
|
|
|||
|
// 应用设置
|
|||
|
SetParticleEmission(particle, Emission);
|
|||
|
SetParticleSize(particle, Size);
|
|||
|
SetParticleSpeed(particle, speed);
|
|||
|
particle.Play();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[System.Serializable]
|
|||
|
public class ParticleStep
|
|||
|
{
|
|||
|
public int MinCount;
|
|||
|
public int MaxCount;
|
|||
|
public float MinSize;
|
|||
|
public float MaxSize;
|
|||
|
public int MinEmission;
|
|||
|
public int MaxEmission;
|
|||
|
|
|||
|
public ParticleStep(int minCount, int maxCount, float minSize, float maxSize, int minEmission, int maxEmission)
|
|||
|
{
|
|||
|
MinCount = minCount;
|
|||
|
MaxCount = maxCount;
|
|||
|
MinSize = minSize;
|
|||
|
MaxSize = maxSize;
|
|||
|
MinEmission = minEmission;
|
|||
|
MaxEmission = maxEmission;
|
|||
|
}
|
|||
|
}
|