首页 > 解决方案 > 如何将单击的按钮文本值统一分配给另一个选定的按钮文本值?

问题描述

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


    }

 } 

标签: c#arraysunity3dbuttontext

解决方案


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

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


推荐阅读