【问题标题】:How to assign clicked button text value to another selected Button Text Value in unity?如何将单击的按钮文本值统一分配给另一个选定的按钮文本值?
【发布时间】:2019-07-15 00:51:30
【问题描述】:

我正在显示 4 个问题,用户必须从底部给定的答案按钮数组中选择答案。要回答第一个问题 (22 + 6),用户必须选择旁边给出的黄色按钮,然后单击底部列表中的任何按钮。单击任何答案按钮时,我想将该值分配给提供的黄色按钮。然后对于下一个问题(16 + 7),用户必须再次选择另一个黄色按钮并从列表中单击答案按钮。我无法将单击的答案按钮的值分配给选定的黄色按钮。谁能建议我如何实现这一目标?

public class WithButtons : MonoBehaviour
{
     Text[] userInputs;
     public Text aValue = null, bValue = null, cValue = null, dValue = null;
     int a, b, c, d;
     public GameObject[] ansButtons;
     private GameObject tempGO;
     Text[] answers;

     private Button myButton;

     //-----------------------------------------------------------------------//
     void Start()
     {        
         PuzzleMaker();
     }

     public void PuzzleMaker()
     {

         a = Random.Range(0, 25);
         b = Random.Range(0, 25);
         c = Random.Range(0, 25);
         d = Random.Range(0, 25);

         aValue.text = a.ToString();
         bValue.text = b.ToString();
         cValue.text = c.ToString();
         dValue.text = d.ToString();


         ansButtons[0].GetComponentInChildren<Text>().text = (a + b).ToString();
         ansButtons[1].GetComponentInChildren<Text>().text = (c + d).ToString();
         ansButtons[2].GetComponentInChildren<Text>().text = (a + c).ToString();
         ansButtons[3].GetComponentInChildren<Text>().text = (b + d).ToString();
         ansButtons[4].GetComponentInChildren<Text>().text = (a + a).ToString();
         ansButtons[5].GetComponentInChildren<Text>().text = (b + b).ToString();
         ansButtons[6].GetComponentInChildren<Text>().text = (c + c).ToString();
         ansButtons[7].GetComponentInChildren<Text>().text = (d + d).ToString();           

     }

     //check which button is clicked
     public void WhichButtonIsClicked(string buttonName)
     {
         Debug.Log(buttonName);

         if (buttonName == "AB")
         {
             //assign value of clicked button to ? button Q1
             //userInput1.text = 
         }
         else if (buttonName == "CD")
         {
             //assign value of clicked button to ? button Q2

         }
         else if (buttonName == "AC")
         {
             //assign value of clicked button to ? button Q3

         }
         else if (buttonName == "BD")
         {
             //assign value of clicked button to ? button Q4

         }
     }    

    public void OnEnable()
     {
         myButton = GetComponent<Button>();
         myButton.onClick.AddListener(() => { ChangeTarget(); });
     }

    public void ChangeTarget()
    {        
        int i;
        for (i = 0; i < ansButtons.Length; i++)
        {
            userInputs[i].text = 
            UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.GetComponentInChildren<Text>().text.ToString();
        }      


    }

 } 

【问题讨论】:

  • @Jannis 我已经分配了 4 个答案和 4 个随机值。他们有更好的方法吗?我是统一的新手,所以不确定。
  • 我先分配了 8 个值,然后我将这些值改组,所以我每次都不会在同一个位置得到答案
  • 哦,好的。然后我将删除循环并简单地分配值并保持 shuffle.wil 可以吗?
  • 删除了谢谢。但是你能指导我将点击的按钮文本分配给选定的按钮吗

标签: c# arrays unity3d button text


【解决方案1】:

我有一个解决方案。哟可以查

