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

30 lines
680 B
C#
Raw Permalink Normal View History

2021-03-08 17:26:33 +01:00
using System;
2021-03-08 16:13:19 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2021-03-08 17:26:33 +01:00
using UnityEngine.UI;
2021-03-08 16:13:19 +01:00
2021-03-08 17:26:33 +01:00
[RequireComponent(typeof(Slider))]
2021-03-08 16:13:19 +01:00
public class PlayerPrefSlider : MonoBehaviour
{
2021-03-08 17:26:33 +01:00
private Slider _slider;
2021-03-08 18:05:33 +01:00
[SerializeField]
private float _defaultValue;
2021-03-08 17:26:33 +01:00
[SerializeField]
private PlayerPrefValue _playerPref;
private void Awake()
2021-03-08 16:13:19 +01:00
{
2021-03-08 17:26:33 +01:00
_slider = GetComponent<Slider>();
_slider.onValueChanged.AddListener( _playerPref.SetFloat );
2021-03-08 16:13:19 +01:00
}
2021-03-08 17:26:33 +01:00
private void OnEnable()
2021-03-08 16:13:19 +01:00
{
2021-03-08 18:05:33 +01:00
float value = _playerPref.GetFloat(_defaultValue);
2021-03-08 17:26:33 +01:00
_slider.SetValueWithoutNotify( value );
2021-03-08 16:13:19 +01:00
}
}