【问题标题】:How to generate random data without repeating in unity C#如何在统一C#中生成随机数据而不重复
【发布时间】:2017-02-28 17:49:40
【问题描述】:

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

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

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

【问题讨论】:

标签: c# unity3d unity5


【解决方案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());
}

【讨论】:

  • 非常感谢!帮了我很多。我面临的唯一问题是 UserSelected() 方法由于某种原因不会触发动画并启动 couroutine。但是如果我使用以前的方法 UserSelectTrue() 和 UserSelectFalse() 它工作正常。我认为问题出在 IsTrue.ToString()。
  • 完全没问题!我编辑了答案,那肯定可以解决触发问题。如果您发现此答案有帮助,请考虑将其标记为 :)
  • 对不起!一切正常。这是我的错误,我忘了在 On Click() 中添加方法。
  • 嘿!正如你理解我的代码。你能看看这个..stackoverflow.com/questions/42594257/…
猜你喜欢
  • 2015-07-12
  • 2014-03-11
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
相关资源
最近更新 更多