2016-04-17 18 views
0

Bitte fühlen Sie sich von meiner langen Frage nicht eingeschüchtert, ich bin mir sicher, dass ich es einfach komisch formuliert habe lol. Ich folgte Mike Geigs exzellentem Tutorial über die Verwendung von Object Pooling in Unity, und ich verstehe das Konzept ziemlich gut. Ich habe nur eine Frage zu etwas, das mir seit fast einer Woche Kopfschmerzen bereitet. Wie bringe ich dieses Hindernis zurück in meinen Objektpooler, wenn es mit der Südwand kollidiert (die mit "SouthWall" markiert ist)? Ich habe ein Bild und meine Skripte: Alt-TextWie man Hindernis kodiert, um in meinen Objekt-Pooler zurückzugehen, wenn sie mit einer Grenze zusammenstoßen?

(The Script Unten ist mein Allgemein Pooler Script, das ich aus dem Objekt-Pooling Tutorial nur ein bisschen etwas geändert)

 using UnityEngine; 
    using System.Collections; 
    using System.Collections.Generic; 

    public class PoolerTestScript : MonoBehaviour 
    { 
     public static PoolerTestScript current; 
     public GameObject spikeWall; 
     public int wallPooledAmount = 20; 
     public bool willGrow = true; // This will be false in the inspector and the game, but here I will keep it true so I dont mess anything up. 

     private List<GameObject> wallPooledObjects; 

     void Awake() 
     { 
      current = this; 
     } 

     void Start() 
     { 
      wallPooledObjects = new List<GameObject>(); 
      for(int i = 0; i < wallPooledAmount; i++) 
      { 
       GameObject obj = (GameObject)Instantiate(spikeWall); 
       obj.SetActive(false); 
       wallPooledObjects.Add(obj); 
      } 
     } 

     public GameObject GetPooledObject() 
     { 
      for(int i = 0; i< wallPooledObjects.Count; i++) 
      { 
       if(!wallPooledObjects[i].activeInHierarchy) 
       { 
        return wallPooledObjects[i]; 
       } 
      } 

       if (willGrow) 
      { 
       GameObject obj = (GameObject)Instantiate(spikeWall); 
       wallPooledObjects.Add(obj); 
       return obj; 
      } 

      return null; 

     } 

    } 

(The Script Im Folgenden finden Was Spawns die Hindernisse, habe ich es aus dem Weltraum-Shooter Tutorial und verändert es auf einige!)

using UnityEngine; 
using System.Collections; 

[System.Serializable] 

public class Obstacle2 // Spike Wall Obstacle 
{ 
    public GameObject wall; // The second obstacle gameobject. This is attached in the inspector. 
    public Vector3 spawnWPosValues; // Position where the second obstacle will be spawned at on the X,Y,Z plane. 
    public int wCount; // This is the count of the second obstacle in a given wave. 
    public float wSpawnWait; // Time in seconds between next wave of obstacle 2. 
    public float wStartGameWait; // Time in seconds between when the game starts and when the second obstacle start spawning. 
    public float wWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 2 will spawn. 
} 

public class SpawnWalls : MonoBehaviour { 

    public Obstacle2 obstacle2; 

    void Start() { 

     StartCoroutine (SpawnWall()); 
     //InvokeRepeating ("Spawn", 1, 1); 

     //Get reference to rigidbody, and set the speed 
    } 


    IEnumerator SpawnWall() { 

     yield return new WaitForSeconds(obstacle2.wStartGameWait); 
     while (true) 
     { 

      for (int i = 0; i < obstacle2.wCount; i++) { 

       Vector3 spawnPosition_2 = new Vector3 (Random.Range(-obstacle2.spawnWPosValues.x, obstacle2.spawnWPosValues.x), 
                 obstacle2.spawnWPosValues.y, 
                 obstacle2.spawnWPosValues.z); 
       Quaternion spawnRotation_2 = Quaternion.Euler(0,270,0); // was 90, 0, 90 
       Instantiate (obstacle2.wall, spawnPosition_2, spawnRotation_2); 
       yield return new WaitForSeconds(obstacle2.wSpawnWait); 
      } 
      yield return new WaitForSeconds (obstacle2.wWaveSpawnWait); 
     } 
    } 
} 

(The Script Unten ist, was die Hindernisse Verschiebt)

using UnityEngine; 
using System.Collections; 

public class WallObstacleMover : MonoBehaviour { 

    private Rigidbody rb;  //Reference to Rigidbody Component 

    public float speed;  //Speed, updated through script 
    public float acceleration; //Every second, the speed will increase by this much 

    //Executes once, when object is spawned/scene loaded 
    void Start() { 
     //Get reference to rigidbody, and set the speed 
     rb = GetComponent<Rigidbody>(); 
     rb.velocity = -transform.right * speed; 

} 
    //Executes every frame 
    void Update() { 
     //Add acceleration to speed, make sure it's not above topSpeed) 
     speed += Time.deltaTime * acceleration; 
     //Set object velocity 
     rb.velocity = -transform.right * speed; 

    } 
} 
+0

Ich denke, Ihre Frage wird etwas Aufmerksamkeit auf http://codereview.stackexchange.com/ erhalten –

Antwort

0

Verwenden Sie Ihren Pooler nicht, um das Hindernis zu instanziieren, sondern instanziieren Sie es einfach in der Coroutine. Sie müssen GetPooledObject() von PoolerTestScript verwenden, um das Hindernis zu poolen. dann bei der Kollision, anstatt das Spielobjekt zu zerstören, rufen Sie einige GoBackToPool() -Methode auf, die Sie auch erstellen müssen (und das gepoolte Objekt muss einen Weg finden, den Pool zu finden, also entweder einen Verweis behalten oder eine Szenenverwaltung haben Das bietet statischen Zugriff - Delegaten wäre eine andere Option)