【问题标题】:Unity 5 - Object pooling not deactivating objectsUnity 5 - 对象池不停用对象
【发布时间】:2016-12-27 22:27:41
【问题描述】:

我正在开发一款无尽的跑步者游戏。我是编程的初学者,正在学习 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);

    }
}
}

这是对象池的代码:

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


    【解决方案1】:

    据我所知,您的代码中没有将对象返回到池中的功能。有一个函数可以从池中获取对象,如果需要,它会自动实例化,否则从池中提取,并且您在 start 方法逻辑中具有预池,但您永远不会放回对象。您需要创建一个函数将对象返回到池中。

    【讨论】:

      【解决方案2】:

      功能

      public GameObject GetPooledObject(){...}

      如果对象在层次结构中被禁用,则从池中返回一个 poolObject。 否则它会实例化一个新的池对象并返回它。

      newPlatform.SetActive (true);

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

      您可以及时进行 - 假设对象每 2 秒消失一次(但对于不同的级别,您需要更改此时间)或者您可以计算对象是否已被越过并且远远落后,然后您可以停用它。

      【讨论】:

      • 就是这样!在离开屏幕后停用它们的平台上添加了一个脚本。我想我错过了教程的那部分......谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多