C# 统一:将粒子系统更改为池
我试图将粒子系统函数更改为池,而不是每次实例化。我想重复使用粒子。这怎么可能?我不知道如何开始,虽然我看了unity教程,但不知为什么它仍然不清楚。也许是因为我在其他类中调用了粒子系统函数,这让我有些困惑C# 统一:将粒子系统更改为池,c#,unity3d,particle-system,C#,Unity3d,Particle System,我试图将粒子系统函数更改为池,而不是每次实例化。我想重复使用粒子。这怎么可能?我不知道如何开始,虽然我看了unity教程,但不知为什么它仍然不清楚。也许是因为我在其他类中调用了粒子系统函数,这让我有些困惑 public ParticleSystem[] Myeffects; public void Particle(int particleNum, Vector3 Pos) { if (Myeffects != null && Myeffects[particleNu
public ParticleSystem[] Myeffects;
public void Particle(int particleNum, Vector3 Pos)
{
if (Myeffects != null && Myeffects[particleNumber] != null )
{
if (Myeffects[particleNumber].isPlaying)
Myeffects[particleNumber].Stop();
ParticleSystem temp = Instantiate(Myeffects[particleNum], particlePos, new Quaternion()) as ParticleSystem;
temp.Play();
}
}
}
首先,选择粒子系统预设/TNT,然后确保未选中“唤醒时播放”。下面的池脚本是实现这一点的专用技巧。它将创建一个指定的ParticleSystem
数组,然后重新使用它们。请注意,ParticlePool
不会继承自monobhavior
,因此请确保直接复制它
using UnityEngine;
using System.Collections;
using System;
public class ParticlePool
{
int particleAmount;
ParticleSystem[] NormalParticle;
ParticleSystem[] TNTParticle;
public ParticlePool(ParticleSystem normalPartPrefab, ParticleSystem tntPartPrefab, int amount = 10)
{
particleAmount = amount;
NormalParticle = new ParticleSystem[particleAmount];
TNTParticle = new ParticleSystem[particleAmount];
for (int i = 0; i < particleAmount; i++)
{
//Instantiate 10 NormalParticle
NormalParticle[i] = GameObject.Instantiate(normalPartPrefab, new Vector3(0, 0, 0), new Quaternion()) as ParticleSystem;
//Instantiate 10 TNTParticle
TNTParticle[i] = GameObject.Instantiate(tntPartPrefab, new Vector3(0, 0, 0), new Quaternion()) as ParticleSystem;
}
}
//Returns available GameObject
public ParticleSystem getAvailabeParticle(int particleType)
{
ParticleSystem firstObject = null;
//Normal crate
if (particleType == 0)
{
//Get the first GameObject
firstObject = NormalParticle[0];
//Move everything Up by one
shiftUp(0);
}
//TNT crate
else if (particleType == 1)
{
//Get the first GameObject
firstObject = TNTParticle[0];
//Move everything Up by one
shiftUp(1);
}
return firstObject;
}
//Returns How much GameObject in the Array
public int getAmount()
{
return particleAmount;
}
//Moves the GameObject Up by 1 and moves the first one to the last one
private void shiftUp(int particleType)
{
//Get first GameObject
ParticleSystem firstObject;
//Normal crate
if (particleType == 0)
{
firstObject = NormalParticle[0];
//Shift the GameObjects Up by 1
Array.Copy(NormalParticle, 1, NormalParticle, 0, NormalParticle.Length - 1);
//(First one is left out)Now Put first GameObject to the Last one
NormalParticle[NormalParticle.Length - 1] = firstObject;
}
//TNT crate
else if (particleType == 1)
{
firstObject = TNTParticle[0];
//Shift the GameObjects Up by 1
Array.Copy(TNTParticle, 1, TNTParticle, 0, TNTParticle.Length - 1);
//(First one is left out)Now Put first GameObject to the Last one
TNTParticle[TNTParticle.Length - 1] = firstObject;
}
}
}
你想要你的粒子;但是你的代码是实例化粒子系统,而不是粒子本身。这会有什么影响?只需复制教程中的脚本,而不是使用,这就是iThanks@Absiness的全部功能。我会尝试一下。你是说10种不同类型的粒子?我以为只有两种类型?是的,我说过实例化10。经过更多的测试,我决定只实例化5个。你可以看到我在哪里做的
particlePool=newparticlepool(效果[0],效果[1],5)代码>。五个就够了。它将重复使用这5个。你不需要更多了。测试一下,自己看看。阅读评论。这是非常值得评论的。它不会像Unity那样禁用和启用游戏对象(这也是很昂贵的),而是在调用GetAvailableParticle
时拾取阵列中的第一个游戏对象,然后将其移动到最后一个阵列索引。基本上,每次调用getAvailableParticle
时都会使用不同的游戏对象。它会不断地移动它们,以确保当前粒子有足够的时间来完成播放。这种类型的池对粒子有好处。看这里。我首先用它来回答一个问题。场景中只有10个粒子。正常颗粒为5,tnt颗粒为5。这没什么错。假设你有100多个粒子,那么禁用它们是值得的。你很好。
public class ParticleHolder : MonoBehaviour
{
public ParticleSystem[] effects;
ParticlePool particlePool;
void Start()
{
// 0 = Normal crate
// 1 = TNT crate
particlePool = new ParticlePool(effects[0], effects[1], 5);
}
public void playParticle(int particleType, Vector3 particlePos)
{
ParticleSystem particleToPlay = particlePool.getAvailabeParticle(particleType);
if (particleToPlay != null)
{
if (particleToPlay.isPlaying)
particleToPlay.Stop();
particleToPlay.transform.position = particlePos;
particleToPlay.Play();
}
}
}