Toggle中的四个状态同Button一样,核心部分是可以勾选白框部分,Is on表示其状态,可以通过Is on的true和false来触发需要的方法
我们可以通过创建一个空物体,然后在该空物体下,,创建几个Tongle子物体,然后在空物体上加上一个组件 ToggleGroup,可以实现
这个空物体下的几个Toggle只可以勾选一个的效果. 其中ToggleGroup中的 AllowSwitchOff勾选上表示允许一个都不勾选,否则必须
勾选且只能勾选一个.
附上Toggle的API
Toggle
Namespace: UnityEngine.UI
/
Inherits from: UI.Selectable
Implements interfaces: ICanvasElement, IDeselectHandler, IEventSystemHandler,
IEventSystemHandler, IMoveHandler, IPointerClickHandler, IPointerDownHandler,
IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler, ISelectHandler, ISubmitHandler
Description
A standard toggle that has an on / off state.
The toggle component is a Selectable that controls a child graphic which displays the on / off state.
When a toggle event occurs a callback is sent to any registered listeners of onValueChanged.
Variables
graphic | Graphic affected by the toggle. |
group | Group the toggle belongs to. |
isOn | Is the toggle on. |
onValueChanged | Callback executed when the value of the toggle is changed. |
toggleTransition | Transition mode for the toggle. |
Public Functions
OnPointerClick | Handling for when the toggle is 'clicked'. |
OnSubmit | Handling for when the submit key is pressed. |
Rebuild | Handling for when the canvas is rebuilt. |
Inherited members
Static Variables
allSelectables | List of all the selectable objects currently active in the scene. |
Variables
enabled | Enabled Behaviours are Updated, disabled Behaviours are not. |
isActiveAndEnabled | Has the Behaviour had enabled called. |
gameObject | The game object this component is attached to. A component is always attached to a game object. |
tag | The tag of this game object. |
transform | The Transform attached to this GameObject (null if there is none attached). |
useGUILayout | Disabling this lets you skip the GUI layout phase. |
hideFlags | Should the object be hidden, saved with the scene or modifiable by the user? |
name | The name of the object. |
animationTriggers | The AnimationTriggers for this selectable object. |
animator | Convenience function to get the Animator component on the GameObject. |
colors | The ColorBlock for this selectable object. |
image | Convenience function that converts the referenced Graphic to a Image, if possible. |
interactable | UI.Selectable.interactable. |
navigation | The Navigation setting for this selectable object. |
spriteState | The SpriteState for this selectable object. |
targetGraphic | Graphic that will be transitioned upon. |
transition | The type of transition that will be applied to the targetGraphic when the state changes. |