Ei kuvausta
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.

EditorHelpers.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Reflection;
  6. using UnityEditor;
  7. namespace UnityEngine.InputSystem.Editor
  8. {
  9. internal static class EditorHelpers
  10. {
  11. // Provides an abstraction layer on top of EditorGUIUtility to allow replacing the underlying buffer.
  12. public static Action<string> SetSystemCopyBufferContents = s => EditorGUIUtility.systemCopyBuffer = s;
  13. // Provides an abstraction layer on top of EditorGUIUtility to allow replacing the underlying buffer.
  14. public static Func<string> GetSystemCopyBufferContents = () => EditorGUIUtility.systemCopyBuffer;
  15. // Attempts to retrieve the asset GUID associated with the given asset. If asset is null or the asset
  16. // is not associated with a GUID or the operation fails for any other reason the return value will be null.
  17. public static string GetAssetGUID(Object asset)
  18. {
  19. return !AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var assetGuid, out long _)
  20. ? null : assetGuid;
  21. }
  22. // SerializedProperty.tooltip *should* give us the tooltip as per [Tooltip] attribute. Alas, for some
  23. // reason, it's not happening.
  24. public static string GetTooltip(this SerializedProperty property)
  25. {
  26. if (!string.IsNullOrEmpty(property.tooltip))
  27. return property.tooltip;
  28. var field = property.GetField();
  29. if (field != null)
  30. {
  31. var tooltipAttribute = field.GetCustomAttribute<TooltipAttribute>();
  32. if (tooltipAttribute != null)
  33. return tooltipAttribute.tooltip;
  34. }
  35. return string.Empty;
  36. }
  37. public static string GetHyperlink(string text, string path)
  38. {
  39. return "<a href=\"" + path + $"\">{text}</a>";
  40. }
  41. public static string GetHyperlink(string path)
  42. {
  43. return GetHyperlink(path, path);
  44. }
  45. public static void RestartEditorAndRecompileScripts(bool dryRun = false)
  46. {
  47. // The API here are not public. Use reflection to get to them.
  48. var editorApplicationType = typeof(EditorApplication);
  49. var restartEditorAndRecompileScripts =
  50. editorApplicationType.GetMethod("RestartEditorAndRecompileScripts",
  51. BindingFlags.NonPublic | BindingFlags.Static);
  52. if (!dryRun)
  53. restartEditorAndRecompileScripts.Invoke(null, null);
  54. else if (restartEditorAndRecompileScripts == null)
  55. throw new MissingMethodException(editorApplicationType.FullName, "RestartEditorAndRecompileScripts");
  56. }
  57. // Attempts to make an asset editable in the underlying version control system and returns true if successful.
  58. public static bool CheckOut(string path)
  59. {
  60. if (string.IsNullOrEmpty(path))
  61. throw new ArgumentNullException(nameof(path));
  62. // Make path relative to project folder.
  63. var projectPath = Application.dataPath;
  64. if (path.StartsWith(projectPath) && path.Length > projectPath.Length &&
  65. (path[projectPath.Length] == '/' || path[projectPath.Length] == '\\'))
  66. path = path.Substring(0, projectPath.Length + 1);
  67. return AssetDatabase.MakeEditable(path);
  68. }
  69. /// <summary>
  70. /// Attempts to checkout an asset for editing at the given path and overwrite its file content with
  71. /// the given asset text content.
  72. /// </summary>
  73. /// <param name="assetPath">Path to asset to be checkout out and overwritten.</param>
  74. /// <param name="text">The new file content.</param>
  75. /// <returns>true if the file was successfully checkout for editing and the file was written.
  76. /// This function may return false if unable to checkout the file for editing in the underlying
  77. /// version control system.</returns>
  78. internal static bool WriteAsset(string assetPath, string text)
  79. {
  80. // Attempt to checkout the file path for editing and inform the user if this fails.
  81. if (!CheckOut(assetPath))
  82. return false;
  83. // (Over)write file text content.
  84. File.WriteAllText(GetPhysicalPath(assetPath), text);
  85. // Reimport the asset (indirectly triggers ADB notification callbacks)
  86. AssetDatabase.ImportAsset(assetPath);
  87. return true;
  88. }
  89. /// <summary>
  90. /// Saves an asset to the given <c>assetPath</c> with file content corresponding to <c>text</c>
  91. /// if the current content of the asset given by <c>assetPath</c> is different or the asset do not exist.
  92. /// </summary>
  93. /// <param name="assetPath">Destination asset path.</param>
  94. /// <param name="text">The new desired text content to be written to the asset.</param>
  95. /// <returns><c>true</c> if the asset was successfully modified or created, else <c>false</c>.</returns>
  96. internal static bool SaveAsset(string assetPath, string text)
  97. {
  98. var existingJson = File.Exists(assetPath) ? File.ReadAllText(assetPath) : string.Empty;
  99. // Return immediately if file content has not changed, i.e. touching the file would not yield a difference.
  100. if (text == existingJson)
  101. return false;
  102. // Attempt to write asset to disc (including checkout the file) and inform the user if this fails.
  103. if (WriteAsset(assetPath, text))
  104. return true;
  105. Debug.LogError($"Unable save asset to \"{assetPath}\" since the asset-path could not be checked-out as editable in the underlying version-control system.");
  106. return false;
  107. }
  108. // Maps path into a physical path.
  109. public static string GetPhysicalPath(string path)
  110. {
  111. // Note that we can only get physical path for 2021.2 or newer
  112. #if UNITY_2021_2_OR_NEWER
  113. return FileUtil.GetPhysicalPath(path);
  114. #else
  115. return path;
  116. #endif
  117. }
  118. }
  119. }
  120. #endif // UNITY_EDITOR