2017-01-17 4 views
1

私は複数の選択肢を持つクイズゲームを持っています。私はランダム質問を表示したいが、同じ質問が複数回選択されることがある。選択した質問をリストから削除して、それ以上ピックしないようにします。Unity:Remove random.rangeから選択したオブジェクト

これは私の質問 を表示するゲームマネージャです**ここで私のランダムを実行するShowQuestion機能をチェックしてください。質問が再び

public class GameController : MonoBehaviour { 

private dataController DataController; 
private roundData currentRoundData; 
// QUestions that we will be working with in this round 
private questionData[] questionPool; 
private List<GameObject> answerButtonGameobjects = new List<GameObject>(); 

private List<GameObject> QuestionGameobjects = new List<GameObject>(); 


[Header("OTHER")] 
public SimpleObjectPool answerButtonObjectPool; 
public Transform answerButtonParent; 


[Tooltip("Variables")] 
// is the game going? 
private bool isRoundActive; 
private float timerRemaing; 
// What number question we are on 
private int questionIndex; 
private int questionNumber = 1; 
private int totalQuestions; 



[Header("UI")] 
[Header("TEXT ")] 
public Text questionText; 
public Text scoreDisplayText; 
public Text timeRemainingDisplayText; 
public Text highScoreText; 
public Text questionIndexText; 

void Start() { 

    DataController = FindObjectOfType<dataController>(); 

    currentRoundData = DataController.getCurrentRoundData(); 



    // stores our questions 
    questionPool = currentRoundData.questions; 

    timerRemaing = currentRoundData.timeLimitInSeconds; 

    questionIndex = 0; 

    totalQuestions = 10; 


    ShowQuestion(); 

} 

// Show our 1st question 
private void ShowQuestion() 
{ 
    RemoveAnsweredButtons(); 


    // Hold current question data from our pool 
    questionData QuestionData = questionPool[Random.Range(0, totalQuestions)]; 

    questionText.text = QuestionData.questionText; 

    for (int i = 0; i < QuestionData.answers.Length; i++) 
    { 
     GameObject answerButtonGameobject = answerButtonObjectPool.GetObject(); 
     answerButtonGameobject.transform.SetParent(answerButtonParent); 
     answerButtonGameobjects.Add(answerButtonGameobject); 

     AnswerButton answerButton = answerButtonGameobject.GetComponent<AnswerButton>(); 
     answerButton.Setup(QuestionData.answers[i]); 
    } 


} 

/// <summary> 
/// Removes the old answers button before we display new ones. 
/// </summary> 
private void RemoveAnsweredButtons() 
{ 
    while (answerButtonGameobjects.Count > 0) 
    { 
     answerButtonObjectPool.ReturnObject(answerButtonGameobjects[0]); 
     answerButtonGameobjects.RemoveAt(0); 
    } 


} 




private void UpdateQuestionIndex() 
{ 
    questionIndex++; 

    questionNumber++; 
    questionIndexText.text = "Question " + (questionNumber.ToString()) + " out of 10"; 
} 



} 

質問と回答ユニティサイトから取られたシンプルなプールシステムを使用して生成されているを表示しないように選ばれたとき、私は基本的にしたいです。

ここプーリングクラスは

using UnityEngine; 
using System.Collections.Generic; 

// A very simple object pooling class 
public class SimpleObjectPool : MonoBehaviour 
{ 
// the prefab that this object pool returns instances of 
public GameObject prefab; 
// collection of currently inactive instances of the prefab 
private Stack<GameObject> inactiveInstances = new Stack<GameObject>(); 

// Returns an instance of the prefab 
public GameObject GetObject() 
{ 
    GameObject spawnedGameObject; 

    // if there is an inactive instance of the prefab ready to return, return that 
    if (inactiveInstances.Count > 0) 
    { 
     // remove the instance from teh collection of inactive instances 
     spawnedGameObject = inactiveInstances.Pop(); 
    } 
    // otherwise, create a new instance 
    else 
    { 
     spawnedGameObject = (GameObject)GameObject.Instantiate(prefab); 

     // add the PooledObject component to the prefab so we know it came from this pool 
     PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>(); 
     pooledObject.pool = this; 
    } 

    // put the instance in the root of the scene and enable it 
    spawnedGameObject.transform.SetParent(null); 
    spawnedGameObject.SetActive(true); 

    // return a reference to the instance 
    return spawnedGameObject; 
} 

// Return an instance of the prefab to the pool 
public void ReturnObject(GameObject toReturn) 
{ 
    PooledObject pooledObject = toReturn.GetComponent<PooledObject>(); 

    // if the instance came from this pool, return it to the pool 
    if(pooledObject != null && pooledObject.pool == this) 
    { 
     // make the instance a child of this and disable it 
     toReturn.transform.SetParent(transform); 
     toReturn.SetActive(false); 

     // add the instance to the collection of inactive instances 
     inactiveInstances.Push(toReturn); 
    } 
    // otherwise, just destroy it 
    else 
    { 
     Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying."); 
     Destroy(toReturn); 
    } 
} 
} 

// a component that simply identifies the pool that a GameObject came from 
public class PooledObject : MonoBehaviour 
{ 
    public SimpleObjectPool pool; 
} 
+0

を指針を与えるためにテストされていないのです、とバニラの「擬似」はあまりにも多くのコードのように見える、あなたは無関係なコードを削除してくださいだろうか? –

+0

私の答え[ここ](http://stackoverflow.com/questions/41631918/generate-10-unique-integers-in-c-sharp-for-unity/41632286#41632286)をチェックしてください。それはあなたが欲しいものですか? – Maakep

+2

すべての質問をシャッフルします。それから、質問を1からn – bixarrio

答えて

3

だそれはC#あなた

private List<int> _questions = null; 
private int _current = 0; 
void Start() 
{ 
    _questions = Enumerable.Range(0, questionPool.Length).ToList(); 
    Shuffle(_questions); 
} 
void Shuffle(List<int> list) 
{ 
    // put fisher-yates algorithm here to shuffle the numbers 
} 
void ShowQuestion() 
{ 
    var idx = _questions[_current++]; 
    var questionData = questionPool[idx]; 
} 
+0

インデックス自体をランダム化して、選んだインデックスを削除できないのですか? idkは実際にはとても混乱していて、これは簡単ですが...。 – Nanopili

+0

質問を削除する必要はありません。インデックスはランダムでユニークです。リストを実行するだけであれば、すでに使用されているインデックスは使用しません。 – bixarrio

+0

あなたが望むことをするには、インデックスで質問を削除します。配列とは対照的に質問のリストを使用する方が簡単です。 'questionPool.RemoveAt(i)'。 – bixarrio

関連する問題