2016-07-09 26 views
1

我試圖將我的粒子系統函數更改爲合併,而不是每次都實例化。我想重複使用這些粒子。這怎麼可能?我不知道如何開始,雖然我看到了統一教程,但不知何故目前還不清楚。也許是因爲即時通訊在其他類中調用粒子系統函數會讓我感到困惑。所有的統一:將粒子系統更改爲合併

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(); 
    } 
} 
} 
+0

你想喲重用粒子;但你的代碼正在實例化particle **系統**,而不是粒子本身。 – AgentFire

+0

它是如何影響的? – John

+0

只需從教程中複製腳本,而不是使用使用,這就是它的全部內容 – Absinthe

回答

2

首先,選擇您粒子系統預製/ TNT然後確保玩清醒沒有被選中。下面的彙集腳本專門用來實現這一點。它將創建一個指定的ParticleSystem數組然後重新使用它們。請注意,ParticlePool確實是不是繼承自MonoBehaviour,所以請確保直接複製它。

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; 
     } 
    } 
} 

然後,您的ParticleHolder腳本應更新下面的代碼。而已。沒有更多的實例。

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(); 
     } 

    } 
} 
+0

您是指10種不同類型的粒子?我以爲只有兩種類型? – Programmer

+0

是的,我確實說實例化10.經過更多的測試,我決定只實例化5.你可以看到我在哪裏做了'particlePool = new ParticlePool(effects [0],effects [1],5);'。這五個就夠了。它將重新使用這些5.你不需要更多。測試一下,看看你自己。 – Programmer

+0

會做和更新你。謝謝^^ – John