You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
819 B
38 lines
819 B
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using System.Collections;
|
|
|
|
[RequireComponent(typeof(Image))]
|
|
[RequireComponent(typeof(Toggle))]
|
|
public class ToggleButtonIcon : MonoBehaviour {
|
|
|
|
public Sprite onIcon;
|
|
public Sprite offIcon;
|
|
private Toggle toggleButton;
|
|
|
|
void Start()
|
|
{
|
|
// This automatically registers the event click on the button component
|
|
toggleButton = GetComponent<Toggle>();
|
|
toggleButton.onValueChanged.AddListener(Click);
|
|
SetIcon();
|
|
}
|
|
|
|
public void Click(bool newValue)
|
|
{
|
|
SetIcon();
|
|
}
|
|
|
|
private void SetIcon()
|
|
{
|
|
if (toggleButton.isOn)
|
|
{
|
|
GetComponent<Image>().sprite = onIcon;
|
|
}
|
|
else
|
|
{
|
|
GetComponent<Image>().sprite = offIcon;
|
|
}
|
|
}
|
|
}
|