暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DebugUI.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using System.Linq;
  3. using UnityEngine.Assertions;
  4. namespace UnityEngine.Rendering
  5. {
  6. /// <summary>
  7. /// Debug UI Class
  8. /// </summary>
  9. public partial class DebugUI
  10. {
  11. /// <summary>
  12. /// A column of checkboxes for enabling and disabling flags.
  13. /// </summary>
  14. [Flags]
  15. public enum Flags
  16. {
  17. /// <summary>
  18. /// None.
  19. /// </summary>
  20. None = 0,
  21. /// <summary>
  22. /// This widget is Editor only.
  23. /// </summary>
  24. EditorOnly = 1 << 1,
  25. /// <summary>
  26. /// This widget is Runtime only.
  27. /// </summary>
  28. RuntimeOnly = 1 << 2,
  29. /// <summary>
  30. /// This widget will force the Debug Editor Window refresh.
  31. /// </summary>
  32. EditorForceUpdate = 1 << 3,
  33. /// <summary>
  34. /// This widget will appear in the section "Frequently Used"
  35. /// </summary>
  36. FrequentlyUsed = 1 << 4
  37. }
  38. /// <summary>
  39. /// Base class for all debug UI widgets.
  40. /// </summary>
  41. public abstract class Widget
  42. {
  43. // Set to null until it's added to a panel, be careful
  44. /// <summary>
  45. /// Panels containing the widget.
  46. /// </summary>
  47. protected Panel m_Panel;
  48. /// <summary>
  49. /// Panels containing the widget.
  50. /// </summary>
  51. public virtual Panel panel
  52. {
  53. get { return m_Panel; }
  54. internal set { m_Panel = value; }
  55. }
  56. /// <summary>
  57. /// Parent container.
  58. /// </summary>
  59. protected IContainer m_Parent;
  60. /// <summary>
  61. /// Parent container.
  62. /// </summary>
  63. public virtual IContainer parent
  64. {
  65. get { return m_Parent; }
  66. internal set { m_Parent = value; }
  67. }
  68. /// <summary>
  69. /// Flags for the widget.
  70. /// </summary>
  71. public Flags flags { get; set; }
  72. /// <summary>
  73. /// Display name.
  74. /// </summary>
  75. public string displayName { get; set; }
  76. /// <summary>
  77. /// Tooltip.
  78. /// </summary>
  79. public string tooltip { get; set; }
  80. /// <summary>
  81. /// Path of the widget.
  82. /// </summary>
  83. public string queryPath { get; private set; }
  84. /// <summary>
  85. /// True if the widget is Editor only.
  86. /// </summary>
  87. public bool isEditorOnly => flags.HasFlag(Flags.EditorOnly);
  88. /// <summary>
  89. /// True if the widget is Runtime only.
  90. /// </summary>
  91. public bool isRuntimeOnly => flags.HasFlag(Flags.RuntimeOnly);
  92. /// <summary>
  93. /// True if the widget is inactive in the editor (i.e. widget is runtime only and the application is not 'Playing').
  94. /// </summary>
  95. public bool isInactiveInEditor => (isRuntimeOnly && !Application.isPlaying);
  96. /// <summary>
  97. /// Optional delegate that can be used to conditionally hide widgets at runtime (e.g. due to state of other widgets).
  98. /// </summary>
  99. public Func<bool> isHiddenCallback;
  100. /// <summary>
  101. /// If <see cref="isHiddenCallback">shouldHideDelegate</see> has been set and returns true, the widget is hidden from the UI.
  102. /// </summary>
  103. public bool isHidden => isHiddenCallback?.Invoke() ?? false;
  104. internal virtual void GenerateQueryPath()
  105. {
  106. queryPath = displayName.Trim();
  107. if (m_Parent != null)
  108. queryPath = m_Parent.queryPath + " -> " + queryPath;
  109. }
  110. /// <summary>
  111. /// Returns the hash code of the widget.
  112. /// </summary>
  113. /// <returns>The hash code of the widget.</returns>
  114. public override int GetHashCode()
  115. {
  116. return queryPath.GetHashCode() ^ isHidden.GetHashCode();
  117. }
  118. /// <summary>
  119. /// Helper struct to allow more compact initialization of widgets.
  120. /// </summary>
  121. public struct NameAndTooltip
  122. {
  123. /// <summary>
  124. /// The name
  125. /// </summary>
  126. public string name;
  127. /// <summary>
  128. /// The tooltip
  129. /// </summary>
  130. public string tooltip;
  131. }
  132. /// <summary>
  133. /// Helper setter to allow more compact initialization of widgets.
  134. /// </summary>
  135. public NameAndTooltip nameAndTooltip
  136. {
  137. set
  138. {
  139. displayName = value.name;
  140. tooltip = value.tooltip;
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Interface for widgets that can contain other widgets.
  146. /// </summary>
  147. public interface IContainer
  148. {
  149. /// <summary>
  150. /// List of children of the container.
  151. /// </summary>
  152. ObservableList<Widget> children { get; }
  153. /// <summary>
  154. /// Display name of the container.
  155. /// </summary>
  156. string displayName { get; set; }
  157. /// <summary>
  158. /// Path of the container.
  159. /// </summary>
  160. string queryPath { get; }
  161. }
  162. /// <summary>
  163. /// Any widget that implements this will be considered for serialization (only if the setter is set and thus is not read-only)
  164. /// </summary>
  165. public interface IValueField
  166. {
  167. /// <summary>
  168. /// Return the value of the field.
  169. /// </summary>
  170. /// <returns>Value of the field.</returns>
  171. object GetValue();
  172. /// <summary>
  173. /// Set the value of the field.
  174. /// </summary>
  175. /// <param name="value">Input value.</param>
  176. void SetValue(object value);
  177. /// <summary>
  178. /// Function used to validate the value when setting it.
  179. /// </summary>
  180. /// <param name="value">Input value.</param>
  181. /// <returns>Validated value.</returns>
  182. object ValidateValue(object value);
  183. }
  184. // Miscellaneous
  185. /// <summary>
  186. /// Button widget.
  187. /// </summary>
  188. public class Button : Widget
  189. {
  190. /// <summary>
  191. /// Action performed by the button.
  192. /// </summary>
  193. public Action action { get; set; }
  194. }
  195. /// <summary>
  196. /// A field that displays a read-only value.
  197. /// </summary>
  198. public class Value : Widget
  199. {
  200. /// <summary>
  201. /// Getter for the Value.
  202. /// </summary>
  203. public Func<object> getter { get; set; }
  204. /// <summary>
  205. /// Refresh rate for the read-only value (runtime only)
  206. /// </summary>
  207. public float refreshRate = 0.1f;
  208. /// <summary>
  209. /// Optional C# numeric format string, using following syntax: "{0[:numericFormatString]}"
  210. /// See https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
  211. /// and https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting
  212. /// Example: 123.45678 with formatString "{0:F2} ms" --> "123.45 ms".
  213. /// </summary>
  214. public string formatString = null;
  215. /// <summary>
  216. /// Constructor.
  217. /// </summary>
  218. public Value()
  219. {
  220. displayName = "";
  221. }
  222. /// <summary>
  223. /// Returns the value of the widget.
  224. /// </summary>
  225. /// <returns>The value of the widget.</returns>
  226. public virtual object GetValue()
  227. {
  228. Assert.IsNotNull(getter);
  229. return getter();
  230. }
  231. /// <summary>
  232. /// Returns the formatted value string for display purposes.
  233. /// </summary>
  234. /// <param name="value">Value to be formatted.</param>
  235. /// <returns>The formatted value string.</returns>
  236. public virtual string FormatString(object value)
  237. {
  238. return string.IsNullOrEmpty(formatString) ? $"{value}" : string.Format(formatString, value);
  239. }
  240. }
  241. /// <summary>
  242. /// A progress bar that displays values between 0% and 100%.
  243. /// </summary>
  244. public class ProgressBarValue : Value
  245. {
  246. /// <summary>
  247. /// Minimum value.
  248. /// </summary>
  249. public float min = 0f;
  250. /// <summary>
  251. /// Maximum value.
  252. /// </summary>
  253. public float max = 1f;
  254. /// <summary>
  255. /// Get the current progress string, remapped to [0, 1] range, representing the progress between min and max.
  256. /// </summary>
  257. /// <param name="value">Value to be formatted.</param>
  258. /// <returns>Formatted progress percentage string between 0% and 100%.</returns>
  259. public override string FormatString(object value)
  260. {
  261. static float Remap01(float v, float x0, float y0) => (v - x0) / (y0 - x0);
  262. float clamped = Mathf.Clamp((float)value, min, max);
  263. float percentage = Remap01(clamped, min, max);
  264. return $"{percentage:P1}";
  265. }
  266. }
  267. /// <summary>
  268. /// An array of read-only values that Unity displays in a horizontal row.
  269. /// </summary>
  270. public class ValueTuple : Widget
  271. {
  272. /// <summary>
  273. /// Number of elements in the tuple.
  274. /// </summary>
  275. public int numElements
  276. {
  277. get
  278. {
  279. Assert.IsTrue(values.Length > 0);
  280. return values.Length;
  281. }
  282. }
  283. /// <summary>
  284. /// Value widgets.
  285. /// </summary>
  286. public Value[] values;
  287. /// <summary>
  288. /// Refresh rate for the read-only values (runtime only)
  289. /// </summary>
  290. public float refreshRate => values.FirstOrDefault()?.refreshRate ?? 0.1f;
  291. /// <summary>
  292. /// The currently pinned element index, or -1 if none are pinned.
  293. /// </summary>
  294. public int pinnedElementIndex = -1;
  295. }
  296. }
  297. }