Unity 5 - 对象池不停用对象

时间:2016-12-27 22:27:41

标签: c# arrays unity5 object-pooling

我正在进行一场无尽的亚军比赛。我是编程的初学者,正在研究thisthis教程,所以我不能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);

    }
}
}

这是Object Pooler的代码:

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;

}
}

该脚本可以正常运行几秒钟,但很快就开始创建新平台,这正是我想要避免的。我认为正在发生的事情是平台没有被禁用,因此代码无法移动平台而是创建新平台。我确定这很容易解决,但我不知道该怎么做。有谁知道如何解决这个问题?感谢。

2 个答案:

答案 0 :(得分:0)

根据我的看法,您的代码中没有将对象返回池的功能。有一个函数可以从池中获取一个对象,如果需要可以自动实例化,否则从池中拉出来,并且你在启动方法逻辑中有prepool,但是你永远不会把对象放回去。您需要创建一个函数来将对象返回池中。

答案 1 :(得分:0)

功能

public GameObject GetPooledObject(){...}

如果在层次结构中禁用了对象,则

从池中返回poolObject。 否则它会实例化一个新的池对象并返回它。

newPlatform.SetActive (true);

在更新功能中,您将poolObject设置为活动但从不停用它。 因此,您的pooler会不断生成新对象。您需要使用object.setActive(false);

停用该对象

你可以及时做到 - 比如每2秒对象消失一次(但是对于不同的等级你需要改变这个时间)或者你可以计算对象是否已被越过并远远落后于你可以停用它。 / p>