﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

[RequireComponent(typeof(Toggle))]
public class AudioToggle : MonoBehaviour
{
    public AudioMixer mixer;
    public string key;
    public AnimationCurve curve;

    Toggle toggle;

    private void Awake()
    {
        toggle = GetComponent<Toggle>();

        toggle.SetIsOnWithoutNotify(PlayerPrefs.GetInt(key + "_mute", 0) > 0);

        toggle.onValueChanged.AddListener(OnValueChanged);
    }

    void OnValueChanged(bool val)
    {
        if (!val)
            mixer.SetFloat(key, curve.Evaluate(PlayerPrefs.GetFloat(key, 0)));
        else
            mixer.SetFloat(key, -80);

        PlayerPrefs.SetInt(key + "_mute", val ? 1 : 0);
    }

}
