我为每个实例化的预制件创建了一个对象池。我有2个问题
这是我的 ObjectPool 代码的完整脚本:
public class ObjectPool : MonoBehaviour
{
public Transform player;
[Header("General Values")]
public float currentBlockY;
public float currentBlockX;
public float disBeforeSpawnBlock = 10f;
public float minDisBetweenBlocks;
public float maxDisBetweenBlocks;
public float minInsOffsetX;
public float maxInsOffsetX;
public float brokenMinOffsetX;
public float brokenMaxOffsetX;
public int initBlockLines = 10;
public List<GameObject> normalBlockPool;
public AnimController animeC1;
[Header("Grass")]
public GameObject grassPlatformPrefab;
public List<Sprite> grassSpriteList;
int grassSpriteCount;
[Header("Slime")]
public GameObject slimePlatformPrefab;
[Header("Broken")]
public GameObject brokenPlatform;
public bool isBrokenPlatform = true;
private void Awake()
{
InitSpawnBlock();
}
private void Update()
{
if(currentBlockY - player.transform.position.y < disBeforeSpawnBlock)
{
ContinueToSpawn();
}
}
private void InitSpawnBlock()
{
for (int i = 0; i < initBlockLines; i++)
{
GrassSpawner();
}
}
private void ContinueToSpawn()
{
normalBlockPool[0].transform.position = new Vector2(Random.Range(minInsOffsetX, maxInsOffsetX), currentBlockY);
currentBlockY += Random.Range(minDisBetweenBlocks, maxDisBetweenBlocks);
if(player.transform.position.y > 20 && Random.Range(0f, 2f) < 0.5f)
{
isBrokenPlatform = true;
BrokenPlatformSpawner();
if(isBrokenPlatform)
{
Vector2 blockPos = new Vector2(currentBlockX + Random.Range(brokenMinOffsetX, brokenMaxOffsetX), currentBlockY - Random.Range(1, 5));
GameObject gO = Instantiate(grassPlatformPrefab, blockPos, Quaternion.identity);
gO.GetComponent<SpriteRenderer>().sprite = grassSpriteList[grassSpriteCount];
gO.transform.SetParent(transform);
}
}
else if(player.transform.position.y > 150 && Random.Range(0f, 1f) < 0.1f)
{
SlimeSpawner();
}
else
{
GrassSpawner();
}
GameObject temp = normalBlockPool[0];
normalBlockPool.RemoveAt(0);
normalBlockPool.Add(temp);
temp.SetActive(true);
}
private void GrassSpawner()
{
grassSpriteCount = Random.Range(0, grassSpriteList.Count);
Vector2 blockPos = new Vector2(Random.Range(minInsOffsetX, maxInsOffsetX), currentBlockY);
currentBlockY += Random.Range(minDisBetweenBlocks, maxDisBetweenBlocks);
GameObject gO = Instantiate(grassPlatformPrefab, blockPos, Quaternion.identity);
gO.GetComponent<SpriteRenderer>().sprite = grassSpriteList[grassSpriteCount];
gO.transform.SetParent(transform);
normalBlockPool.Add(gO);
}
private void SlimeSpawner()
{
Vector2 blockPos = new Vector2(Random.Range(minInsOffsetX, maxInsOffsetX), currentBlockY);
currentBlockY += Random.Range(minDisBetweenBlocks, maxDisBetweenBlocks);
GameObject gO = Instantiate(slimePlatformPrefab, blockPos, Quaternion.identity);
animeC1.SlimeAnimationOne();
gO.transform.SetParent(transform);
normalBlockPool.Add(gO);
}
private void BrokenPlatformSpawner()
{
Vector2 blockPos = new Vector2(Random.Range(minInsOffsetX, maxInsOffsetX), currentBlockY);
currentBlockY += Random.Range(minDisBetweenBlocks, maxDisBetweenBlocks);
GameObject gO = Instantiate(brokenPlatform, blockPos, Quaternion.identity);
gO.transform.SetParent(transform);
normalBlockPool.Add(gO);
gO.SetActive(true);
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "BrokenPlatform")
{
other.gameObject.GetComponent<CapsuleCollider2D>().gameObject.SetActive(false);
}
}
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