Unity运行器游戏中的对象池实现(重用) [英] Object Pooling implementation ( reusing ) in Unity runner game

查看:151
本文介绍了Unity运行器游戏中的对象池实现(重用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的第一场亚军比赛.一切正常.因此,这个问题与优化有关.

I have my first runner game. Everything works fine. So, this question is about optimisation.

在我的游戏中,有15个平台(玩家在其上运行的道路预制件).随机从15中任意实例化出1个,并且这种情况一直在发生.当玩家经过几个平台时,留下的平台将被销毁.我列出了要跟踪平台的列表,并在上一个平台预制设备(list [0])上将其删除.并且新的实例会在前面实例化.

In my game, There are 15 platforms(road prefabs on which player runs). Randomly out 15 any 1 is instantiated and this keeps happening. When player passes a few platforms, The left behind platforms gets destroyed. I made a list to keep track of platforms and delete them last platform prefab ( list[0]). And new ones are instantiated ahead.

随着游戏的临近,它变得越来越快,这意味着实例化/销毁的操作现在越来越频繁.

As the game approaches, It gets fast which means the operation of instantiating/destroying is happening more frequently now.

我阅读了有关对象池的知识.我理解了这个概念,并且我强烈认为应该在游戏中使用它.我创建一个对象池.工作正常.现在我的问题-

I read about object pooling. I understood the concept and I hold very strong opinion that I should use this in my game. I create an object pool. Works fine. Now my problem -

问题-如何重用已创建池中的对象?在我的游戏中,我想到的是-用户留下的平台应将位置从后退更改为前进(用户前进的方向).我该如何实现?

PROBLEM - How should I reuse object from my created pool? In my game, What I came up with is - The platforms that user has left behind should change position from back to forward( direction in which user is heading ). How can I achieve that ?

我遵循了这些教程- https://www.youtube.com/playlist?list= PLLH3mUGkfFCXps_IYvtPcE9vcvqmGMpRK

推荐答案

当玩家经过几个平台时,剩下的平台将获得 毁了.我列出了要跟踪的平台并将其删除 最后一个平台预制件(列表[0]).并且新的实例会在前面实例化.

When player passes a few platforms, The left behind platforms gets destroyed. I made a list to keep track of platforms and delete them last platform prefab ( list[0]). And new ones are instantiated ahead.

有许多方法可以进行对象池化.其中之一包括将对象添加到队列中,并在使用它们时将其删除.当您要回收/销毁它们时,请将它们重新添加到列表中.

There many ways to make Object pooling. One of them includes adding the Objects to a queue and removing them when they are in use. When you want to recycle/destroy them, add them back to the List instead.

如何重用已创建池中的对象?

How should I reuse object from my created pool?

这真的很容易.不必每次都这样做:

It's really easy. Instead of doing this each time:

Instantiate(prefab, postion, Quaternion.identity);

在游戏从开始"功能开始时执行多次,然后将其存储在数组/列表中:

Do it many times when the game begins in the Start function and store then in an Array/List:

List<GameObject> pool = new List<GameObject>();

void Start()
{
    for (int i = 0; i < 50; i++)
    {
        GameObject tempObj = Instantiate(prefab, postion, Quaternion.identity) as GameObject;
        pool.Add(tempObj);
    }
}


当您需要在游戏过程中实例化Object时,只需从List/Array中获取一个即可.


When you need to instantiate Object during gameplay, just get one from the List/Array:

//Check if object is available in pool
if (pool.Count > 0)
{
    //Object is. Return 1
    GameObject objFromPool = pool[0];
    //Enable the Object
    objFromPool.SetActive(true);
}
else
{
    //Object is NOT. Instantiate new one
    GameObject objFromPool = Instantiate(prefab, postion, Quaternion.identity) as GameObject;
    //Enable the Object
    objFromPool.SetActive(true);
}


完成使用对象后.不用执行Destroy(objFromPool);,而是重新设置该GameObject的位置,也可以根据需要将其禁用,然后将其重新添加到List:


When you are done using the Object. Instead of doing Destroy(objFromPool);, reset the position of that GameObject, maybe disable it also if you want then add it back to the List:

//Reset Pos
objFromPool.transform.position = Vector3.zero;
//Disable?
objFromPool.SetActive(false);
//Return back to the array
pool.Add(objFromPool);


最后,执行此操作的最佳方法是将所有对象实例化为数组或列表.使用您增加的整数.整数从0开始并递增,直到达到list/array.Length-1.然后,您可以使用该整数从池中获取对象.


Finally, the best way of doing this is to instantiate all the objects into an array or list. Use an integer that you increment. The integer starts from 0 and increments until it reaches list/array.Length-1. You can then use that integer to get an Object from the pool.

您可以在ArrayObjectPooling 此处中看到实现此方法的示例,以及如何使用它.

You can see example of this method implemented as ArrayObjectPooling here and how to use it.

这篇关于Unity运行器游戏中的对象池实现(重用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