如何将单击的按钮文本值统一分配给另一个选定的按钮文本值?

时间:2019-02-21 07:28:18

标签: c# arrays unity3d button text

我正在显示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();
        }      


    }

 } 

3 个答案:

答案 0 :(得分:0)

您已经可以分配文本,如下所示:

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

您基本上需要执行与本行代码相同的操作
你需要你的吗?按钮和右侧的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

答案 1 :(得分:0)

我有一个解决方案。哟可以检查一下 enter image description here

公共局部类Form1:表单     {

    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 :(得分:0)

您可以使用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;