2016-12-27 181 views
0

我正在製作一個無盡的亞軍遊戲。我非常喜歡編程,因此我的thisthis教程正在工作,所以我不能100%確定這段代碼是如何工作的,這使得很難弄清楚如何解決我的問題。 代碼應該使用對象池來創建平臺,然後激活它們,停用它們,將它們移動到播放器前面並再次激活它們。 (至少,我認爲)這給玩家提供了無限的平臺,無需不斷實例化新的平臺。下面是該平臺生成的代碼:Unity 5 - 對象池不禁用對象

using UnityEngine; 
using System.Collections; 

public class PlatformGenerator : MonoBehaviour 
{ 

public GameObject thePlatform; 
public Transform generationPoint; 
public float distanceBetween; 

private float platformWidth; 

public float distanceBetweenMin; 
public float distanceBetweenMax; 

private int platformSelector; 
private float[] platformWidths; 
// public GameObject[] thePlatforms; 

public ObjectPooler[] theObjectPools; 

private float minHeight; 
private float maxHeight; 
public Transform maxHeightPoint; 
public float maxHeightChange; 
private float heightChange; 

void Start() 
{ 
//  platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x; 

    platformWidths = new float[theObjectPools.Length]; 

    for (int i = 0; i < theObjectPools.Length; i++) 
    { 
     platformWidths[i] =    theObjectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x; 
    } 

    minHeight = transform.position.y; 
    maxHeight = maxHeightPoint.position.y; 
} 

void Update() 
{ 
    if (transform.position.x < generationPoint.position.x) 
    { 
     distanceBetween = Random.Range (distanceBetweenMin, distanceBetweenMax); 

     platformSelector = Random.Range (0, theObjectPools.Length); 

     heightChange = transform.position.y + Random.Range (maxHeightChange, -maxHeightChange); 
     //   if you want to have platforms generating outside boundries, comment out this code: 
     if (heightChange > maxHeight) 
     { 
      heightChange = maxHeight; 
     } 
     else if (heightChange < minHeight) 
     { 
      heightChange = minHeight; 
     } 

     transform.position = new Vector3 (transform.position.x + (platformWidths[platformSelector]/2) + distanceBetween, heightChange, transform.position.z); 

//   Instantiate (/*thePlatform*/ thePlatforms[platformSelector], transform.position, transform.rotation); 

     GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject(); 

     newPlatform.transform.position = transform.position; 
     newPlatform.transform.rotation = transform.rotation; 
     newPlatform.SetActive (true); 

     transform.position = new Vector3 (transform.position.x + (platformWidths[platformSelector]/2), transform.position.y, transform.position.z); 

    } 
} 
} 

,這裏是爲對象波爾代碼:

public class ObjectPooler : MonoBehaviour 
{ 
public GameObject pooledObject; 
public int pooledAmount; 

List<GameObject> pooledObjects; 

void Start() 
{ 
    pooledObjects = new List<GameObject>(); 

    for (int i = 0; i < pooledAmount; i++) 
    { 
     GameObject obj = (GameObject) Instantiate (pooledObject); 
     obj.SetActive (false); 
     pooledObjects.Add (obj); 
    } 
} 

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

    GameObject obj = (GameObject) Instantiate (pooledObject); 
    obj.SetActive (false); 
    pooledObjects.Add (obj); 
    return obj; 

} 
} 

該腳本工作正常播放了幾秒鐘,但很快就開始打造新的平臺,這是我想要避免的。我認爲發生的事情是平臺沒有被禁用,所以代碼不能移動平臺,而是創建新的平臺。我相信這是一個簡單的解決方案,但我不知道該怎麼做。任何人都知道如何解決這個問題?謝謝。

回答

0

就我所見,代碼中沒有將對象返回到池的功能。有一個函數可以從池中獲取一個對象,如果需要它會自動實例化,否則從池中拉出,並且在啓動方法邏輯中有預先池,但是你永遠不會放回對象。您需要創建一個函數將對象返回到池中。

0

功能

public GameObject GetPooledObject(){...}

如果對象在層次結構禁用返回從池中poolObject。否則它實例化一個新的池對象並返回它。

newPlatform.SetActive (true);

在更新功能,您的poolObject設置爲活躍,但從來沒有關閉它。 因此,你的池不斷生成新的對象。您需要停用對象使用object.setActive(false);

您可以及時做到這一點 - 例如說每2秒鐘對象消失(但對於不同的級別,您將需要改變這次),或者您可以計算對象是否已被穿過並且是遠遠落後,那麼你可以停用它。

+0

就是這樣!在屏幕上停用後,在平臺上添加了一個腳本。我想我錯過了教程的那一部分...謝謝! – MayorDump