我正在製作一個無盡的亞軍遊戲。我非常喜歡編程,因此我的this和this教程正在工作,所以我不能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;
}
}
該腳本工作正常播放了幾秒鐘,但很快就開始打造新的平臺,這是我想要避免的。我認爲發生的事情是平臺沒有被禁用,所以代碼不能移動平臺,而是創建新的平臺。我相信這是一個簡單的解決方案,但我不知道該怎麼做。任何人都知道如何解決這個問題?謝謝。
就是這樣!在屏幕上停用後,在平臺上添加了一個腳本。我想我錯過了教程的那一部分...謝謝! – MayorDump