使用对象池为无尽奔跑者生成平台

时间:2018-07-25 16:18:46

标签: unity3d 2d 2d-games object-pooling

我一直在独自制作一个无尽的2D跑步者。我有点被平台生成所困扰,我想在三个级别(即底部,中间和顶部)中生成平台。现在,我可以在任意位置的底部生成平台,但是当我在中部和顶部生成平台时,游戏会出现滞后。

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    [System.Serializable]
    public class ObjectPoolItem
    {
 public GameObject objectToPool;
 public int amountToPool;
 public bool shouldExpand = true;
 public ObjectPoolItem(GameObject obj, int amt, bool exp = true)
 {
     objectToPool = obj;
     amountToPool = Mathf.Max(amt,2);
     shouldExpand = exp;
 }
 }
 public class ObjectPooler : MonoBehaviour
 {
 public static ObjectPooler SharedInstance;
 public List<ObjectPoolItem> itemsToPool;
 public List<List<GameObject>> pooledObjectsList;
 public List<GameObject> pooledObjects;
 private List<int> positions;
 void Awake()
 {
     SharedInstance = this;
     pooledObjectsList = new List<List<GameObject>>();
     pooledObjects = new List<GameObject>();
     positions = new List<int>();
     for (int i = 0; i < itemsToPool.Count; i++)
     {
         ObjectPoolItemToPooledObject(i);
     }
 }
 public GameObject GetPooledObject(int index)
 {
     int curSize = pooledObjectsList[index].Count;
     for (int i = positions[index] + 1; i < positions[index] + pooledObjectsList[index].Count; i++)
     {
         if (!pooledObjectsList[index][i % curSize].activeInHierarchy)
         {
             positions[index] = i % curSize;
             return pooledObjectsList[index][i % curSize];
         }
     }
     if (itemsToPool[index].shouldExpand)
     {
         GameObject obj = (GameObject)Instantiate(itemsToPool[index].objectToPool);
         obj.SetActive(false);
         obj.transform.parent = this.transform;
         pooledObjectsList[index].Add(obj);
         return obj;
     }
     return null;
 }
 public List<GameObject> GetAllPooledObjects(int index)
 {
     return pooledObjectsList[index];
 }
 public int AddObject(GameObject GO, int amt = 3, bool exp = true)
 {
     ObjectPoolItem item = new ObjectPoolItem(GO, amt, exp);
     int currLen = itemsToPool.Count;
     itemsToPool.Add(item);
     ObjectPoolItemToPooledObject(currLen);
     return currLen;
 }
 void ObjectPoolItemToPooledObject(int index)
 {
     ObjectPoolItem item = itemsToPool[index];
     pooledObjects = new List<GameObject>();
     for (int i = 0; i < item.amountToPool; i++)
     {
         GameObject obj = (GameObject)Instantiate(item.objectToPool);
         obj.SetActive(false);
         obj.transform.parent = this.transform;
         pooledObjects.Add(obj);
     }
     pooledObjectsList.Add(pooledObjects);
     positions.Add(0);
 }
 }

上面是ObjectPooling的代码。下面是我进行平台生成的逻辑,现在它仅在底层生成平台。

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class Spawn : MonoBehaviour {
 public Transform generationPoint;
 private float distanceBetween;
 public float distanceBetweenMin;
 public float distanceBetweenMax;
 private float width;
 ObjectPooler objectPool;
 void Start()
 {
     objectPool = ObjectPooler.SharedInstance;

 }
 void Update()
 {
     if (transform.position.x < generationPoint.position.x)
     {
         distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
         int index = Random.Range(0, 3);
         width = objectPool.GetPooledObject(index).GetComponent<BoxCollider2D>().size.x;
         transform.position = new Vector3(transform.position.x + (width / 2) + distanceBetween, transform.position.y, transform.position.z);
         GameObject newPlatform = objectPool.GetPooledObject(index);
         newPlatform.transform.position = transform.position;
         newPlatform.transform.rotation = transform.rotation;
         newPlatform.SetActive(true);
         transform.position = new Vector3(transform.position.x + (width / 2), transform.position.y, transform.position.z);
     }
 }
 }

This is how i wish to generate platforms 这就是我要生成平台的方式,即,只要两个底层平台之间存在间隙,就会在某个高度生成一个中层平台以填补该差距。

感谢所有帮助,我还很团结。 :)

1 个答案:

答案 0 :(得分:-1)

我认为最好只使用Random函数来生成平台