2017-04-25 1 views
0

현재 유니티 안의 안드로이드 용 앱을 만들고 있습니다. 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; 
    } 
    } 
} 
} 

enter image description here 는 참고 : 게임의 작은 원은 지속적으로 상승 수직 이동과 더 큰 원은 장애의 일종으로 전환 될 것입니다,하지만 지금 난 그냥 :)이 잘 리스폰 갖고 싶어.

+3

확실하지 않습니까? 스크린 샷에 8 개의 객체가 생성 된 것을 볼 수 있습니다. 볼 수있는 코드 문제는 "spawnYPosition"변수를 사용하고 있지 않다는 것입니다. – CaTs

답변

0

귀하의 게시물에 CaT 코멘트에 추가하려면 로직이 올바른지 확인하십시오 (단, 변환의 위치를 ​​설정하는 행은 예외). 당신은 spawnXPosition이나 spawnYPosition을 사용하지 않습니다.

한번에 변경이 :

circles[currentCircle].transform.position = new Vector2(spawnXPosition, spawnYPosition); 

이 화재로 스폰 타이머를 방해하는 그것처럼 보인다 유일한 다른 점은 GameController.instance.gameOver로 설정 한 경우 : 여기에

circles[currentCircle].transform.position = new Vector2(0, +2); 

참된. 일부 디버그 문을 추가하거나 디버거를 사용하여이를 확인해보십시오.

Debug.Log("Is the game over? " GameController.instance.gameOver);