using UnityEditor; using UnityEngine; namespace SubAssetsToolbox.Editor { [InitializeOnLoad] public static class ContextualActions { static ContextualActions() { EditorApplication.projectWindowItemOnGUI += OnProjectWindowItemGUI; } private static void OnProjectWindowItemGUI(string guid, Rect selectionRect) { Event current = Event.current; Object obj; switch (current.type) { case EventType.MouseDown: ClearLastSelection(); break; case EventType.KeyUp when current.keyCode == KeyCode.Delete || current.keyCode == KeyCode.Backspace && current.command: obj = Selection.activeObject; if (obj == null) return; if(AssetDatabase.IsSubAsset(obj)) { // It's a sub-asset bool decision = EditorUtility.DisplayDialog("Delete Sub-Asset", string.Format(Constants.ConfirmDeleteSubAsset, obj.name), "Delete", "Cancel"); if (decision) { string parentObjectPath = AssetDatabase.GetAssetPath(obj); SubAssetsToolbox.RemoveSubAsset(parentObjectPath, obj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } ClearLastSelection(); current.Use(); break; case EventType.KeyUp when #if UNITY_EDITOR_WIN current.keyCode == KeyCode.F2: #else current.keyCode == KeyCode.Return: #endif obj = Selection.activeObject; if (obj == null || Selection.objects.Length > 1) return; if (AssetDatabase.IsSubAsset(obj)) { RenameSubAssetWindow.Open(obj); current.Use(); } break; case EventType.ContextClick when selectionRect.Contains(current.mousePosition): { string path = AssetDatabase.GUIDToAssetPath(guid); if (AssetDatabase.IsValidFolder(path)) { ClearLastSelection(); return; } obj = AssetDatabase.LoadAssetAtPath(path); if (obj != null) { AssetRelocator.LastSelectionOnRightClick = obj; } break; } } } private static void ClearLastSelection() { AssetRelocator.LastSelectionOnRightClick = null; } } }