Settings.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using System.ComponentModel;
  2. using UnityEngine.Serialization;
  3. namespace SRDebugger
  4. {
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.IO;
  9. using System.Linq;
  10. using SRF;
  11. using UnityEngine;
  12. #if UNITY_EDITOR
  13. using UnityEditor;
  14. #endif
  15. #pragma warning disable 0649
  16. public enum DefaultTabs
  17. {
  18. SystemInformation = 0,
  19. Options = 1,
  20. Console = 2,
  21. Profiler = 3,
  22. BugReporter = 4
  23. }
  24. public enum PinAlignment
  25. {
  26. TopLeft = 0,
  27. TopRight = 1,
  28. BottomLeft = 2,
  29. BottomRight = 3,
  30. CenterLeft = 4,
  31. CenterRight = 5,
  32. TopCenter = 6,
  33. BottomCenter = 7
  34. }
  35. public enum ConsoleAlignment
  36. {
  37. Top,
  38. Bottom
  39. }
  40. public class Settings : ScriptableObject
  41. {
  42. public enum ShortcutActions
  43. {
  44. None = 0,
  45. OpenSystemInfoTab = 1,
  46. OpenConsoleTab = 2,
  47. OpenOptionsTab = 3,
  48. OpenProfilerTab = 4,
  49. OpenBugReporterTab = 5,
  50. ClosePanel = 6,
  51. OpenPanel = 7,
  52. TogglePanel = 8,
  53. ShowBugReportPopover = 9,
  54. ToggleDockedConsole = 10,
  55. ToggleDockedProfiler = 11
  56. }
  57. public enum TriggerBehaviours
  58. {
  59. TripleTap,
  60. TapAndHold,
  61. DoubleTap
  62. }
  63. public enum TriggerEnableModes
  64. {
  65. Enabled,
  66. MobileOnly,
  67. Off,
  68. DevelopmentBuildsOnly
  69. }
  70. public enum UIModes
  71. {
  72. NewInputSystem,
  73. LegacyInputSystem
  74. }
  75. internal const string ResourcesName = "Settings";
  76. private static Settings _instance;
  77. public static Settings Instance
  78. {
  79. get
  80. {
  81. if (_instance == null)
  82. {
  83. _instance = GetOrCreateInstance();
  84. }
  85. return _instance;
  86. }
  87. }
  88. private static KeyboardShortcut[] GetDefaultKeyboardShortcuts()
  89. {
  90. return new[]
  91. {
  92. new KeyboardShortcut
  93. {
  94. Control = true,
  95. Shift = true,
  96. Key = KeyCode.F1,
  97. Action = ShortcutActions.OpenSystemInfoTab
  98. },
  99. new KeyboardShortcut
  100. {
  101. Control = true,
  102. Shift = true,
  103. Key = KeyCode.F2,
  104. Action = ShortcutActions.OpenConsoleTab
  105. },
  106. new KeyboardShortcut
  107. {
  108. Control = true,
  109. Shift = true,
  110. Key = KeyCode.F3,
  111. Action = ShortcutActions.OpenOptionsTab
  112. },
  113. new KeyboardShortcut
  114. {
  115. Control = true,
  116. Shift = true,
  117. Key = KeyCode.F4,
  118. Action = ShortcutActions.OpenProfilerTab
  119. }
  120. };
  121. }
  122. private void UpgradeKeyboardShortcuts()
  123. {
  124. if (_keyboardShortcuts == null || _keyboardShortcuts.Length == 0)
  125. {
  126. return; // Nothing to do
  127. }
  128. Debug.Log("[SRDebugger] Upgrading Settings format");
  129. var newShortcuts = new List<KeyboardShortcut>();
  130. for (var i = 0; i < _keyboardShortcuts.Length; i++)
  131. {
  132. var s = _keyboardShortcuts[i];
  133. newShortcuts.Add(new KeyboardShortcut
  134. {
  135. Action = s.Action,
  136. Key = s.Key,
  137. Alt = _keyboardModifierAlt,
  138. Shift = _keyboardModifierShift,
  139. Control = _keyboardModifierControl
  140. });
  141. }
  142. _keyboardShortcuts = new KeyboardShortcut[0];
  143. _newKeyboardShortcuts = newShortcuts.ToArray();
  144. #if UNITY_EDITOR
  145. EditorUtility.SetDirty(this);
  146. #endif
  147. }
  148. [Serializable]
  149. public sealed class KeyboardShortcut
  150. {
  151. [SerializeField] public ShortcutActions Action;
  152. [SerializeField] public bool Alt;
  153. [SerializeField] public bool Control;
  154. [SerializeField] public KeyCode Key;
  155. [SerializeField] public bool Shift;
  156. #if ENABLE_INPUT_SYSTEM
  157. [NonSerialized] public UnityEngine.InputSystem.Key? Cached_KeyCode;
  158. #endif
  159. }
  160. public event PropertyChangedEventHandler PropertyChanged;
  161. #region Settings
  162. public bool IsEnabled
  163. {
  164. get { return _isEnabled; }
  165. #if UNITY_EDITOR
  166. set { _isEnabled = value; }
  167. #endif
  168. }
  169. public UIModes UIInputMode
  170. {
  171. get { return _uiInputMode; }
  172. #if UNITY_EDITOR
  173. set { _uiInputMode = value; }
  174. #endif
  175. }
  176. public DefaultTabs DefaultTab
  177. {
  178. get { return _defaultTab; }
  179. #if UNITY_EDITOR
  180. set { _defaultTab = value; }
  181. #endif
  182. }
  183. /// <summary>
  184. /// Enable the triple-tap button.
  185. /// </summary>
  186. public TriggerEnableModes EnableTrigger
  187. {
  188. get { return _triggerEnableMode; }
  189. #if UNITY_EDITOR
  190. set { _triggerEnableMode = value; }
  191. #endif
  192. }
  193. /// <summary>
  194. /// Enable the triple-tap button.
  195. /// </summary>
  196. public TriggerBehaviours TriggerBehaviour
  197. {
  198. get { return _triggerBehaviour; }
  199. #if UNITY_EDITOR
  200. set { _triggerBehaviour = value; }
  201. #endif
  202. }
  203. /// <summary>
  204. /// Enable a notification when a new error is logged.
  205. /// </summary>
  206. public bool ErrorNotification
  207. {
  208. get { return _errorNotification; }
  209. #if UNITY_EDITOR
  210. set { _errorNotification = value; }
  211. #endif
  212. }
  213. public bool EnableKeyboardShortcuts
  214. {
  215. get { return _enableKeyboardShortcuts; }
  216. #if UNITY_EDITOR
  217. set { _enableKeyboardShortcuts = value; }
  218. #endif
  219. }
  220. public IList<KeyboardShortcut> KeyboardShortcuts
  221. {
  222. get { return _newKeyboardShortcuts; }
  223. #if UNITY_EDITOR
  224. set { _newKeyboardShortcuts = value.ToArray(); }
  225. #endif
  226. }
  227. public bool KeyboardEscapeClose
  228. {
  229. get { return _keyboardEscapeClose; }
  230. #if UNITY_EDITOR
  231. set { _keyboardEscapeClose = value; }
  232. #endif
  233. }
  234. public bool EnableBackgroundTransparency
  235. {
  236. get { return _enableBackgroundTransparency; }
  237. #if UNITY_EDITOR
  238. set { _enableBackgroundTransparency = value; }
  239. #endif
  240. }
  241. public float BackgroundTransparency
  242. {
  243. get { return _backgroundTransparency; }
  244. #if UNITY_EDITOR
  245. set { _backgroundTransparency = value; }
  246. #endif
  247. }
  248. public bool RequireCode
  249. {
  250. get { return _requireEntryCode; }
  251. #if UNITY_EDITOR
  252. set { _requireEntryCode = value; }
  253. #endif
  254. }
  255. public bool RequireEntryCodeEveryTime
  256. {
  257. get { return _requireEntryCodeEveryTime; }
  258. #if UNITY_EDITOR
  259. set { _requireEntryCodeEveryTime = value; }
  260. #endif
  261. }
  262. public IList<int> EntryCode
  263. {
  264. get { return new ReadOnlyCollection<int>(_entryCode); }
  265. set
  266. {
  267. if (value.Count != 4)
  268. {
  269. throw new Exception("Entry code must be length 4");
  270. }
  271. if (value.Any(p => p > 9 || p < 0))
  272. {
  273. throw new Exception("All digits in entry code must be >= 0 and <= 9");
  274. }
  275. _entryCode = value.ToArray();
  276. }
  277. }
  278. public bool UseDebugCamera
  279. {
  280. get { return _useDebugCamera; }
  281. #if UNITY_EDITOR
  282. set { _useDebugCamera = value; }
  283. #endif
  284. }
  285. public int DebugLayer
  286. {
  287. get { return _debugLayer; }
  288. #if UNITY_EDITOR
  289. set { _debugLayer = value; }
  290. #endif
  291. }
  292. public float DebugCameraDepth
  293. {
  294. get { return _debugCameraDepth; }
  295. #if UNITY_EDITOR
  296. set { _debugCameraDepth = value; }
  297. #endif
  298. }
  299. public bool CollapseDuplicateLogEntries
  300. {
  301. get { return _collapseDuplicateLogEntries; }
  302. #if UNITY_EDITOR
  303. set { _collapseDuplicateLogEntries = value; }
  304. #endif
  305. }
  306. public bool RichTextInConsole
  307. {
  308. get { return _richTextInConsole; }
  309. #if UNITY_EDITOR
  310. set { _richTextInConsole = value; }
  311. #endif
  312. }
  313. public string ApiKey
  314. {
  315. get { return _apiKey; }
  316. #if UNITY_EDITOR
  317. set
  318. {
  319. _apiKey = value;
  320. EditorUtility.SetDirty(this);
  321. }
  322. #endif
  323. }
  324. public bool EnableBugReporter
  325. {
  326. get { return _enableBugReporter; }
  327. #if UNITY_EDITOR
  328. set { _enableBugReporter = value; }
  329. #endif
  330. }
  331. public bool EnableBugReportScreenshot
  332. {
  333. get { return _enableBugReportScreenshot; }
  334. #if UNITY_EDITOR
  335. set { _enableBugReportScreenshot = value; }
  336. #endif
  337. }
  338. public IList<DefaultTabs> DisabledTabs
  339. {
  340. get { return _disabledTabs; }
  341. #if UNITY_EDITOR
  342. set { _disabledTabs = value.ToArray(); }
  343. #endif
  344. }
  345. /// <summary>
  346. /// Position for the triple-tap button
  347. /// </summary>
  348. public PinAlignment TriggerPosition
  349. {
  350. get { return _triggerPosition; }
  351. #if UNITY_EDITOR
  352. set
  353. {
  354. var prevValue = _triggerPosition;
  355. _triggerPosition = value;
  356. if (OptionsAlignment == value)
  357. {
  358. OptionsAlignment = prevValue;
  359. }
  360. if (ProfilerAlignment == value)
  361. {
  362. ProfilerAlignment = prevValue;
  363. }
  364. }
  365. #endif
  366. }
  367. public PinAlignment ProfilerAlignment
  368. {
  369. get { return _profilerAlignment; }
  370. #if UNITY_EDITOR
  371. set
  372. {
  373. // Profiler only supports corners
  374. if (value == PinAlignment.CenterRight || value == PinAlignment.CenterLeft ||
  375. value == PinAlignment.TopCenter || value == PinAlignment.BottomCenter)
  376. {
  377. // Prefer bottom left
  378. if (TriggerPosition != PinAlignment.BottomLeft && OptionsAlignment != PinAlignment.BottomLeft)
  379. {
  380. _profilerAlignment = PinAlignment.BottomLeft;
  381. return;
  382. }
  383. // Find next available opening
  384. for (var i = 0; i < 4; i++)
  385. {
  386. var pin = (PinAlignment) i;
  387. if (_triggerPosition == pin || _optionsAlignment == pin)
  388. continue;
  389. _profilerAlignment = pin;
  390. return;
  391. }
  392. _profilerAlignment = PinAlignment.BottomLeft;
  393. return;
  394. }
  395. var prevValue = _profilerAlignment;
  396. _profilerAlignment = value;
  397. if (TriggerPosition == value)
  398. {
  399. TriggerPosition = prevValue;
  400. }
  401. if (OptionsAlignment == value)
  402. {
  403. OptionsAlignment = prevValue;
  404. }
  405. }
  406. #endif
  407. }
  408. public PinAlignment OptionsAlignment
  409. {
  410. get { return _optionsAlignment; }
  411. #if UNITY_EDITOR
  412. set
  413. {
  414. // Options doesn't support CenterRight and CenterLeft
  415. if (value == PinAlignment.CenterRight || value == PinAlignment.CenterLeft)
  416. {
  417. // Prefer bottom right
  418. if (TriggerPosition != PinAlignment.BottomRight && ProfilerAlignment != PinAlignment.BottomRight)
  419. {
  420. _optionsAlignment = PinAlignment.BottomRight;
  421. return;
  422. }
  423. for (var i = 0; i < 4; i++)
  424. {
  425. var pin = (PinAlignment) i;
  426. if (_triggerPosition == pin || _profilerAlignment == pin)
  427. continue;
  428. _optionsAlignment = pin;
  429. return;
  430. }
  431. _optionsAlignment = PinAlignment.BottomRight;
  432. return;
  433. }
  434. var prevValue = _optionsAlignment;
  435. _optionsAlignment = value;
  436. if (TriggerPosition == value)
  437. {
  438. TriggerPosition = prevValue;
  439. }
  440. if (ProfilerAlignment == value)
  441. {
  442. ProfilerAlignment = prevValue;
  443. }
  444. }
  445. #endif
  446. }
  447. public ConsoleAlignment ConsoleAlignment
  448. {
  449. get { return _consoleAlignment; }
  450. set { _consoleAlignment = value; }
  451. }
  452. public int MaximumConsoleEntries
  453. {
  454. get { return _maximumConsoleEntries; }
  455. set { _maximumConsoleEntries = value; }
  456. }
  457. public bool EnableEventSystemGeneration
  458. {
  459. get { return _enableEventSystemCreation; }
  460. set { _enableEventSystemCreation = value; }
  461. }
  462. public bool AutomaticallyShowCursor
  463. {
  464. get { return _automaticShowCursor; }
  465. #if UNITY_EDITOR
  466. set { _automaticShowCursor = value; }
  467. #endif
  468. }
  469. public float UIScale
  470. {
  471. get { return _uiScale; }
  472. set
  473. {
  474. if (value == _uiScale) return;
  475. _uiScale = value;
  476. OnPropertyChanged("UIScale");
  477. }
  478. }
  479. public bool UnloadOnClose
  480. {
  481. get { return _unloadOnClose; }
  482. #if UNITY_EDITOR
  483. set
  484. {
  485. _unloadOnClose = value;
  486. }
  487. #endif
  488. }
  489. #if UNITY_EDITOR
  490. public bool DisableWelcomePopup
  491. {
  492. get { return _disableWelcomePopup; }
  493. set { _disableWelcomePopup = value; }
  494. }
  495. #endif
  496. #endregion
  497. #region Serialization
  498. [SerializeField] private bool _isEnabled = false;
  499. [SerializeField] private bool _disableWelcomePopup = false;
  500. [SerializeField] private UIModes _uiInputMode = UIModes.NewInputSystem;
  501. [SerializeField] private DefaultTabs _defaultTab = DefaultTabs.SystemInformation;
  502. [SerializeField] private TriggerEnableModes _triggerEnableMode = TriggerEnableModes.Enabled;
  503. [SerializeField] private TriggerBehaviours _triggerBehaviour = TriggerBehaviours.TripleTap;
  504. [SerializeField] private bool _errorNotification = true;
  505. [SerializeField] private bool _enableKeyboardShortcuts = true;
  506. // Legacy keyboard shortcuts, should be upgraded
  507. [SerializeField] private KeyboardShortcut[] _keyboardShortcuts;
  508. // New keyboard shortcut array, containing upgraded shortcuts
  509. [SerializeField] private KeyboardShortcut[] _newKeyboardShortcuts = GetDefaultKeyboardShortcuts();
  510. [SerializeField] private bool _keyboardModifierControl = true;
  511. [SerializeField] private bool _keyboardModifierAlt = false;
  512. [SerializeField] private bool _keyboardModifierShift = true;
  513. [SerializeField] private bool _keyboardEscapeClose = true;
  514. [SerializeField] private bool _enableBackgroundTransparency = true;
  515. [SerializeField] private float _backgroundTransparency = 0.9f;
  516. [SerializeField] private bool _collapseDuplicateLogEntries = true;
  517. [SerializeField] private bool _richTextInConsole = true;
  518. [SerializeField] private bool _requireEntryCode;
  519. [SerializeField] private bool _requireEntryCodeEveryTime;
  520. [SerializeField] private int[] _entryCode = {0, 0, 0, 0};
  521. [SerializeField] private bool _useDebugCamera;
  522. [SerializeField] private int _debugLayer = 5;
  523. [SerializeField] [Range(-100, 100)] private float _debugCameraDepth = 100f;
  524. [SerializeField] private string _apiKey = "";
  525. [SerializeField] private bool _enableBugReporter;
  526. [SerializeField] private bool _enableBugReportScreenshot = true;
  527. [SerializeField] private DefaultTabs[] _disabledTabs = {};
  528. [SerializeField] private PinAlignment _profilerAlignment = PinAlignment.BottomLeft;
  529. [SerializeField] private PinAlignment _optionsAlignment = PinAlignment.BottomRight;
  530. [SerializeField] private ConsoleAlignment _consoleAlignment = ConsoleAlignment.Top;
  531. [SerializeField] private PinAlignment _triggerPosition = PinAlignment.TopLeft;
  532. [SerializeField] private int _maximumConsoleEntries = 1500;
  533. [SerializeField] private bool _enableEventSystemCreation = true;
  534. [SerializeField] private bool _automaticShowCursor = true;
  535. [SerializeField] private float _uiScale = 1;
  536. [SerializeField] private bool _unloadOnClose = false;
  537. #endregion
  538. #region Property Changed
  539. private void OnPropertyChanged(string propertyName)
  540. {
  541. if (PropertyChanged != null)
  542. {
  543. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  544. }
  545. }
  546. #endregion
  547. #region Saving/Loading
  548. internal static void ClearCache()
  549. {
  550. if (_instance != null)
  551. {
  552. Resources.UnloadAsset(_instance);
  553. }
  554. _instance = null;
  555. }
  556. internal static Settings GetInstance()
  557. {
  558. var instance = Resources.Load<Settings>("SRDebugger/" + ResourcesName);
  559. return instance;
  560. }
  561. private static Settings GetOrCreateInstance()
  562. {
  563. var instance = GetInstance();
  564. if (instance == null)
  565. {
  566. Debug.Log("[SRDebugger] No SRDebugger settings object found - using defaults. (Open SRDebugger Settings window in the Unity Editor to create settings file)");
  567. // Create instance
  568. instance = CreateInstance<Settings>();
  569. } else
  570. {
  571. instance.UpgradeKeyboardShortcuts();
  572. }
  573. return instance;
  574. }
  575. #endregion
  576. }
  577. }