【问题标题】:Unity: Remove The selected object from random.rangeunity:从random.range中移除选中的对象
【发布时间】:2017-01-17 13:32:13
【问题描述】:

我有一个有多种选择的问答游戏。我想显示随机问题,但有时会多次选择相同的问题。我想从我的列表中删除选定的问题,这样它就不会再被选中了..

这是显示我的问题的游戏管理器 ** 检查 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";
}



}

问题和答案是使用从 Unity 站点获取的简单池系统生成的。

这是池化类

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;
}

【问题讨论】:

  • 代码太多了,能不能把不相关的代码去掉?
  • 检查我的答案here。这就是你想要的吗?
  • 随机播放所有问题。然后只需将问题从 1 输入到 n
  • @un-lucky 现在好点了吗?对不起!
  • @fredrik 不是真的 =/

标签: c# unity3d


【解决方案1】:

它未经测试,香草“伪”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];
}

【讨论】:

  • 我不能随机化索引本身并删除选择的索引吗? idk 我实际上很困惑,尽管这很容易......
  • 您不需要删除问题。索引是随机且唯一的。如果您只是浏览列表,您将不会使用已经使用过的索引。
  • 要做你想做的事,你会删除索引中的问题。当您使用问题列表而不是数组时,它会更容易; questionPool.RemoveAt(i).
猜你喜欢
  • 2016-10-19
  • 2016-12-11
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多