2016-07-09 3 views
1

Ich versuche, meine Partikelsystemfunktion in ein Pooling zu ändern, anstatt jedes Mal zu instanziieren. Ich möchte die Partikel wiederverwenden. Wie ist das möglich? Ich habe keine Ahnung, wie ich anfangen soll, obwohl ich das Einheits-Tutorial gesehen habe, aber irgendwie ist es immer noch unklar. vielleicht, weil ich die Partikelsystemfunktion in anderen Klassen anrufe, verwirrte es mich irgendwie.Unity: Ändern des Partikelsystems in Pooling

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

Sie wollen yo Wiederverwendung Partikel; Ihr Code ist jedoch die Instanziierung von ** Partikel-Systemen **, nicht von Partikeln selbst. – AgentFire

+0

Wie wirkt es sich aus? – John

+0

Kopieren Sie einfach das Skript aus dem Tutorial, aber verwenden Sie , das ist alles, was es ist – Absinthe

Antwort

2

Zu allererst Wählen Sie Particle Systems Fertig/TNT dann stellen Sie sicher, dass Play Auf Awake ist nicht markiert. Das Pooling-Skript unten ist eine knifflige Art, dies zu erreichen. Es wird ein Array von ParticleSystem erstellt und dann wiederverwenden. Beachten Sie, dass ParticlePoolnicht erbt von MonoBehaviour, so stellen Sie sicher, es direkt zu kopieren.

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

Dann Ihre ParticleHolder Skript sollte unten mit dem Code aktualisiert werden. Das ist es. Keine Instanziierung mehr.

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

Sie meinen, 10 verschiedene Arten von Partikeln? Ich dachte, es gibt nur 2 Arten? – Programmer

+0

Ja, ich habe instanziiert 10. Nach weiteren Tests habe ich beschlossen, nur 5 zu instanziieren. Sie können sehen, wo ich 'particlePool = new ParticlePool (effects [0], effects [1], 5);'. Die fünf sind genug. Es wird diese 5 wiederverwenden. Sie brauchen nicht mehr als das. Testen Sie es und überzeugen Sie sich selbst. – Programmer

+0

Wird Sie tun und aktualisieren. Danke ^^ – John