Unity | radio button -> check box -> radio button

Recent projects related to the radio, multiple choice question:

  1. Under normal circumstances, each picture is a radio, were selected after OCR text recognition;
  2. Click the Delete button, all pictures can be changed to multiple-choice state. Upon completion or cancel the deletion, and then into the radio state.

We all know that control whether the object is a series of conditions for the radio <Toggle> component Group whether the same, then the state becomes multiple choice, simply Group is set to Null.

My problem lies before canceling the delete operation, there are several picture is selected, then I need to use the following code to set isOn = false, and set the radio status:

 foreach (Transform item in content.transform)
 {
      Toggle toggle = item.Find("Toggle").GetComponent<Toggle>();

      toggle.group = content.GetComponent<ToggleGroup>();
      if (toggle.isOn)
      {
           toggle.isOn = false;
      }
 }

Logic appears to be right, but operating results are as follows (Note: Before you click the Delete button, all are in Image not selected):

You can see from the chart, there is a delete operation after canceling Image is selected, but I obviously put all isOn are set to false. . . Reflect for a long time, put the code into the bottom of this:

         foreach (Transform item in content.transform)
            {
                Toggle toggle = item.Find("Toggle").GetComponent<Toggle>();
                
                if (toggle.isOn)
                {
                    toggle.isOn = false;
                }
                toggle.group = content.GetComponent<ToggleGroup>();
            }

Comparative above two codes, and the sequence of operations just isOn group has changed, but this effect is on the right:

It concluded that: In multiple choice mode, select the number may be equal to 0, but in radio state, there must be a Image is selected, your isOn = false is no good drops. They must be in a multi-select state isOn = false, then the state is set to the radio, the radio can not be set to a state, after isOn = false.

 

 

Published 162 original articles · won praise 20 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/90752096