【发布时间】:2016-12-27 22:27:41
【问题描述】:
我正在开发一款无尽的跑步者游戏。我是编程的初学者,正在学习 this 和 this 教程,所以我不能 100% 确定这段代码是如何工作的,这使得我很难弄清楚如何解决我的问题。 该代码应该使用对象池来创建平台,然后激活它们、停用它们、将它们移动到玩家面前并再次激活它们。 (至少,我认为)这为玩家提供了无穷无尽的平台,而无需不断地实例化新平台。这是平台生成器的代码:
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;
}
}
该脚本在播放几秒钟后运行良好,但很快就开始创建新平台,这是我试图避免的。我认为正在发生的事情是平台没有被禁用,因此代码无法移动平台,而是创建新平台。我确信这很容易解决,但我不知道该怎么做。有人知道如何解决这个问题吗?谢谢。
【问题讨论】:
标签: c# arrays unity5 object-pooling