1
This commit is contained in:
113
Assets/BITKit/Unity/Scripts/UX/Settings/UXSettings.cs
Normal file
113
Assets/BITKit/Unity/Scripts/UX/Settings/UXSettings.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
#if UNITY_EDITOR
|
||||
using Types = FullscreenEditor.Types;
|
||||
#endif
|
||||
namespace BITKit.UX.Settings
|
||||
{
|
||||
public class UXSettings : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private UIDocument document;
|
||||
|
||||
[UXBindPath("resolution-dropdown")]
|
||||
private DropdownField _resolutionDropdown;
|
||||
[UXBindPath("sensitivity-slider")]
|
||||
private Slider _sensitivitySlider;
|
||||
[UXBindPath("fullscreen-toggle")]
|
||||
private Toggle _fullscreenToggle;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UXUtils.Inject(this);
|
||||
|
||||
|
||||
|
||||
_resolutionDropdown.choices = Screen.resolutions.Select(x => x.ToString()).ToList();
|
||||
_resolutionDropdown.SetValueWithoutNotify(Screen.currentResolution.ToString());
|
||||
_resolutionDropdown.RegisterValueChangedCallback(x =>
|
||||
{
|
||||
Data.Set(Constant.Environment.mat_setvideomode, x.newValue);
|
||||
});
|
||||
|
||||
_fullscreenToggle.SetValueWithoutNotify(Screen.fullScreen);
|
||||
_fullscreenToggle.RegisterValueChangedCallback(x =>
|
||||
{
|
||||
Data.Set(Constant.Environment.fullscreen, x.newValue);
|
||||
});
|
||||
|
||||
//_sensitivitySlider.SetValueWithoutNotify(PlayerConfig.Singleton.Sensitivity);
|
||||
_sensitivitySlider.RegisterValueChangedCallback(x =>
|
||||
{
|
||||
Data.Set(Constant.Environment.sensitivity, x.newValue);
|
||||
});
|
||||
|
||||
Data.AddListener<string>(Constant.Environment.mat_setvideomode, OnResolutionChanged);
|
||||
Data.AddListener<bool>(Constant.Environment.fullscreen,OnFullScreenChanged);
|
||||
Data.AddListener<float>(Constant.Environment.sensitivity,OnSensitivityChanged);
|
||||
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
Data.RemoveListender<string>(Constant.Environment.mat_setvideomode, OnResolutionChanged);
|
||||
Data.RemoveListender<bool>(Constant.Environment.fullscreen,OnFullScreenChanged);
|
||||
Data.RemoveListender<float>(Constant.Environment.sensitivity,OnSensitivityChanged);
|
||||
|
||||
PlayerPrefs.Save();
|
||||
});
|
||||
|
||||
if(PlayerPrefs.HasKey(Constant.Environment.fullscreen))
|
||||
Data.Set(Constant.Environment.fullscreen, PlayerPrefs.GetInt(Constant.Environment.fullscreen) == 1);
|
||||
if(PlayerPrefs.HasKey(Constant.Environment.sensitivity))
|
||||
Data.Set(Constant.Environment.sensitivity, PlayerPrefs.GetFloat(Constant.Environment.sensitivity));
|
||||
}
|
||||
|
||||
private async void OnSensitivityChanged(float obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread(destroyCancellationToken);
|
||||
|
||||
_sensitivitySlider.SetValueWithoutNotify(obj);
|
||||
|
||||
PlayerPrefs.SetFloat(Constant.Environment.sensitivity,obj);
|
||||
}
|
||||
|
||||
private async void OnFullScreenChanged(bool obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread(destroyCancellationToken);
|
||||
_fullscreenToggle.SetValueWithoutNotify(obj);
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (obj)
|
||||
{
|
||||
FullscreenEditor.Fullscreen.MakeFullscreen(Types.GameView);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var x in FullscreenEditor.Fullscreen.GetAllFullscreen())
|
||||
{
|
||||
x.Close();
|
||||
}
|
||||
}
|
||||
#else
|
||||
Screen.fullScreen = obj;
|
||||
#endif
|
||||
PlayerPrefs.SetInt(Constant.Environment.fullscreen, obj ? 1 : 0);
|
||||
}
|
||||
private async void OnResolutionChanged(string newResolution)
|
||||
{
|
||||
await UniTask.SwitchToMainThread(destroyCancellationToken);
|
||||
|
||||
_resolutionDropdown.SetValueWithoutNotify(newResolution);
|
||||
var resolution = Screen.resolutions.Single(x => x.ToString() == newResolution);
|
||||
|
||||
Screen.SetResolution(resolution.width,resolution.height,Data.Get<bool>(Constant.Environment.fullscreen));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user