revival/game/Assets/Scripts/Settings/PlayerPrefToggle.cs

29 lines
703 B
C#
Raw Normal View History

2021-03-08 17:26:33 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Toggle))]
public class PlayerPrefToggle : MonoBehaviour
{
private Toggle _toggle;
[SerializeField]
private PlayerPrefValue _playerPref;
[SerializeField]
private bool _defaultValue;
private void Awake()
{
_toggle = GetComponent<Toggle>();
_toggle.onValueChanged.AddListener( ( ticked ) => _playerPref.SetInt( ticked ? 1 : 0 ) );
}
private void OnEnable()
{
bool value = _playerPref.HasValue ? _playerPref.GetInt() > 0 : _defaultValue;
_toggle.isOn = value;
}
}