公共部分类 Form1 : Form {

    public int _valueBtn1;
    public int _valueBtn2;
    public int _valueSelectedBtn;
    public bool wasClicked1 = false;
    public bool wasClicked2 =  false;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    // BUTTON [?] - TOP
    private void button3_Click(object sender, EventArgs e)
    {

        wasClicked1 = true;


    }

    // BUTTON [5]
    private void button4_Click(object sender, EventArgs e)
    {
        if (wasClicked1)
        {
            _valueSelectedBtn = Convert.ToInt32(this.button4.Text);
            button3.Text = _valueSelectedBtn.ToString();
            wasClicked1 = false;
        }
        else if (wasClicked2)
        {
            _valueSelectedBtn = Convert.ToInt32(this.button4.Text);
            button5.Text = _valueSelectedBtn.ToString();
            wasClicked2 = false;

        }
    }

    // BUTTON [?] - bottom 
    private void button5_Click(object sender, EventArgs e)
    {
        wasClicked2 = true;

    }

    // BUTTON [2]
    private void button8_Click(object sender, EventArgs e)
    {
        if (wasClicked1)
        {
            _valueSelectedBtn = Convert.ToInt32(this.button8.Text);
            button3.Text = _valueSelectedBtn.ToString();
            wasClicked1 = false;
        }
        else if (wasClicked2)
        {
            _valueSelectedBtn = Convert.ToInt32(this.button8.Text);
            button5.Text = _valueSelectedBtn.ToString();
            wasClicked2 = false;

        }
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 Button.text。

    只需获取您单击的答案按钮的引用。

    
    string textAnswer ;
    void OnAnswerButton1(text buttonName){
    GameObject answerButton = GameObject.Find("buttonName");
     textAnswer = answerButton.text;
    
    }
    

    现在你在 textAnswer 变量中有那个答案按钮的文本。 所以把这个文本给“?”按钮

    你一定有一些“?”的参考。按钮。 所以就像下面那样做

    Button aBbutton; //reference of "?" button
    
    aBbutton.text = textAnswer;
    
    
    
    
    

    【讨论】:

      【解决方案3】:

      您可以如图所示分配文本:

      ansButtons[0].GetComponentInChildren<Text>().text = (a + b).ToString();
      

      你需要在这行代码中做与 up 基本相同的事情
      你需要你的?按钮和右边的ansButton

      如何获得这些参考?

      当按下黄色按钮时,您将获得第一个引用。
      你可能有这样的代码:

      Button selectedYellowButton = null;
      
      public void getMyYellowButton(string buttonNameEnding) {
      Button yellowButton = GameObject.find("YellowButton" + buttonNameEnding);
      selectedYellowButton = yellowButton;
      }
      

      然后当按下 ansButton 时,您只需分配

      public void assignToYellowButton(string buttonNameEnding) {
      if (selectedYellowButton != null) {
           Button ansButton = GameObject.find("AnsButton" + buttonNameEnding);
           selectedYellowButton.GetComponentInChildren<Text>().text = ansButtons[indexInArray].GetComponentInChildren<Text>().text;
           }
      }
      

      您实际上可以使用任何方法来获取您喜欢的参考文献。
      由你决定:)

      希望对你有帮助:D

      【讨论】:

      • 非常感谢您的解释。我在我的 c# 文件中实现它。很快就会更新。
      • 在检查器中,我已将 assignToYellowButton 函数分配给所有 8 个答案按钮,并将 getMyYellowButton 函数分配给所有 4 个黄色按钮。但是当我选择任何黄色按钮并单击任何答案按钮时,相同的值被分配给黄色按钮。为什么我们必须传递 int indexInArray 值?
      • 我的意思是我已经将 0-7 int 值传递给每个答案按钮。但是当我单击任何答案按钮时,它没有将正确的按钮文本值传递给选定的黄色按钮。它正在从数组中提取值,而不是按钮上显示的内容
      • 我们可以简单地将 clicked button.text 分配给选中的黄色 button.text 吗?
      • @Sarita indexInArray 只是一个参考。您可以再次使用Button ansButton = GameObject.find("AnsButton" + buttonNameEnding),您可以将 button.text 分配给 button.text,由您自己尝试。抱歉,解决方案无效
      猜你喜欢
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多