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