현재 유니티 안의 안드로이드 용 앱을 만들고 있습니다. C#으로 스크립팅하기. 게임은 수직 스크롤러이며 무한대로 게임 개체 (내 경우에는 서클)를 생성하고 싶습니다. 내가 가지고있는 문제는 객체 풀이 생성되지 않을 것이고 스폰하기 위해 그것을 얻을 수있을 때 재활용 및 재 생성하지 않는다는 것입니다 ... 지금 몇 시간 동안 사용해 왔고 어디에도 가지 않는다. 여기에 제 코드가 있습니다. Unity의 스크린 샷도 링크 할 것입니다.C# 유니티 2D 객체 풀링
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CirclePool : MonoBehaviour {
public GameObject BigCircle; //the circle game object
public int circlePoolSize = 8; //how many circles to keep on standby
public float spawnRate = 1f; //how quickly circles spawn
public float circleMin = 3; //minimum y value of circle pos
public float circleMax = 7; //max value
private GameObject[] circles; //collection of pooled circles
private int currentCircle = 0; //index of the current circle in the collection
private Vector2 objectPoolPosition = new Vector2(-15, -25); //holding position for unused circles offscreen
private float spawnXposition = 10f;
private float timeSinceLastSpawned;
void Start() {
timeSinceLastSpawned = 0f;
//initialize the circles collection
circles = new GameObject[circlePoolSize];
//loop through collection..
for(int i = 0; i < circlePoolSize; i++)
{
//and create individual circles
circles[i] = (GameObject)Instantiate(BigCircle, objectPoolPosition, Quaternion.identity);
}
}
void Update() {
timeSinceLastSpawned += Time.deltaTime;
if (GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0f;
//set random y pos for circle
float spawnYPosition = Random.Range(circleMin, circleMax);
//then set the current circle to that pos
circles[currentCircle].transform.position = new Vector2(0, +2);
//increase the value of currentCircle. if the new size is too big, back to 0
currentCircle++;
if (currentCircle >= circlePoolSize)
{
currentCircle = 0;
}
}
}
}
는 참고 : 게임의 작은 원은 지속적으로 상승 수직 이동과 더 큰 원은 장애의 일종으로 전환 될 것입니다,하지만 지금 난 그냥 :)이 잘 리스폰 갖고 싶어.
확실하지 않습니까? 스크린 샷에 8 개의 객체가 생성 된 것을 볼 수 있습니다. 볼 수있는 코드 문제는 "spawnYPosition"변수를 사용하고 있지 않다는 것입니다. – CaTs