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.

InputAssetEditorUtils.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.IO;
  4. using UnityEditor;
  5. namespace UnityEngine.InputSystem.Editor
  6. {
  7. internal static class InputAssetEditorUtils
  8. {
  9. /// <summary>
  10. /// Represents a dialog result.
  11. /// </summary>
  12. internal enum DialogResult
  13. {
  14. /// <summary>
  15. /// The dialog was closed with an invalid path.
  16. /// </summary>
  17. InvalidPath,
  18. /// <summary>
  19. /// The dialog was cancelled by the user and the path is invalid.
  20. /// </summary>
  21. Cancelled,
  22. /// <summary>
  23. /// The dialog was accepted by the user and the associated path is valid.
  24. /// </summary>
  25. Valid
  26. }
  27. internal struct PromptResult
  28. {
  29. public PromptResult(DialogResult result, string path)
  30. {
  31. this.result = result;
  32. this.relativePath = path;
  33. }
  34. public readonly DialogResult result;
  35. public readonly string relativePath;
  36. }
  37. internal static string MakeProjectFileName(string projectNameSuffixNoExtension)
  38. {
  39. return PlayerSettings.productName + "." + projectNameSuffixNoExtension;
  40. }
  41. internal static PromptResult PromptUserForAsset(string friendlyName, string suggestedAssetFilePathWithoutExtension, string assetFileExtension)
  42. {
  43. // Prompt user for a file name.
  44. var fullAssetFileExtension = "." + assetFileExtension;
  45. var path = EditorUtility.SaveFilePanel(
  46. title: $"Create {friendlyName} File",
  47. directory: "Assets",
  48. defaultName: suggestedAssetFilePathWithoutExtension + "." + assetFileExtension,
  49. extension: assetFileExtension);
  50. if (string.IsNullOrEmpty(path))
  51. return new PromptResult(DialogResult.Cancelled, null);
  52. // Make sure the path is in the Assets/ folder.
  53. path = path.Replace("\\", "/"); // Make sure we only get '/' separators.
  54. var dataPath = Application.dataPath + "/";
  55. if (!path.StartsWith(dataPath, StringComparison.CurrentCultureIgnoreCase))
  56. {
  57. Debug.LogError($"{friendlyName} must be stored in Assets folder of the project (got: '{path}')");
  58. return new PromptResult(DialogResult.InvalidPath, null);
  59. }
  60. // Make sure path ends with expected extension
  61. var extension = Path.GetExtension(path);
  62. if (string.Compare(extension, fullAssetFileExtension, StringComparison.InvariantCultureIgnoreCase) != 0)
  63. path += fullAssetFileExtension;
  64. return new PromptResult(DialogResult.Valid, "Assets/" + path.Substring(dataPath.Length));
  65. }
  66. internal static T CreateAsset<T>(T asset, string relativePath) where T : ScriptableObject
  67. {
  68. AssetDatabase.CreateAsset(asset, relativePath);
  69. EditorGUIUtility.PingObject(asset);
  70. return asset;
  71. }
  72. public static void DrawMakeActiveGui<T>(T current, T target, string targetName, string entity, Action<T> apply, bool allowAssignActive = true)
  73. where T : ScriptableObject
  74. {
  75. if (current == target)
  76. {
  77. EditorGUILayout.HelpBox($"These actions are assigned as the {entity}.", MessageType.Info);
  78. return;
  79. }
  80. string currentlyActiveAssetsPath = null;
  81. if (current != null)
  82. currentlyActiveAssetsPath = AssetDatabase.GetAssetPath(current);
  83. if (!string.IsNullOrEmpty(currentlyActiveAssetsPath))
  84. currentlyActiveAssetsPath = $" The actions currently assigned as the {entity} are: {currentlyActiveAssetsPath}. ";
  85. EditorGUILayout.HelpBox($"These actions are not assigned as the {entity} for the Input System. {currentlyActiveAssetsPath??""}", MessageType.Warning);
  86. GUI.enabled = allowAssignActive;
  87. if (GUILayout.Button($"Assign as the {entity}", EditorStyles.miniButton))
  88. apply(target);
  89. GUI.enabled = true;
  90. }
  91. public static bool IsValidFileExtension(string path)
  92. {
  93. return path != null && path.EndsWith("." + InputActionAsset.Extension, StringComparison.InvariantCultureIgnoreCase);
  94. }
  95. }
  96. }
  97. #endif // UNITY_EDITOR