UPF/Assets/Game/Scripts/ControlSettings.cs
2023-02-24 22:14:55 +05:30

55 lines
1.7 KiB
C#
Executable File

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class ControlSettings : MonoBehaviour
{
private const string ControlOnRightKey = "ControlOnRight";
private const string HapticKey = "ControlOnRight";
public Button left;
public Button right;
public Toggle HapticToggle;
public static bool ControlIsOnRight = true;
public static bool HapticEnabled = true;
public UnityEvent OnChanged;
void Awake()
{
if(PlayerPrefs.HasKey(ControlOnRightKey)){
ControlIsOnRight = PlayerPrefs.GetInt(ControlOnRightKey) == 1;
}
if(PlayerPrefs.HasKey(HapticKey)){
HapticEnabled = PlayerPrefs.GetInt(HapticKey) == 1;
}
left.onClick.AddListener(()=>{ChangeControlSide(true);});
right.onClick.AddListener(()=>{ChangeControlSide(false);});
HapticToggle.onValueChanged.AddListener(ToggleHaptic);
UpdateSettingsPage();
}
void ChangeControlSide(bool value){
ControlIsOnRight = value;
PlayerPrefs.SetInt(ControlOnRightKey, value? 1: 0);
PlayerPrefs.Save();
UpdateSettingsPage();
OnChanged.Invoke();
AudioManager.instnace.UIClick();
}
public void ToggleHaptic(bool value){
PlayerPrefs.SetInt(HapticKey, value ? 1 : 0);
PlayerPrefs.Save();
HapticEnabled = value;
}
void UpdateSettingsPage(){
left.transform.GetChild(0).GetComponent<Image>().color = (!ControlIsOnRight) ? Color.blue : Color.green;
right.transform.GetChild(0).GetComponent<Image>().color = (ControlIsOnRight) ? Color.blue : Color.green;
HapticToggle.isOn = HapticEnabled;
}
}