androidiosunity-game-enginematerial-design

UI Switch in Unity


I'm working on cross-platform mobile Unity application. I want add a switch on my screen allowing user turn on and off some functionality (like this: Android Material Switch).

As I know there is no native implementation of UI Switches in Unity. I found only this asset (Switch UI element) in Unity Asset Store, but it has really poor design.

So should I manually implement the Material Switch? Or is there anything in the web I can use in this case?

Thanks for response.


Solution

  • You can use slider as switch.

    Here are simple steps:

    1. Create a slider by going to GameObject->UI->Slider
    2. Check whole numbers chech-box in slider component's inspector.
    3. set min value to 0 and max value to 1.
    4. Use whatever graphics you want (even from material design)
    5. Register "OnValueChange" method to handle change in value:

      public void SliderValueChanged(float value)
      {
          if(value == 0)
              // off
          else 
              // on
      }