如何将随机int值赋予多次调用的预制件(不重复相同的值)?

问题描述 投票:-1回答:3

我创建了一个预制文件,其中包含文本(a * b =)和输入字段(以获取用户的答案)。我用c#脚本调用了这个预制件5次。我已经为random.range(1,10)分配了一个&b,所以我可以得到5个不同的总和。但在我的情况下,我得到所有5个总和相同的值。

我尝试了foreach循环,它获得了给定范围内的随机数字,点击检查按钮,它显示红色的正确答案(不正确)。这是我第一次通过脚本多次调用预制件。所以需要一些帮助来解决它。

TestModeQuestionUI.cs

using Helper.Keyboard;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class TestModeQuestionUI : MonoBehaviour
{
internal RectTransform refRectTransform;
[SerializeField]
TextMeshProUGUI valueA; // valueB;
[SerializeField]
MyInputField AnswerInputField;
internal int id;
internal Action<TestModeQuestionUI> onSubmitValueOfInputFieldAction;
internal Action<TestModeQuestionUI> onSelectInputFieldAction;

Color defaultColorOfAnswer = new Color(0.19f, 0.19f, 0.19f, 1f);
Color correctColorOfAnswer = new Color(0f, 0.44f, 0f, 1f);
Color incorrectColorOfAnswer = new Color(1f, 0f, 0f, 1f);

public static TestModeQuestionUI curSelectedAnswerInputField;

//-------------------------------------------------------------------------

void Awake()
{
    refRectTransform = GetComponent<RectTransform>();
}


private void SetNextAnswerInputFieldAsSelected(TestModeQuestionUI 
curSelectedAnswerInputField)
{
    throw new NotImplementedException();
}

public void SetQuestionLabel(string v)
{
    valueA.text = v;        
}

public void ActiveAnswerInputField(bool active)
{
    AnswerInputField.gameObject.SetActive(active);
}

public int GetAnswerInputField()
{
    int result = -1;
    int.TryParse(AnswerInputField.textComponent.text, out result);
    return result;
}

public void SetAnswerInputField(string msg)
{
    AnswerInputField.textComponent.text = msg;
}

public void SelectAnswerInputField()
{
    DeSelectCurSelectedAnswerInputField();
    AnswerInputField.ActivateInputField();
    curSelectedAnswerInputField = this;   
}

public static void DeSelectCurSelectedAnswerInputField()
{
    if (curSelectedAnswerInputField != null)
    {            
       curSelectedAnswerInputField.AnswerInputField.DeactivateInputField();   
    }
       curSelectedAnswerInputField = null;
}

public void SetResultOfAnswerInputField(int result)
{
    switch (result)
    {
        //==================================
        // Default Color For Answer Has Not Checked
        case 0:
            AnswerInputField.textComponent.color = defaultColorOfAnswer;
            break;

        //==================================
        // Correct Color For Answer Has Checked Correct
        case 1:
            AnswerInputField.textComponent.color = correctColorOfAnswer;
            break;

        //==================================
        // Incorrect Color For Answer Has Checked Incorrect
        case 2:
            AnswerInputField.textComponent.color = incorrectColorOfAnswer;
            break;
    }
}

public void OnSelectInputField()
{
    //Debug.Log("On Select : " + id);
    if (curSelectedAnswerInputField != this)
        DeSelectCurSelectedAnswerInputField();
    curSelectedAnswerInputField = this;

    if (onSelectInputFieldAction != null)
        onSelectInputFieldAction(this);
}

public void OnSubmitValueOfInputField()
{
    if (onSubmitValueOfInputFieldAction != null)
        onSubmitValueOfInputFieldAction(this);
}

}

