我在这里对C#和Unity很陌生,并在此处遵循本教程 - http://catlikecoding.com/unity/tutorials/runner/
在本教程中,创建者通过生成具有变量x大小的多维数据集并调整大小以及在循环函数中重新定位这些多维数据集来创建对象队列:
model
我希望简化此功能以使队列循环(仅重新定位,而不是调整大小)10设置预制件(即设置长度的多维数据集),而不是每次重新缩放和更改多维数据集。
我完全不了解队列,我知道如何从预制件集合中随机选择10个预制件,但我不知道如何将这些预制件“存储”在队列中,或者如何将这些预制件再循环到正确的位置,因为这依赖于立方体的using UnityEngine;
using System.Collections.Generic;
public class PlatformManager : MonoBehaviour {
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 startPosition;
public Vector3 minSize, maxSize, minGap, maxGap;
public float minY, maxY;
public Material[] materials;
public PhysicMaterial[] physicMaterials;
public PhysicMaterial noFrictionMaterial;
public float spawnPosChance;
public Booster booster;
public AnimalSpawn animalSpawner;
public EnemySpawn enemySpawner;
private Vector3 nextPosition;
private Vector3 objectSpawnPos;
private Queue<Transform> objectQueue;
public float chance;
void Start () {
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
objectQueue = new Queue<Transform>(numberOfObjects);
for (int i = 0; i < numberOfObjects; i++) {
objectQueue.Enqueue((Transform)Instantiate(
prefab, new Vector3(0f, 0f, -100f), Quaternion.identity));
}
enabled = false;
}
void Update () {
if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
Recycle();
}
}
private void Recycle () {
Vector3 scale = new Vector3(
Random.Range(minSize.x, maxSize.x),
Random.Range(minSize.y, maxSize.y),
Random.Range(minSize.z, maxSize.z));
Vector3 position = nextPosition;
position.x += scale.x * 0.5f;
position.y += scale.y * 0.5f;
//Debug.Log (position);
Transform o = objectQueue.Dequeue();
o.localScale = scale;
o.localPosition = position;
o.gameObject.SetActive (true);
int materialIndex = Random.Range(0, materials.Length);
o.GetComponent<Renderer>().material = materials[materialIndex];
o.GetComponent<Collider> ().material = noFrictionMaterial;
//o.GetComponent<Collider>().material = physicMaterials[materialIndex]; //Dont randomize materials
objectQueue.Enqueue (o);
if (chance >= Random.Range (0f, 100f)) {
o.gameObject.SetActive (false); //randomize platform appearance
}
//Randomize where objects spawn
if (o.gameObject.active) {
if (spawnPosChance >= Random.Range (0f, 100f)) {
objectSpawnPos = position;
} else {
objectSpawnPos = new Vector3 (position.x,-4f,0f);
}
} else {
objectSpawnPos = new Vector3 (position.x,-6f,0f);
}
booster.SpawnIfAvailable(objectSpawnPos);
animalSpawner.SpawnAnimal (objectSpawnPos);
enemySpawner.Spawn (objectSpawnPos);
nextPosition += new Vector3(
Random.Range(minGap.x, maxGap.x) + scale.x,
Random.Range(minGap.y, maxGap.y),
Random.Range(minGap.z, maxGap.z));
if(nextPosition.y < minY){
nextPosition.y = minY + maxGap.y;
}
else if(nextPosition.y > maxY){
nextPosition.y = maxY - maxGap.y;
}
}
private void GameStart () {
nextPosition = startPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle ();
}
enabled = true;
}
private void GameOver () {
enabled = false;
}
}
。
我在这里很迷茫。怎么可以有一个10个随机选择的SET队列(不缩放或改变大小)预制件回收或重新定位?有什么建议吗?