Нема описа
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.

Documentation.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Diagnostics.CodeAnalysis;
  7. #if UNITY_EDITOR
  8. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  9. #endif
  10. [assembly: InternalsVisibleTo("Unity.RenderPipelines.Core.Editor.Tests")]
  11. namespace UnityEngine.Rendering
  12. {
  13. /// <summary>
  14. /// Attribute to define the help url
  15. /// </summary>
  16. /// <example>
  17. /// [CoreRPHelpURLAttribute("Volume")]
  18. /// public class Volume : MonoBehaviour
  19. /// </example>
  20. [Conditional("UNITY_EDITOR")]
  21. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum, AllowMultiple = false)]
  22. public class CoreRPHelpURLAttribute : HelpURLAttribute
  23. {
  24. /// <summary>
  25. /// The constructor of the attribute
  26. /// </summary>
  27. /// <param name="pageName">The name of the documentation page.</param>
  28. /// <param name="packageName">The package name, defaulting to "com.unity.render-pipelines.core".</param>
  29. public CoreRPHelpURLAttribute(string pageName, string packageName = "com.unity.render-pipelines.core")
  30. : base(DocumentationInfo.GetPageLink(packageName, pageName, ""))
  31. {
  32. }
  33. /// <summary>
  34. /// The constructor of the attribute
  35. /// </summary>
  36. /// <param name="pageName">The name of the documentation page.</param>
  37. /// <param name="pageHash">The hash specifying a section within the page.</param>
  38. /// <param name="packageName">The package name, defaulting to "com.unity.render-pipelines.core".</param>
  39. public CoreRPHelpURLAttribute(string pageName, string pageHash, string packageName = "com.unity.render-pipelines.core")
  40. : base(DocumentationInfo.GetPageLink(packageName, pageName, pageHash))
  41. {
  42. }
  43. }
  44. /// <summary>
  45. /// Use this attribute to define the help URP.
  46. /// </summary>
  47. /// <example>
  48. /// [CoreRPHelpURLAttribute("Volume")]
  49. /// public class Volume : MonoBehaviour
  50. /// </example>
  51. [Conditional("UNITY_EDITOR")]
  52. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum, AllowMultiple = false)]
  53. public class CurrentPipelineHelpURLAttribute : HelpURLAttribute
  54. {
  55. private string pageName { get; }
  56. /// <summary>
  57. /// The constructor of the attribute
  58. /// </summary>
  59. /// <param name="pageName">The name of the documentation page.</param>
  60. public CurrentPipelineHelpURLAttribute(string pageName)
  61. : base(null)
  62. {
  63. this.pageName = pageName;
  64. }
  65. /// <summary>
  66. /// Returns the URL to the given page in the current Render Pipeline package documentation site.
  67. /// </summary>
  68. public override string URL
  69. {
  70. get
  71. {
  72. #if UNITY_EDITOR
  73. if (DocumentationUtils.TryGetPackageInfoForType(GraphicsSettings.currentRenderPipeline?.GetType() ?? typeof(DocumentationInfo), out var package, out var version))
  74. {
  75. return DocumentationInfo.GetPackageLink(package, version, this.pageName);
  76. }
  77. #endif
  78. return string.Empty;
  79. }
  80. }
  81. }
  82. //We need to have only one version number amongst packages (so public)
  83. /// <summary>
  84. /// Documentation Info class.
  85. /// </summary>
  86. public class DocumentationInfo
  87. {
  88. const string fallbackVersion = "13.1";
  89. const string url = "https://docs.unity3d.com/Packages/{0}@{1}/manual/{2}.html{3}";
  90. /// <summary>
  91. /// Current version of the documentation.
  92. /// </summary>
  93. public static string version
  94. {
  95. get
  96. {
  97. #if UNITY_EDITOR
  98. if (DocumentationUtils.TryGetPackageInfoForType(typeof(DocumentationInfo), out _, out var version))
  99. return version;
  100. #endif
  101. return fallbackVersion;
  102. }
  103. }
  104. /// <summary>
  105. /// Generates a help URL for the given package and page name.
  106. /// </summary>
  107. /// <param name="packageName">The package name.</param>
  108. /// <param name="packageVersion">The package version.</param>
  109. /// <param name="pageName">The page name without the extension.</param>
  110. /// <returns>The full URL of the page.</returns>
  111. public static string GetPackageLink(string packageName, string packageVersion, string pageName) => string.Format(url, packageName, packageVersion, pageName, "");
  112. /// <summary>
  113. /// Generates a help url for the given package and page name
  114. /// </summary>
  115. /// <param name="packageName">The package name</param>
  116. /// <param name="pageName">The page name without the extension.</param>
  117. /// <returns>The full URL of the page.</returns>
  118. public static string GetPageLink(string packageName, string pageName) => string.Format(url, packageName, version, pageName, "");
  119. /// <summary>
  120. /// Generates a help url for the given package and page name
  121. /// </summary>
  122. /// <param name="packageName">The package name</param>
  123. /// <param name="pageName">The page name without the extension.</param>
  124. /// <param name="pageHash">The page hash</param>
  125. /// <returns>The full URL of the page.</returns>
  126. public static string GetPageLink(string packageName, string pageName, string pageHash) => string.Format(url, packageName, version, pageName, pageHash);
  127. }
  128. /// <summary>
  129. /// Set of utils for documentation
  130. /// </summary>
  131. public static class DocumentationUtils
  132. {
  133. /// <summary>
  134. /// Obtains the help url from an enum
  135. /// </summary>
  136. /// <typeparam name="TEnum">The enum with a <see cref="HelpURLAttribute"/></typeparam>
  137. /// <param name="mask">[Optional] The current value of the enum</param>
  138. /// <returns>The full url</returns>
  139. public static string GetHelpURL<TEnum>(TEnum mask = default)
  140. where TEnum : struct, IConvertible
  141. {
  142. var helpURLAttribute = (HelpURLAttribute)mask
  143. .GetType()
  144. .GetCustomAttributes(typeof(HelpURLAttribute), false)
  145. .FirstOrDefault();
  146. return helpURLAttribute == null ? string.Empty : $"{helpURLAttribute.URL}#{mask}";
  147. }
  148. /// <summary>
  149. /// Obtains the help URL from a type.
  150. /// </summary>
  151. /// <param name="type">The type decorated with the HelpURL attribute.</param>
  152. /// <param name="url">The full URL from the HelpURL attribute. If the attribute is not present, this value is null.</param>
  153. /// <returns>Returns true if the attribute is present, and false otherwise.</returns>
  154. public static bool TryGetHelpURL(Type type, out string url)
  155. {
  156. var attribute = type.GetCustomAttribute<HelpURLAttribute>(false);
  157. url = attribute?.URL;
  158. return attribute != null;
  159. }
  160. #if UNITY_EDITOR
  161. /// <summary>
  162. /// Obtain package informations from a specific type
  163. /// </summary>
  164. /// <param name="type">The type used to retrieve package information</param>
  165. /// <param name="packageName">The name of the package containing the given type</param>
  166. /// <param name="version">The version number of the package containing the given type. Only Major.Minor will be returned as fix is not used for documentation</param>
  167. /// <returns></returns>
  168. public static bool TryGetPackageInfoForType([DisallowNull] Type type, [NotNullWhen(true)] out string packageName, [NotNullWhen(true)] out string version)
  169. {
  170. var packageInfo = PackageInfo.FindForAssembly(type.Assembly);
  171. if (packageInfo == null)
  172. {
  173. packageName = null;
  174. version = null;
  175. return false;
  176. }
  177. packageName = packageInfo.name;
  178. version = packageInfo.version.Substring(0, packageInfo.version.LastIndexOf('.'));
  179. return true;
  180. }
  181. #endif
  182. }
  183. }