Search Unity

Disabling Toggle when used as a menu item

Discussion in 'Scripting' started by eco_bach, Mar 14, 2020.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    So I am using Toggle and ToggleGroup as the basis for a menu.

    The only issue is I need to disable any given toggle once it is selected.

    Cannot simply use 'enabled=false' because that breaks the ToggleGroup-Toggle relationship!
    This is my custom class extending Toggle.

    Any suggestions welcome. Am pulling my hair out.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class PointerDownToggle : Toggle {
    8.        
    9.     string _selected;
    10.     public override void OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
    11.     {
    12.        // this.enabled=false;// won't work as it breaks the ToggleGroup ie button state remains selected
    13.     }
    14.  
    15.     public override void OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData)
    16.     {
    17.       base.OnPointerClick (eventData);
    18.         group.NotifyToggleOn(this);        
    19.     }
    20. }
    21.  
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
  3. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Thanks. So no way to turn off a specific toggle and instead it seems you need to do a check for .isOn before any callbacks are called.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    togglegroup can't be switched off by its very design. it's basically a radio button, one is always lit.
    however, this is exactly what allowSwitchOff is for. they've let you modify the behavior if you need that.