212 lines
6.4 KiB
C#
212 lines
6.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
using TMPro;
|
|
|
|
// 流程 : 勾同意書 → 按開始 → 跳出地圖/武器選擇視窗 → 按確認 → 存 JSON → 切到選擇的地圖場景
|
|
// 測試 : 按測試 → 用目前設定存 JSON → 直接進測試場景 (不記錄資料,不需勾同意書)
|
|
public class SettingsSaver : MonoBehaviour
|
|
{
|
|
[Header("開始與同意書")]
|
|
[Tooltip("開始 Button")]
|
|
public Button startButton;
|
|
|
|
[Tooltip("同意書 Toggle,不勾就不能開始")]
|
|
public Toggle consentToggle;
|
|
|
|
[Header("一般設定")]
|
|
public IDManager idManager;
|
|
public Slider sensitivitySlider;
|
|
public Slider fovSlider;
|
|
|
|
[Header("輔助設定")]
|
|
public TMP_Dropdown assistDropdown;
|
|
public Slider assistStrengthSlider;
|
|
public Toggle disableHorizontalToggle;
|
|
public Toggle disableVerticalToggle;
|
|
public Slider assistDelaySlider;
|
|
|
|
[Tooltip("延遲抖動範圍 Slider (±N ms)")]
|
|
public Slider delayJitterSlider;
|
|
|
|
[Header("地圖與武器選擇視窗")]
|
|
[Tooltip("按開始後跳出的視窗 (Panel)")]
|
|
public GameObject selectPanel;
|
|
|
|
[Tooltip("確認開始 Button")]
|
|
public Button confirmButton;
|
|
|
|
[Tooltip("取消 Button")]
|
|
public Button cancelButton;
|
|
|
|
public TMP_Dropdown mapDropdown;
|
|
public TMP_Dropdown weaponDropdown;
|
|
|
|
[Header("選項名稱 (順序對應 Dropdown 選項)")]
|
|
[Tooltip("地圖的場景名稱 (需加入 Build Profiles 的場景清單)")]
|
|
public string[] mapSceneNames = { "Forest", "Basement", "Desert" };
|
|
|
|
[Tooltip("武器名稱,會存進 JSON")]
|
|
public string[] weaponNames = { "M1911", "AK74", "Uzi", "Benelli_M4" };
|
|
|
|
[Header("測試")]
|
|
[Tooltip("測試 Button")]
|
|
public Button testButton;
|
|
|
|
[Tooltip("測試場景名稱 (需加入 Build Profiles 的場景清單)")]
|
|
public string testSceneName = "Test";
|
|
|
|
[Tooltip("測試時使用的武器")]
|
|
public string testWeapon = "M1911";
|
|
|
|
void Start()
|
|
{
|
|
if (selectPanel != null)
|
|
{
|
|
selectPanel.SetActive(false);
|
|
}
|
|
|
|
if (consentToggle != null)
|
|
{
|
|
consentToggle.onValueChanged.AddListener(OnConsentChanged);
|
|
OnConsentChanged(consentToggle.isOn);
|
|
}
|
|
|
|
if (startButton != null)
|
|
{
|
|
startButton.onClick.AddListener(OnStartClicked);
|
|
}
|
|
|
|
if (confirmButton != null)
|
|
{
|
|
confirmButton.onClick.AddListener(OnConfirmClicked);
|
|
}
|
|
|
|
if (cancelButton != null)
|
|
{
|
|
cancelButton.onClick.AddListener(OnCancelClicked);
|
|
}
|
|
|
|
if (testButton != null)
|
|
{
|
|
testButton.onClick.AddListener(OnTestClicked);
|
|
}
|
|
}
|
|
|
|
// 要勾同意書,而且 Raw Input 正常運作 (沒有它就拿不到每一筆的真實時間,資料不能用) ; 測試不受影響
|
|
void OnConsentChanged(bool isOn)
|
|
{
|
|
if (startButton != null)
|
|
{
|
|
startButton.interactable = isOn && RawMouseInput.IsRunning;
|
|
}
|
|
}
|
|
|
|
void OnStartClicked()
|
|
{
|
|
if (consentToggle != null && !consentToggle.isOn) return;
|
|
if (!RawMouseInput.IsRunning)
|
|
{
|
|
Debug.LogWarning($"Raw Input 沒有運作 ({RawMouseInput.Status}),無法開始收集資料");
|
|
return;
|
|
}
|
|
|
|
if (selectPanel != null)
|
|
{
|
|
selectPanel.SetActive(true);
|
|
}
|
|
}
|
|
|
|
void OnCancelClicked()
|
|
{
|
|
if (selectPanel != null)
|
|
{
|
|
selectPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
void OnConfirmClicked()
|
|
{
|
|
SaveAndLoad(CollectSettings());
|
|
}
|
|
|
|
void OnTestClicked()
|
|
{
|
|
GameSettings settings = CollectSettings();
|
|
settings.map = testSceneName;
|
|
settings.weapon = testWeapon;
|
|
settings.isTest = true;
|
|
|
|
SaveAndLoad(settings);
|
|
}
|
|
|
|
void SaveAndLoad(GameSettings settings)
|
|
{
|
|
if (!Application.CanStreamedLevelBeLoaded(settings.map))
|
|
{
|
|
Debug.LogError($"找不到場景 \"{settings.map}\",請確認已加入 Build Profiles 的場景清單");
|
|
return;
|
|
}
|
|
|
|
settings.Save();
|
|
Debug.Log($"設定已存到 : {GameSettings.FilePath}");
|
|
|
|
SceneManager.LoadScene(settings.map);
|
|
}
|
|
|
|
GameSettings CollectSettings()
|
|
{
|
|
GameSettings s = new GameSettings();
|
|
|
|
if (idManager != null) s.contributorID = idManager.GetFinalContributorID();
|
|
|
|
// 與 Sensitivity.cs 相同的 0.1 階梯
|
|
if (sensitivitySlider != null) s.sensitivity = Mathf.Round(sensitivitySlider.value * 10f) / 10f;
|
|
if (fovSlider != null) s.fov = Mathf.RoundToInt(fovSlider.value);
|
|
|
|
if (assistDropdown != null) s.assistMode = assistDropdown.value;
|
|
if (disableHorizontalToggle != null) s.disableHorizontalAssist = disableHorizontalToggle.isOn;
|
|
if (disableVerticalToggle != null) s.disableVerticalAssist = disableVerticalToggle.isOn;
|
|
|
|
// 水平和垂直都取消 = 實際上沒有任何輔助,存成無輔助,避免資料標籤錯誤
|
|
if (s.disableHorizontalAssist && s.disableVerticalAssist) s.assistMode = 0;
|
|
|
|
// 無輔助時,輔助相關的設定一律存預設值
|
|
if (s.assistMode == 0)
|
|
{
|
|
s.disableHorizontalAssist = false;
|
|
s.disableVerticalAssist = false;
|
|
}
|
|
|
|
if (assistStrengthSlider != null && s.assistMode != 0)
|
|
{
|
|
s.assistStrength = Mathf.RoundToInt(assistStrengthSlider.value);
|
|
}
|
|
if (assistDelaySlider != null && s.assistMode != 0)
|
|
{
|
|
s.assistDelayMs = Mathf.RoundToInt(assistDelaySlider.value);
|
|
if (delayJitterSlider != null) s.assistDelayJitterMs = Mathf.RoundToInt(delayJitterSlider.value);
|
|
}
|
|
|
|
s.map = PickName(mapSceneNames, mapDropdown);
|
|
s.weapon = PickName(weaponNames, weaponDropdown);
|
|
|
|
s.screenWidth = Screen.width;
|
|
s.screenHeight = Screen.height;
|
|
s.refreshRate = Mathf.RoundToInt((float)Screen.currentResolution.refreshRateRatio.value);
|
|
s.savedTime = DateTime.Now.ToString("o");
|
|
|
|
return s;
|
|
}
|
|
|
|
// 依 Dropdown 目前的選項取出對應名稱,沒接 Dropdown 就用第一個
|
|
string PickName(string[] names, TMP_Dropdown dropdown)
|
|
{
|
|
if (names == null || names.Length == 0) return "";
|
|
|
|
int index = dropdown != null ? dropdown.value : 0;
|
|
return names[Mathf.Clamp(index, 0, names.Length - 1)];
|
|
}
|
|
}
|