Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在创建对象池时需要帮助吗_C#_Unity3d_Object - Fatal编程技术网

C# 在创建对象池时需要帮助吗

C# 在创建对象池时需要帮助吗,c#,unity3d,object,C#,Unity3d,Object,我正在制作一个unity2d游戏,需要一个对象池来生成地面。但是在这个版本中,我需要在特定的布局中生成一组地面精灵,而不是单独生成地面精灵。我使用的pooler只单独生成它们 using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectPooler : MonoBehaviour { public G

我正在制作一个unity2d游戏,需要一个对象池来生成地面。但是在这个版本中,我需要在特定的布局中生成一组地面精灵,而不是单独生成地面精灵。我使用的pooler只单独生成它们

using JetBrains.Annotations;
using System.Collections; 
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooler : MonoBehaviour
{

public GameObject pooledObject;
public int pooledamnt;
List<GameObject> pooledObjects;
// Start is called before the first frame update
void Start()
{
    pooledObjects = new List<GameObject>();
    for (int i = 0; i < pooledamnt; 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;
        
    }

   

// Update is called once per frame
void Update()
{
   
}
}
使用JetBrains.annotation;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类ObjectPooler:MonoBehavior
{
公共游戏对象池对象;
公共int pooledamnt;
列出池对象;
//在第一帧更新之前调用Start
void Start()
{
pooledObjects=新列表();
对于(int i=0;i

感谢您的帮助。

在Unity中,对于您想要的每个布局,您都可以手动使用对象进行预制。创建一个空对象并使地面对象成为它的子对象。然后分配给他们

比如:

公共列表池对象;
public List<GameObject> pooledObjects;