Açıklama Yok
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.

Dialog.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if UNITY_EDITOR
  2. using System;
  3. using UnityEditor;
  4. namespace UnityEngine.InputSystem.Editor
  5. {
  6. // Input system UI dialogs centralized as utility methods.
  7. // In the future we may introduce possibility to mock/stub these dialogs via delegates to allow
  8. // automated testing of dialog options and secure that no dialogs are shown in editor testing.
  9. internal static class Dialog
  10. {
  11. // Represents the result of the user selecting a dialog option.
  12. public enum Result
  13. {
  14. // User decided that unsaved changes should be saved to the associated asset.
  15. Save = 0,
  16. // Operation was cancelled by the user.
  17. Cancel = 1,
  18. // Unsaved changes should be discarded and NOT be saved to the associated asset.
  19. Discard = 2,
  20. // User decided to delete the associated resource.
  21. Delete = 3,
  22. }
  23. // User UI dialog windows related to InputActionAssets
  24. public static class InputActionAsset
  25. {
  26. #region Save Changes Dialog
  27. private static Func<string, Result> saveChanges = DefaultSaveChanges;
  28. internal static void SetSaveChanges(Func<string, Result> dialog)
  29. {
  30. saveChanges = dialog ?? DefaultSaveChanges;
  31. }
  32. private static Result DefaultSaveChanges(string path)
  33. {
  34. var id = EditorUtility.DisplayDialogComplex(
  35. title: "Input Action Asset has been modified",
  36. message: $"Do you want to save the changes you made in:\n{path}\n\nYour changes will be lost if you don't save them.",
  37. ok: "Save",
  38. cancel: "Cancel",
  39. alt: "Don't Save");
  40. switch (id)
  41. {
  42. case 0: return Result.Save;
  43. case 1: return Result.Cancel;
  44. case 2: return Result.Discard;
  45. default: throw new ArgumentOutOfRangeException(nameof(id));
  46. }
  47. }
  48. // Shows a dialog prompting the user to save or discard unsaved changes.
  49. // May return Result.Save, Result.Cancel or Result.Discard.
  50. public static Result ShowSaveChanges(string path)
  51. {
  52. return saveChanges(path);
  53. }
  54. #endregion
  55. #region Discard Unsaved Changes Dialog
  56. private static Func<string, Result> discardUnsavedChanges = DefaultDiscardUnsavedChanges;
  57. internal static void SetDiscardUnsavedChanges(Func<string, Result> dialog)
  58. {
  59. discardUnsavedChanges = dialog ?? DefaultDiscardUnsavedChanges;
  60. }
  61. private static Result DefaultDiscardUnsavedChanges(string path)
  62. {
  63. var pressedOkButton = EditorUtility.DisplayDialog(
  64. title: "Unsaved changes",
  65. message:
  66. $"You have unsaved changes for '{path}'. Do you want to discard the changes and delete the asset?",
  67. ok: "Yes, Delete",
  68. cancel: "No, Cancel");
  69. return pressedOkButton ? Result.Discard : Result.Cancel;
  70. }
  71. // Shows a dialog prompting the user to discard changes or cancel the operation.
  72. // May return Result.Discard or Result.Cancel.
  73. public static Result ShowDiscardUnsavedChanges(string path)
  74. {
  75. return discardUnsavedChanges(path);
  76. }
  77. #endregion
  78. #region Create and overwrite existing asset dialog
  79. private static Func<string, Result>
  80. createAndOverwriteExistingAsset = DefaultCreateAndOverwriteExistingAsset;
  81. internal static void SetCreateAndOverwriteExistingAsset(Func<string, Result> dialog)
  82. {
  83. createAndOverwriteExistingAsset = dialog ?? DefaultCreateAndOverwriteExistingAsset;
  84. }
  85. private static Result DefaultCreateAndOverwriteExistingAsset(string path)
  86. {
  87. var pressedOkButton = EditorUtility.DisplayDialog(
  88. title: "Create Input Action Asset",
  89. message: $"This will overwrite the existing asset: '{path}'. Continue and overwrite?",
  90. ok: "Ok",
  91. cancel: "Cancel");
  92. return pressedOkButton ? Result.Discard : Result.Cancel;
  93. }
  94. // Shows a dialog prompting the user whether the intention is to create an asset and overwrite the
  95. // currently existing asset. May return Result.Discard to overwrite or Result.Cancel to cancel.
  96. public static Result ShowCreateAndOverwriteExistingAsset(string path)
  97. {
  98. return createAndOverwriteExistingAsset(path);
  99. }
  100. #endregion
  101. }
  102. // User UI dialog windows related to InputControlSchemes
  103. public static class ControlScheme
  104. {
  105. private static Func<string, Result> deleteControlScheme = DefaultDeleteControlScheme;
  106. internal static void SetDeleteControlScheme(Func<string, Result> dialog)
  107. {
  108. deleteControlScheme = dialog ?? DefaultDeleteControlScheme;
  109. }
  110. private static Result DefaultDeleteControlScheme(string controlSchemeName)
  111. {
  112. // Ask for confirmation.
  113. var pressedOkButton = EditorUtility.DisplayDialog("Delete scheme?",
  114. message: $"Do you want to delete control scheme '{controlSchemeName}'?",
  115. ok: "Delete",
  116. cancel: "Cancel");
  117. return pressedOkButton ? Result.Delete : Result.Cancel;
  118. }
  119. // Shows a dialog prompting the user to delete a control scheme or cancel the operation.
  120. // May return Result.Delete or Result.Cancel.
  121. public static Result ShowDeleteControlScheme(string controlSchemeName)
  122. {
  123. return deleteControlScheme(controlSchemeName);
  124. }
  125. }
  126. }
  127. }
  128. #endif // UNITY_EDITOR