Compare size of current object pulled from pool, with last object pulled from pool

假装没事ソ 提交于 2020-06-17 02:53:12

问题


This is a follow-up for a different question I posted earlier. The solution found there led to a new problem I will describe here.

Having objects spawn, with random size within set range, with equal distance between them regardless of framerate

I am trying to create the effect of several buildings in different sizes moving from left to right at a constant speed regardless of framerate. I want the spacing between the buildings to be the same no matter the size, and each building's size is randomized within a range.

I solved this by comparing the size of the last building with the current and dividing it by their speed, which worked initially, but was very heavy on performance due to Instantiating prefabs constantly. This is the original code from my ScrollingCity script.

if(Time.time > nextBuilding)
    {
        spawningRatio = ((randomSize / 2) + (buildingPrefab.transform.localScale.x / 2) + distanceBetween) / (speed);
        nextBuilding = Time.time + spawningRatio;
        buildingPrefab.transform.localScale = new Vector3(randomSize, randomSize, randomSize);
        Instantiate(buildingPrefab, spawnPoint.transform.position, Quaternion.identity);
        randomSize = Random.Range(ranMin, ranMax);
        }

In the linked thread I was advised to use pooling for my objects, to reduce the load, and I was able to make it work with the use of some tutorials. However, I now don't know how to compare the sizes of the last to objects pulled from the pool, to ensure that they all have the same spacing between them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPooler : MonoBehaviour
{



 [System.Serializable]
    public class Pool
    {
        public string tag;
        public GameObject prefab;
        public int size;
    }

#region Singleton
public static ObjectPooler Instance;

private void Awake()
{
    Instance = this;
}
#endregion

public List<Pool> pools;

public Dictionary<string, Queue<GameObject>> poolDictionary;


private void Start()
{
    poolDictionary = new Dictionary<string, Queue<GameObject>>();

    foreach (Pool pool in pools)
    {

        Queue<GameObject> objectPool = new Queue<GameObject>();


        for (int i = 0; i < pool.size; i++)
        {

            GameObject obj = Instantiate(pool.prefab);
            obj.SetActive(false);
            objectPool.Enqueue(obj);
        }

        poolDictionary.Add(pool.tag, objectPool);
    }



}

public GameObject SpawnFromPool (string tag, Vector3 position, Quaternion rotation)
{

    if (!poolDictionary.ContainsKey(tag))
    {
        Debug.LogWarning("Tag doesn't exist");
        return null;
    }



    GameObject objectToSpawn = poolDictionary[tag].Dequeue();

    objectToSpawn.SetActive(true);
    objectToSpawn.transform.position = position;
    objectToSpawn.transform.rotation = rotation;

    poolDictionary[tag].Enqueue(objectToSpawn);

    return objectToSpawn;
}

}

This is the script for my Object Pooler, learned from a tutorial.

I then call the function in my ScrollingCity Script, where I try to get the transform value of the latest object created so I can compare them with the current object, but I can only get the transform of the first object pulled, until it reaches the end of the Queue.

if(Time.time > nextBuilding) {

        nextBuilding = Time.time + spawningRatio;
        objectPooler.SpawnFromPool("Build", transform.position, Quaternion.identity);
        lastBuilding = objectPooler.SpawnFromPool("Build", transform.position, Quaternion.identity);
        Debug.Log(lastBuilding.transform.localScale);
    }

And it only returns the transform of the first object in the log:

So my Question is how can I access the scale of the last object in the Queue, and compare it with the current to control the frequency with which they are created, and effectively keep the distance between each object the same regardless of scale?

来源:https://stackoverflow.com/questions/62044548/compare-size-of-current-object-pulled-from-pool-with-last-object-pulled-from-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!