TestModeManager.cs

 public class TestModeManager : MonoBehaviour
 {
 public static TestModeManager instance;
 [SerializeField]
 GameObject refTestModeQuestionExampleParent;
 [SerializeField]
 GameObject refTestModeQuestionExamplePrefab;
 [SerializeField]
 GameObject checkButton;
 [SerializeField]
 GameObject nextButton;

 private int a, b;
 List<TestModeQuestionUI> testModeQuestionExampleList;


 void Start()
 {
    CreateUI();
 }

 void Update()
 {        
 #if UNITY_EDITOR
    if (Input.GetKeyDown(KeyCode.KeypadEnter))
    {    SetNextAnswerInputFieldAsSelected(TestModeQuestionUI.curSelectedAnswerInputField);          



    }
 #endif

    if (Input.GetKeyDown(KeyCode.Escape))
    {
        if (Keyboard.instance.gameObject.activeInHierarchy)
            Keyboard.Close();
        else
            Application.Quit();
    }
 }


 void CreateUI()
 {
    GameObject _GO;
    TestModeQuestionUI _TestModeQuestionUIRefrence;

    if (testModeQuestionExampleList == null)
        testModeQuestionExampleList = new List<TestModeQuestionUI>();

   //  a = UnityEngine.Random.Range(1, 20);
  //   b = UnityEngine.Random.Range(1, 10);

    for (int id = 1; id <= 5; id++)
    {
        _GO = Instantiate(refTestModeQuestionExamplePrefab, 
 refTestModeQuestionExampleParent.transform);
        _GO.name = "TestModeQuestion Example " + id;
        _TestModeQuestionUIRefrence = _GO.GetComponent<TestModeQuestionUI> 
 ();
        _TestModeQuestionUIRefrence.id = id;
        _TestModeQuestionUIRefrence.onSubmitValueOfInputFieldAction = 
  SetNextAnswerInputFieldAsSelected;
        testModeQuestionExampleList.Add(_TestModeQuestionUIRefrence);

  }

    ResetUI();               
  }

  void ResetUI()
  {
    // Reset Multiplication Examples
    a = UnityEngine.Random.Range(1, 10);
    b = UnityEngine.Random.Range(1, 10);
    foreach (TestModeQuestionUI _TestModeQuestionUIRefrence in 
  testModeQuestionExampleList)
    {
        _TestModeQuestionUIRefrence.SetQuestionLabel(a + " " + b + "  = ");

   //loop to get 5 different sums
   var questions = GameObject.FindGameObjectsWithTag("question");
   foreach (var question in questions)
   {
         // a++;
         //b++;
  }

   }
  //==================================
    // Set First Answer Input Field As Selected
    SetNextAnswerInputFieldAsSelected();

  }
  void SetNextAnswerInputFieldAsSelected(TestModeQuestionUI _ 
  TestModeQuestionUIRefrence = null)
  {
    if (_TestModeQuestionUIRefrence == null)
    {
        //==================================
        // Get First Input Field And Set As Selected
        _TestModeQuestionUIRefrence = GettestModeQuestionExampleList(1);
        if (_TestModeQuestionUIRefrence != null)
            _TestModeQuestionUIRefrence.SelectAnswerInputField();
    }
    else
    {
        //==================================
        // Get Next Input Field And Set As Selected
        _TestModeQuestionUIRefrence = 
  GettestModeQuestionExampleList(_TestModeQuestionUIRefrence.id + 1);
        if (_TestModeQuestionUIRefrence != null)
            _TestModeQuestionUIRefrence.SelectAnswerInputField();
        else
        {
            Keyboard.Close();
            StartCoroutine(highlighCheckButton());
        }
    }
  }

  TestModeQuestionUI GettestModeQuestionExampleList(int id)
  {
    foreach (TestModeQuestionUI _TestModeQuestionUIRefrence in 
  testModeQuestionExampleList)
    {
        if (_TestModeQuestionUIRefrence.id == id)
        {
            return _TestModeQuestionUIRefrence;
        }
    }
    return null;
  }
  IEnumerator EnableKeyboardAfterSometime(float time)
  {
    yield return new WaitForSeconds(time);
    Keyboard.Open();
  }

IEnumerator highlighCheckButton()
{
    checkButton.transform.localScale = new Vector3(1f, 1f, 1f);
    float animtionTime = 0.3f;
    float scaleUpTo = 1.2f;
    for (int i = 0; i < 4; i++)
    {
        yield return AnimationController.animate(scaleCheckButton, 
        animtionTime, 1f, scaleUpTo);
        yield return AnimationController.animate(scaleCheckButton, 
        animtionTime, scaleUpTo, 1f);
    }
    checkButton.transform.localScale = new Vector3(1f, 1f, 1f);
}

void scaleCheckButton(float value)
{
   checkButton.transform.localScale = new Vector3(value, value, value);
}

public void CheckButton()
{
    int answer;
    foreach (TestModeQuestionUI _TestModeQuestionUIRefrence in 
testModeQuestionExampleList)
    {
        answer = _TestModeQuestionUIRefrence.GetAnswerInputField();
        if ((a * b) == answer)
        {
            _TestModeQuestionUIRefrence.SetResultOfAnswerInputField(1);
        }
        else
        {
            _TestModeQuestionUIRefrence.SetResultOfAnswerInputField(2);
        }
    }

    checkButton.SetActive(false);
    nextButton.SetActive(true);
}

public void NextButton()
{
    ResetUI();
    nextButton.SetActive(false);

}
c# unity3d random duplicates
3个回答
0
投票

您应该在循环中获取随机值,否则您将获得相同的值。

foreach (TestModeQuestionUI _TestModeQuestionUIRefrence in testModeQuestionExampleList)
{
    int a = UnityEngine.Random.Range(1, 10);
    int b = UnityEngine.Random.Range(1, 10);
    _TestModeQuestionUIRefrence.SetQuestionLabel(a, b);
}

你也应该在qazxsw poi中保存qazxsw poi和qazxsw poi,因为它们在实例之间是不同的。

a

0
投票

如果你有一组值,并且你想确保你永远不会得到两倍,那么最好将它们放在池中,并在使用它们时将它们删除。

假设您希望在10个不同的数字中有5个可能的乘法。

b

现在你将vec作为向量2的数组,它将给出操作的两个值。当您使用它们时删除它们,您不能两次相同。

您的vec集合可以在其他方法中用于创建UI。


0
投票

你有很多UI的东西,但如果我是你,如果我不想得到重复,我会创建一个单独的脚本来获取这样的随机值:

TestModeQuestionUI

然后在你的public class TestModeQuestionUI : MonoBehaviour { private int a, b; public void SetQuestionLabel(int a, int b) { this.a = a; this.b = b; valueA.text = a + " " + b + " = "; } } 函数中你可以使用Vector2[] vec; private void GenerateOperations() { List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // store all needed values this.vec = new Vector2[5]; // your 5 results for (int i = 0; i < vec.Length; i++) { int x = GetValue(list); // get x value int y = GetValue(list); // get y value vec[i] = new Vector2(x,y); // store in the index of the collection Debug.Log(vec[i].ToString()); // print to check } } private static int GetValue(List<int>list) { int rand = Random.Range(0, list.Count); // Get a random value from 0 to how many left in the list (last value is discarded so no out of bound exception) int value = list[rand]; // Get the value at index list.RemoveAt(rand); // remove that entry in the collection so it does not get picked again return value; // return } 调用这个函数我之所以用这种方式实现它的原因是你的预制件实例可以在实例化时访问它,因为它是静态的你不会得到a和b的相同随机结果。但是,让我们说你可能得到public class RandomIntegers { private static List<int> myNumbersA = new List<int>(); private static List<int> myNumbersB = new List<int>(); public static void RandomValues(out int a,out int b) { if (myNumbersA.Count == 0) { for (int i = 0; i < 10; i++) { myNumbersA.Add(i + 1); myNumbersB.Add(i + 1); } } int indexA = Random.Range(0, myNumbersA.Count); int indexB = Random.Range(0, myNumbersB.Count); a = myNumbersA[indexA]; b = myNumbersB[indexB]; myNumbersA.RemoveAt(indexA); myNumbersB.RemoveAt(indexB); } } ResetUIRandomIntegers.RandomValues(out a,out b);6a8b8两个将导致a在最后。

© www.soinside.com 2019 - 2024. All rights reserved.