如何生成随机数据而不重复统一C#

时间:2017-02-28 05:23:03

标签: c# unity3d unity5

目前我正在为我的游戏化项目开发一种测验游戏。当玩家达到某个分数并出现问题时,我冻结实际游戏。如果答案是正确的,它将提高播放器的速度。它将在循环中发生。

这里我使用loadscene加载索引场景以避免重复问题。这里的问题是当场景加载时它重新加载整个游戏而不是重新加载测验部分。有什么办法吗?

public class GameManager : MonoBehaviour
{
    public Question[] facts;
    private static List<Question> unansweredfacts;
    private Question currentfacts;

    [SerializeField]
    private Text FactText;

    [SerializeField]
    private float TimeBetweenFacts = 3f;

    [SerializeField]
    private Text TrueAnswerText;

    [SerializeField]
    private Text FalseAnswerText;

    [SerializeField]
    private Animator animator;

    [SerializeField]
    public GameObject canvasquiz;

    void Start()
    {
        if (unansweredfacts == null || unansweredfacts.Count == 0)
        {
            unansweredfacts = facts.ToList<Question>();
        }
        SetCurrentfact();
        Debug.Log(currentfacts.Fact + "is" + currentfacts.IsTrue);
    }

    void SetCurrentfact()
    {
        int RandomFactIndex = Random.Range(0, unansweredfacts.Count);
        currentfacts = unansweredfacts[RandomFactIndex];
        FactText.text = currentfacts.Fact;
        if (currentfacts.IsTrue)
        {
            TrueAnswerText.text = "CORRECT !";
            FalseAnswerText.text = "WRONG !";
        }
        else
        {
            TrueAnswerText.text = "WRONG !";
            FalseAnswerText.text = "CORRECT !";
        }
    }

    IEnumerator TransitionToNextFact()
    {
        unansweredfacts.Remove(currentfacts);
        yield return new WaitForSeconds(TimeBetweenFacts);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void UserSelectTrue()
    {
        animator.SetTrigger("True");
        if (currentfacts.IsTrue)
        {
            Debug.Log("CORRECT !");
        }
        else
        {
            Debug.Log("WRONG !");
        }
        StartCoroutine(TransitionToNextFact());
    }

    public void UserSelectFalse()
    {
        animator.SetTrigger("False");
        if (!currentfacts.IsTrue)
        {
            Debug.Log("CORRECT !");
        }
        else
        {
            Debug.Log("WRONG !");
        }
        StartCoroutine(TransitionToNextFact());
    }

1 个答案:

答案 0 :(得分:1)

再次加载场景基本上重启它,你想要做的是以下面的方式改变TransitionToNextFact方法

IEnumerator TransitionToNextFact()
{
    unansweredfacts.Remove(currentfacts); // remove the last shown question for the list
    canvasquiz.SetActive(false); // disables the quiz canvas until the next question
    yield return new WaitForSeconds(TimeBetweenFacts);

    SetCurrentfact(); // sets the next random question from the list
    canvasquiz.SetActive(true); // show the quiz canvas along with the new question
}

我还将两种方法 UserSelectTrue UserSelectFalse 合并为一个

public void UserSelected(bool isTrue)
{
    animator.SetTrigger(isTrue ? "True" : "False");
    if (currentfacts.IsTrue == isTrue)
    {
        Debug.Log("CORRECT !");

    }
    else
    {
        Debug.Log("WRONG !");

    }


    StartCoroutine(TransitionToNextFact());
}