0

S'il vous plaît ne vous sentez pas intimidé par ma longue question, je suis sûr que je viens de le dire bizarre lol. J'ai suivi l'excellent tutoriel de Mike Geig sur l'utilisation d'Object Pooling dans Unity, et je comprends très bien le concept. J'ai juste une question sur quelque chose qui a été un gros problème pour moi pendant près d'une semaine. Comment faire pour que cet obstacle retourne dans mon Pool d'Objets quand il rencontre le Mur Sud (qui est marqué "SouthWall")? J'ai une photo et mes scripts: alt texteComment code obstacle pour retourner dans mon pool d'objets lorsqu'ils se heurtent à une frontière?

(Le script ci-dessous est mon générique Pooler Script que je changé juste un peu peu de l'objet Pooling Tutorial)

 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; 

     } 

    } 

(le script est en dessous de ce Spawns les obstacles, je l'ai obtenu du Space Shooter Tutoriel et changé en certains!)

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

(le script est ci-dessous ce qui bouge les obstacles)

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

Je pense que votre question recevra un peu d'attention à http://codereview.stackexchange.com/ –

Répondre

0

vous n'utilisez pas votre pooler pour instancier l'obstacle, vous l'instantiez simplement dans la coroutine. vous devez utiliser GetPooledObject() à partir de PoolerTestScript pour que l'obstacle soit regroupé. alors, au lieu de détruire l'objet gameobject, vous appelez une méthode GoBackToPool() que vous devez également créer (et souvent l'objet groupé a besoin d'un moyen de trouver le pool auquel il appartient, donc gardez une référence ou ayez une gestion de scène qui fournit un accès statique - les délégués seraient une autre option)