Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using UnityEditor;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Jobs.LowLevel.Unsafe;
  6. using UnityEngine;
  7. // This menu is available from the editor directly on later versions
  8. #if !UNITY_2022_2_14F1_OR_NEWER
  9. class JobsMenu
  10. {
  11. private static int savedJobWorkerCount = JobsUtility.JobWorkerCount;
  12. [SettingsProvider]
  13. private static SettingsProvider JobsPreferencesItem()
  14. {
  15. var provider = new SettingsProvider("Preferences/Jobs", SettingsScope.User)
  16. {
  17. label = "Jobs",
  18. keywords = new[]{"Jobs"},
  19. guiHandler = (searchContext) =>
  20. {
  21. var originalWidth = EditorGUIUtility.labelWidth;
  22. EditorGUIUtility.labelWidth = 200f;
  23. EditorGUILayout.BeginVertical();
  24. GUILayout.BeginVertical();
  25. EditorGUILayout.LabelField("For safety, these values are reset on editor restart.");
  26. bool madeChange = false;
  27. bool oldWorkerCount = (JobsUtility.JobWorkerCount > 0);
  28. bool newWorkerCount = EditorGUILayout.Toggle(new GUIContent("Use Job Threads:"), oldWorkerCount);
  29. if (newWorkerCount != oldWorkerCount)
  30. {
  31. madeChange = true;
  32. SwitchUseJobThreads();
  33. }
  34. bool oldUseJobsDebugger = JobsUtility.JobDebuggerEnabled;
  35. var newUseJobsDebugger = EditorGUILayout.Toggle(new GUIContent("Enable Jobs Debugger"), JobsUtility.JobDebuggerEnabled);
  36. if (newUseJobsDebugger != oldUseJobsDebugger)
  37. {
  38. madeChange = true;
  39. SetUseJobsDebugger(newUseJobsDebugger);
  40. }
  41. var oldLeakDetectionMode = NativeLeakDetection.Mode;
  42. var newLeakDetectionMode = (NativeLeakDetectionMode) EditorGUILayout.EnumPopup(new GUIContent("Leak Detection Level"), oldLeakDetectionMode);
  43. if (newLeakDetectionMode != oldLeakDetectionMode)
  44. {
  45. madeChange = true;
  46. SetLeakDetection(newLeakDetectionMode);
  47. }
  48. if (madeChange)
  49. Telemetry.LogMenuPreferences(new Telemetry.MenuPreferencesEvent { useJobsThreads = newUseJobsDebugger, enableJobsDebugger = newUseJobsDebugger, nativeLeakDetectionMode = newLeakDetectionMode });
  50. GUILayout.EndVertical();
  51. EditorGUILayout.EndVertical();
  52. EditorGUIUtility.labelWidth = originalWidth;
  53. }
  54. };
  55. return provider;
  56. }
  57. static void SwitchUseJobThreads()
  58. {
  59. if (JobsUtility.JobWorkerCount > 0)
  60. {
  61. savedJobWorkerCount = JobsUtility.JobWorkerCount;
  62. JobsUtility.JobWorkerCount = 0;
  63. }
  64. else
  65. {
  66. JobsUtility.JobWorkerCount = savedJobWorkerCount;
  67. if (savedJobWorkerCount == 0)
  68. {
  69. JobsUtility.ResetJobWorkerCount();
  70. }
  71. }
  72. }
  73. static void SetUseJobsDebugger(bool value)
  74. {
  75. JobsUtility.JobDebuggerEnabled = value;
  76. }
  77. static void SetLeakDetection(NativeLeakDetectionMode value)
  78. {
  79. switch(value)
  80. {
  81. case NativeLeakDetectionMode.Disabled:
  82. {
  83. // In the case where someone enables, disables, then re-enables leak checking, we might miss some frees
  84. // while disabled. So to avoid spurious leak warnings, just forgive the leaks every time someone disables
  85. // leak checking through the menu.
  86. UnsafeUtility.ForgiveLeaks();
  87. Debug.LogWarning("Leak detection has been disabled. Leak warnings will not be generated, and all leaks up to now are forgotten.");
  88. break;
  89. }
  90. case NativeLeakDetectionMode.Enabled:
  91. {
  92. Debug.Log("Leak detection has been enabled. Leak warnings will be generated upon exiting play mode.");
  93. break;
  94. }
  95. case NativeLeakDetectionMode.EnabledWithStackTrace:
  96. {
  97. Debug.Log("Leak detection with stack traces has been enabled. Leak warnings will be generated upon exiting play mode.");
  98. break;
  99. }
  100. default:
  101. {
  102. throw new Exception($"Unhandled {nameof(NativeLeakDetectionMode)}");
  103. }
  104. }
  105. NativeLeakDetection.Mode = value;
  106. }
  107. }
  108. #endif