Settings.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 bool RequireCode
  242. {
  243. get { return _requireEntryCode; }
  244. #if UNITY_EDITOR
  245. set { _requireEntryCode = value; }
  246. #endif
  247. }
  248. public bool RequireEntryCodeEveryTime
  249. {
  250. get { return _requireEntryCodeEveryTime; }
  251. #if UNITY_EDITOR
  252. set { _requireEntryCodeEveryTime = value; }
  253. #endif
  254. }
  255. public IList<int> EntryCode
  256. {
  257. get { return new ReadOnlyCollection<int>(_entryCode); }
  258. set
  259. {
  260. if (value.Count != 4)
  261. {
  262. throw new Exception("Entry code must be length 4");
  263. }
  264. if (value.Any(p => p > 9 || p < 0))
  265. {
  266. throw new Exception("All digits in entry code must be >= 0 and <= 9");
  267. }
  268. _entryCode = value.ToArray();
  269. }
  270. }
  271. public bool UseDebugCamera
  272. {
  273. get { return _useDebugCamera; }
  274. #if UNITY_EDITOR
  275. set { _useDebugCamera = value; }
  276. #endif
  277. }
  278. public int DebugLayer
  279. {
  280. get { return _debugLayer; }
  281. #if UNITY_EDITOR
  282. set { _debugLayer = value; }
  283. #endif
  284. }
  285. public float DebugCameraDepth
  286. {
  287. get { return _debugCameraDepth; }
  288. #if UNITY_EDITOR
  289. set { _debugCameraDepth = value; }
  290. #endif
  291. }
  292. public bool CollapseDuplicateLogEntries
  293. {
  294. get { return _collapseDuplicateLogEntries; }
  295. #if UNITY_EDITOR
  296. set { _collapseDuplicateLogEntries = value; }
  297. #endif
  298. }
  299. public bool RichTextInConsole
  300. {
  301. get { return _richTextInConsole; }
  302. #if UNITY_EDITOR
  303. set { _richTextInConsole = value; }
  304. #endif
  305. }
  306. public string ApiKey
  307. {
  308. get { return _apiKey; }
  309. #if UNITY_EDITOR
  310. set
  311. {
  312. _apiKey = value;
  313. EditorUtility.SetDirty(this);
  314. }
  315. #endif
  316. }
  317. public bool EnableBugReporter
  318. {
  319. get { return _enableBugReporter; }
  320. #if UNITY_EDITOR
  321. set { _enableBugReporter = value; }
  322. #endif
  323. }
  324. public bool EnableBugReportScreenshot
  325. {
  326. get { return _enableBugReportScreenshot; }
  327. #if UNITY_EDITOR
  328. set { _enableBugReportScreenshot = value; }
  329. #endif
  330. }
  331. public IList<DefaultTabs> DisabledTabs
  332. {
  333. get { return _disabledTabs; }
  334. #if UNITY_EDITOR
  335. set { _disabledTabs = value.ToArray(); }
  336. #endif
  337. }
  338. /// <summary>
  339. /// Position for the triple-tap button
  340. /// </summary>
  341. public PinAlignment TriggerPosition
  342. {
  343. get { return _triggerPosition; }
  344. #if UNITY_EDITOR
  345. set
  346. {
  347. var prevValue = _triggerPosition;
  348. _triggerPosition = value;
  349. if (OptionsAlignment == value)
  350. {
  351. OptionsAlignment = prevValue;
  352. }
  353. if (ProfilerAlignment == value)
  354. {
  355. ProfilerAlignment = prevValue;
  356. }
  357. }
  358. #endif
  359. }
  360. public PinAlignment ProfilerAlignment
  361. {
  362. get { return _profilerAlignment; }
  363. #if UNITY_EDITOR
  364. set
  365. {
  366. // Profiler only supports corners
  367. if (value == PinAlignment.CenterRight || value == PinAlignment.CenterLeft ||
  368. value == PinAlignment.TopCenter || value == PinAlignment.BottomCenter)
  369. {
  370. // Prefer bottom left
  371. if (TriggerPosition != PinAlignment.BottomLeft && OptionsAlignment != PinAlignment.BottomLeft)
  372. {
  373. _profilerAlignment = PinAlignment.BottomLeft;
  374. return;
  375. }
  376. // Find next available opening
  377. for (var i = 0; i < 4; i++)
  378. {
  379. var pin = (PinAlignment) i;
  380. if (_triggerPosition == pin || _optionsAlignment == pin)
  381. continue;
  382. _profilerAlignment = pin;
  383. return;
  384. }
  385. _profilerAlignment = PinAlignment.BottomLeft;
  386. return;
  387. }
  388. var prevValue = _profilerAlignment;
  389. _profilerAlignment = value;
  390. if (TriggerPosition == value)
  391. {
  392. TriggerPosition = prevValue;
  393. }
  394. if (OptionsAlignment == value)
  395. {
  396. OptionsAlignment = prevValue;
  397. }
  398. }
  399. #endif
  400. }
  401. public PinAlignment OptionsAlignment
  402. {
  403. get { return _optionsAlignment; }
  404. #if UNITY_EDITOR
  405. set
  406. {
  407. // Options doesn't support CenterRight and CenterLeft
  408. if (value == PinAlignment.CenterRight || value == PinAlignment.CenterLeft)
  409. {
  410. // Prefer bottom right
  411. if (TriggerPosition != PinAlignment.BottomRight && ProfilerAlignment != PinAlignment.BottomRight)
  412. {
  413. _optionsAlignment = PinAlignment.BottomRight;
  414. return;
  415. }
  416. for (var i = 0; i < 4; i++)
  417. {
  418. var pin = (PinAlignment) i;
  419. if (_triggerPosition == pin || _profilerAlignment == pin)
  420. continue;
  421. _optionsAlignment = pin;
  422. return;
  423. }
  424. _optionsAlignment = PinAlignment.BottomRight;
  425. return;
  426. }
  427. var prevValue = _optionsAlignment;
  428. _optionsAlignment = value;
  429. if (TriggerPosition == value)
  430. {
  431. TriggerPosition = prevValue;
  432. }
  433. if (ProfilerAlignment == value)
  434. {
  435. ProfilerAlignment = prevValue;
  436. }
  437. }
  438. #endif
  439. }
  440. public ConsoleAlignment ConsoleAlignment
  441. {
  442. get { return _consoleAlignment; }
  443. set { _consoleAlignment = value; }
  444. }
  445. public int MaximumConsoleEntries
  446. {
  447. get { return _maximumConsoleEntries; }
  448. set { _maximumConsoleEntries = value; }
  449. }
  450. public bool EnableEventSystemGeneration
  451. {
  452. get { return _enableEventSystemCreation; }
  453. set { _enableEventSystemCreation = value; }
  454. }
  455. public bool AutomaticallyShowCursor
  456. {
  457. get { return _automaticShowCursor; }
  458. #if UNITY_EDITOR
  459. set { _automaticShowCursor = value; }
  460. #endif
  461. }
  462. public float UIScale
  463. {
  464. get { return _uiScale; }
  465. set
  466. {
  467. if (value == _uiScale) return;
  468. _uiScale = value;
  469. OnPropertyChanged("UIScale");
  470. }
  471. }
  472. public bool UnloadOnClose
  473. {
  474. get { return _unloadOnClose; }
  475. #if UNITY_EDITOR
  476. set
  477. {
  478. _unloadOnClose = value;
  479. }
  480. #endif
  481. }
  482. #endregion
  483. #region Serialization
  484. [SerializeField] private bool _isEnabled = true;
  485. [SerializeField] private UIModes _uiInputMode = UIModes.NewInputSystem;
  486. [SerializeField] private DefaultTabs _defaultTab = DefaultTabs.SystemInformation;
  487. [SerializeField] private TriggerEnableModes _triggerEnableMode = TriggerEnableModes.Enabled;
  488. [SerializeField] private TriggerBehaviours _triggerBehaviour = TriggerBehaviours.TripleTap;
  489. [SerializeField] private bool _errorNotification = true;
  490. [SerializeField] private bool _enableKeyboardShortcuts = true;
  491. // Legacy keyboard shortcuts, should be upgraded
  492. [SerializeField] private KeyboardShortcut[] _keyboardShortcuts;
  493. // New keyboard shortcut array, containing upgraded shortcuts
  494. [SerializeField] private KeyboardShortcut[] _newKeyboardShortcuts = GetDefaultKeyboardShortcuts();
  495. [SerializeField] private bool _keyboardModifierControl = true;
  496. [SerializeField] private bool _keyboardModifierAlt = false;
  497. [SerializeField] private bool _keyboardModifierShift = true;
  498. [SerializeField] private bool _keyboardEscapeClose = true;
  499. [SerializeField] private bool _enableBackgroundTransparency = true;
  500. [SerializeField] private bool _collapseDuplicateLogEntries = true;
  501. [SerializeField] private bool _richTextInConsole = true;
  502. [SerializeField] private bool _requireEntryCode;
  503. [SerializeField] private bool _requireEntryCodeEveryTime;
  504. [SerializeField] private int[] _entryCode = {0, 0, 0, 0};
  505. [SerializeField] private bool _useDebugCamera;
  506. [SerializeField] private int _debugLayer = 5;
  507. [SerializeField] [Range(-100, 100)] private float _debugCameraDepth = 100f;
  508. [SerializeField] private string _apiKey = "";
  509. [SerializeField] private bool _enableBugReporter;
  510. [SerializeField] private bool _enableBugReportScreenshot = true;
  511. [SerializeField] private DefaultTabs[] _disabledTabs = {};
  512. [SerializeField] private PinAlignment _profilerAlignment = PinAlignment.BottomLeft;
  513. [SerializeField] private PinAlignment _optionsAlignment = PinAlignment.BottomRight;
  514. [SerializeField] private ConsoleAlignment _consoleAlignment = ConsoleAlignment.Top;
  515. [SerializeField] private PinAlignment _triggerPosition = PinAlignment.TopLeft;
  516. [SerializeField] private int _maximumConsoleEntries = 1500;
  517. [SerializeField] private bool _enableEventSystemCreation = true;
  518. [SerializeField] private bool _automaticShowCursor = true;
  519. [SerializeField] private float _uiScale = 1;
  520. [SerializeField] private bool _unloadOnClose = false;
  521. #endregion
  522. #region Property Changed
  523. private void OnPropertyChanged(string propertyName)
  524. {
  525. if (PropertyChanged != null)
  526. {
  527. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  528. }
  529. }
  530. #endregion
  531. #region Saving/Loading
  532. internal static void ClearCache()
  533. {
  534. if (_instance != null)
  535. {
  536. Resources.UnloadAsset(_instance);
  537. }
  538. _instance = null;
  539. }
  540. internal static Settings GetInstance()
  541. {
  542. var instance = Resources.Load<Settings>("SRDebugger/" + ResourcesName);
  543. return instance;
  544. }
  545. private static Settings GetOrCreateInstance()
  546. {
  547. var instance = GetInstance();
  548. if (instance == null)
  549. {
  550. Debug.Log("[SRDebugger] No SRDebugger settings object found - using defaults. (Open SRDebugger Settings window in the Unity Editor to create settings file)");
  551. // Create instance
  552. instance = CreateInstance<Settings>();
  553. } else
  554. {
  555. instance.UpgradeKeyboardShortcuts();
  556. }
  557. return instance;
  558. }
  559. #endregion
  560. }
  561. }