262 lines
8.5 KiB
C#
262 lines
8.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
using TMPro;
|
|
|
|
// 流程 : 勾同意書 → 按開始 → 跳出地圖/武器選擇視窗 → 按確認 → 存 JSON → 切到選擇的地圖場景
|
|
// 自動配置 : 有勾的話按開始就直接進遊戲,地圖 / 武器 / FOV / 輔助都由 SessionPlan 決定
|
|
// 測試 : 按測試 → 用目前設定存 JSON → 直接進測試場景 (不記錄資料,不需勾同意書)
|
|
// 執行順序 10 : 回填上次的設定要排在 Fov.cs 等腳本的 Start() 之後,不然會被預設值蓋掉
|
|
[DefaultExecutionOrder(10)]
|
|
public class SettingsSaver : MonoBehaviour
|
|
{
|
|
[Header("開始與同意書")]
|
|
[Tooltip("開始 Button")]
|
|
public Button startButton;
|
|
|
|
[Tooltip("同意書 Toggle,不勾就不能開始")]
|
|
public Toggle consentToggle;
|
|
|
|
[Header("自動配置")]
|
|
[Tooltip("自動配置 Toggle (正式錄製),建議在場景裡預設勾選")]
|
|
public Toggle autoPlanToggle;
|
|
|
|
[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);
|
|
}
|
|
|
|
RestoreLastSettings();
|
|
}
|
|
|
|
// 回填上次的 ID、靈敏度、FOV (不用每次重打)
|
|
// 自動配置不從檔案回填 : 舊版的設定檔沒有這一項,會變成沒勾 ; 以場景的預設為準,這次執行用過就維持勾選
|
|
void RestoreLastSettings()
|
|
{
|
|
if (autoPlanToggle != null && SessionPlan.Started) autoPlanToggle.isOn = true;
|
|
if (!GameSettings.Exists) return;
|
|
|
|
GameSettings last = GameSettings.Load();
|
|
if (idManager != null && idManager.inputField != null && last.contributorID != "Anonymous")
|
|
{
|
|
idManager.inputField.text = last.contributorID;
|
|
}
|
|
if (sensitivitySlider != null) sensitivitySlider.value = (float)last.sensitivity;
|
|
if (fovSlider != null) fovSlider.value = last.fov;
|
|
}
|
|
|
|
// 要勾同意書,而且 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 (IsAutoPlan)
|
|
{
|
|
SaveAndLoad(CollectSettings());
|
|
return;
|
|
}
|
|
|
|
if (selectPanel != null)
|
|
{
|
|
selectPanel.SetActive(true);
|
|
}
|
|
}
|
|
|
|
void OnCancelClicked()
|
|
{
|
|
if (selectPanel != null)
|
|
{
|
|
selectPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
void OnConfirmClicked()
|
|
{
|
|
SaveAndLoad(CollectSettings());
|
|
}
|
|
|
|
bool IsAutoPlan => autoPlanToggle != null && autoPlanToggle.isOn;
|
|
|
|
void OnTestClicked()
|
|
{
|
|
GameSettings settings = CollectSettings(true);
|
|
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();
|
|
SessionPlan.ApplyFrameRate(settings);
|
|
Debug.Log($"設定已存到 : {GameSettings.FilePath}");
|
|
|
|
SceneManager.LoadScene(settings.map);
|
|
}
|
|
|
|
GameSettings CollectSettings(bool isTest = false)
|
|
{
|
|
GameSettings s = new GameSettings();
|
|
|
|
if (idManager != null) s.contributorID = idManager.GetFinalContributorID();
|
|
|
|
// 與 Sensitivity.cs 相同的 0.1 階梯
|
|
if (sensitivitySlider != null) s.sensitivity = Math.Round(sensitivitySlider.value * 10.0) / 10.0;
|
|
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);
|
|
|
|
// 自動配置 : 任務全部鎖定 (測試模式維持自由選擇) ; 輔助條件由排程每回合決定,這裡一律存無輔助
|
|
if (IsAutoPlan && !isTest)
|
|
{
|
|
s.autoPlan = true;
|
|
s.map = SessionPlan.Map;
|
|
s.weapon = SessionPlan.Weapon;
|
|
s.fov = SessionPlan.Fov;
|
|
s.assistMode = 0;
|
|
s.assistStrength = 0;
|
|
s.disableHorizontalAssist = false;
|
|
s.disableVerticalAssist = false;
|
|
s.assistDelayMs = 0;
|
|
s.assistDelayJitterMs = 0;
|
|
}
|
|
|
|
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)];
|
|
}
|
|
}
|