简体   繁体   English

Unity检查按钮是否为空并将其隐藏

[英]Unity check if button is empty and hide it


Hello.你好。 Im making a quiz game and I need you help.我正在做一个问答游戏,我需要你的帮助。
Im getting questions and answers from server and I display them in game like this:我从服务器获取问题和答案,并在游戏中显示它们,如下所示:

    questionText.text = results.Result[0].question;
    button1Text.text = results.Result[0].answer1;
    button2Text.text = results.Result[0].answer2;
    button3Text.text = results.Result[0].answer3;
    button4Text.text = results.Result[0].answer4c;

For some questions I have only 2 answers, and then 2 buttons are left empty, looks something like this对于某些问题,我只有 2 个答案,然后 2 个按钮为空,看起来像这样
I need to make some kind of checker for buttons, if button is empty it gets hidden, but if there is something in button it gets shown.我需要为按钮制作某种检查器,如果按钮为空,它会被隐藏,但如果按钮中有东西,它会显示出来。
Thanks!谢谢!

You could use string.IsNullOrEmpty to check if the answer is empty, for example like this:您可以使用string.IsNullOrEmpty来检查答案是否为空,例如:

questionText.text = results.Result[0].question;
button1Text.text = results.Result[0].answer1;
button2Text.text = results.Result[0].answer2;
button3Text.text = results.Result[0].answer3;
button4Text.text = results.Result[0].answer4c;

// Hide buttons if empty. So we invert the isNullOrEmpty (when it's true -> we do not want to show the button!)
// As corrected below by @dohavin. We take the parent of the text object, assuming that is the actual button object.
button1Text.transform.parent.gameObject.setActive(!string.IsNullOrEmpty(results.Result[0].answer1));
button2Text.transform.parent.gameObject.setActive(!string.IsNullOrEmpty(results.Result[0].answer2));
button3Text.transform.parent.gameObject.setActive(!string.IsNullOrEmpty(results.Result[0].answer3));
button4Text.transform.parent.gameObject.setActive(!string.IsNullOrEmpty(results.Result[0].answer4c));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM