using UnityEditor; using UnityEngine; using System.IO; namespace Editor { public static class PrefabEditorUtility { public static string SanitizeFileName(string fileName) { foreach (char c in Path.GetInvalidFileNameChars()) fileName = fileName.Replace(c, '_'); return fileName; } public static string SelectFolder(string currentPath, string defaultSubfolder) { string selected = EditorUtility.OpenFolderPanel("Select Folder", Path.Combine(Application.dataPath, defaultSubfolder), ""); if (!string.IsNullOrEmpty(selected)) { if (selected.Replace("\\", "/").Contains(Application.dataPath.Replace("\\", "/"))) { return "Assets" + selected.Replace("\\", "/").Substring(Application.dataPath.Replace("\\", "/").Length); } else { EditorUtility.DisplayDialog("Invalid Folder", "Please select a folder inside the Assets directory.", "OK"); } } return currentPath; } public static T CreateScriptableAsset(string assetName, string folderPath) where T : ScriptableObject { if (!AssetDatabase.IsValidFolder(folderPath)) { Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), folderPath)); AssetDatabase.Refresh(); } string folder = !string.IsNullOrEmpty(folderPath) ? folderPath : "Assets"; string path = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(folder, assetName + ".asset")); var asset = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(asset, path); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); Selection.activeObject = asset; return asset; } public static void DrawScriptableObjectEditor(ref UnityEditor.Editor soEditor, ScriptableObject so) { if (so == null) return; if (soEditor == null || soEditor.target != so) soEditor = UnityEditor.Editor.CreateEditor(so); if (soEditor != null) soEditor.OnInspectorGUI(); } public static void AddOrGetComponent(GameObject go) where T : Component { if (go.GetComponent() == null) go.AddComponent(); } public static void RemoveComponent(GameObject go) where T : Component { var comp = go.GetComponent(); if (comp != null) Object.DestroyImmediate(comp, true); } } }