From ec1bc334bb2f9552506f15a1931da046e9c1248b Mon Sep 17 00:00:00 2001 From: yasirkula Date: Sat, 30 Sep 2023 22:05:33 +0300 Subject: [PATCH] Added "Add to Basket" button to Inspect+ context menu --- Plugins/InspectPlus/Editor/BasketWindow.cs | 75 +++++++++++++--------- Plugins/InspectPlus/Editor/MenuItems.cs | 30 +++++++++ Plugins/InspectPlus/README.txt | 30 +-------- package.json | 2 +- 4 files changed, 78 insertions(+), 59 deletions(-) diff --git a/Plugins/InspectPlus/Editor/BasketWindow.cs b/Plugins/InspectPlus/Editor/BasketWindow.cs index dd343ef..faef0c7 100644 --- a/Plugins/InspectPlus/Editor/BasketWindow.cs +++ b/Plugins/InspectPlus/Editor/BasketWindow.cs @@ -34,7 +34,7 @@ public class BasketWindow : EditorWindow, IHasCustomMenu private bool isDirtyActiveWindow; private int titleObjectCount = 0; - public static new void Show( bool newInstance ) + public static new BasketWindow Show( bool newInstance ) { BasketWindow window = newInstance ? CreateInstance() : GetWindow(); window.titleObjectCount = 0; @@ -47,6 +47,7 @@ public class BasketWindow : EditorWindow, IHasCustomMenu window.LoadData( ACTIVE_WINDOW_SAVE_FILE ); window.Show(); + return window; } void IHasCustomMenu.AddItemsToMenu( GenericMenu menu ) @@ -143,6 +144,18 @@ private void OnDestroy() SaveData(); } + private void InitializeTreeViewIfNecessary() + { + if( treeView == null ) + treeView = new BasketWindowDrawer( treeViewState ); + } + + public void AddToBasket( Object[] objects ) + { + InitializeTreeViewIfNecessary(); + treeView.AddObjects( objects, treeViewState.objects.Count ); + } + private void SaveData() { if( isDirtyActiveWindow ) @@ -180,8 +193,7 @@ private void LoadData() private void OnGUI() { - if( treeView == null ) - treeView = new BasketWindowDrawer( treeViewState ); + InitializeTreeViewIfNecessary(); if( searchField == null ) { @@ -381,34 +393,7 @@ protected override DragAndDropVisualMode HandleDragAndDrop( DragAndDropArgs args return DragAndDropVisualMode.None; if( args.performDrop ) - { - List objects = state.objects; - Object[] draggedObjects = DragAndDrop.objectReferences; - List draggedInstanceIDs = new List( draggedObjects.Length ); - int insertIndex = ( args.dragAndDropPosition == DragAndDropPosition.OutsideItems ) ? objects.Count : args.insertAtIndex; - for( int i = 0; i < draggedObjects.Length; i++ ) - { - if( !draggedObjects[i] ) - continue; - - objects.Insert( insertIndex + draggedInstanceIDs.Count, draggedObjects[i] ); - draggedInstanceIDs.Add( draggedObjects[i].GetInstanceID() ); - } - - int addedObjectCount = draggedInstanceIDs.Count; - if( addedObjectCount > 0 ) - { - // Remove duplicates - for( int i = objects.Count - 1; i >= 0; i-- ) - { - if( ( i < insertIndex || i >= insertIndex + addedObjectCount ) && System.Array.IndexOf( draggedObjects, objects[i] ) >= 0 ) - objects.RemoveAt( i ); - } - - SetSelection( draggedInstanceIDs, TreeViewSelectionOptions.FireSelectionChanged ); - Reload(); - } - } + AddObjects( DragAndDrop.objectReferences, ( args.dragAndDropPosition == DragAndDropPosition.OutsideItems ) ? state.objects.Count : args.insertAtIndex ); return DragAndDropVisualMode.Copy; } @@ -434,6 +419,34 @@ protected override void CommandEventHandling() base.CommandEventHandling(); } + public void AddObjects( Object[] objectsToAdd, int insertIndex ) + { + List objects = state.objects; + List addedInstanceIDs = new List( objectsToAdd.Length ); + for( int i = 0; i < objectsToAdd.Length; i++ ) + { + if( !objectsToAdd[i] ) + continue; + + objects.Insert( insertIndex + addedInstanceIDs.Count, objectsToAdd[i] ); + addedInstanceIDs.Add( objectsToAdd[i].GetInstanceID() ); + } + + int addedObjectCount = addedInstanceIDs.Count; + if( addedObjectCount > 0 ) + { + // Remove duplicates + for( int i = objects.Count - 1; i >= 0; i-- ) + { + if( ( i < insertIndex || i >= insertIndex + addedObjectCount ) && System.Array.IndexOf( objectsToAdd, objects[i] ) >= 0 ) + objects.RemoveAt( i ); + } + + SetSelection( addedInstanceIDs, TreeViewSelectionOptions.FireSelectionChanged ); + Reload(); + } + } + private void RemoveObjects( IList instanceIDs ) { bool removedObjects = false; diff --git a/Plugins/InspectPlus/Editor/MenuItems.cs b/Plugins/InspectPlus/Editor/MenuItems.cs index 7f3ccd3..107abca 100644 --- a/Plugins/InspectPlus/Editor/MenuItems.cs +++ b/Plugins/InspectPlus/Editor/MenuItems.cs @@ -115,6 +115,36 @@ private static void ContextMenuItemNewWindow( MenuCommand command ) } #endregion + #region Add to Basket Buttons + [MenuItem( "GameObject/Inspect+/Add to Basket", priority = 49 )] + [MenuItem( "Assets/Inspect+/Add to Basket", priority = 1500 )] +#if UNITY_2019_4_OR_NEWER + [MenuItem( "CONTEXT/Component/Inspect+/Add to Basket", priority = 1475 )] + [MenuItem( "CONTEXT/ScriptableObject/Inspect+/Add to Basket", priority = 1475 )] + [MenuItem( "CONTEXT/AssetImporter/Inspect+/Add to Basket", priority = 1475 )] + [MenuItem( "CONTEXT/Material/Inspect+/Add to Basket", priority = 1475 )] +#else + [MenuItem( "CONTEXT/Component/Add to Basket", priority = 1475 )] + [MenuItem( "CONTEXT/ScriptableObject/Add to Basket", priority = 1475 )] + [MenuItem( "CONTEXT/AssetImporter/Add to Basket", priority = 1475 )] + [MenuItem( "CONTEXT/Material/Add to Basket", priority = 1475 )] +#endif + private static void ContextMenuItemAddToBasket( MenuCommand command ) + { + if( command.context ) + BasketWindow.Show( false ).AddToBasket( new Object[] { ( command.context is AssetImporter ) ? AssetDatabase.LoadMainAssetAtPath( ( (AssetImporter) command.context ).assetPath ) : command.context } ); + else + BasketWindow.Show( false ).AddToBasket( Selection.objects ); + } + + [MenuItem( "GameObject/Inspect+/Add to Basket", validate = true )] + [MenuItem( "Assets/Inspect+/Add to Basket", validate = true )] + private static bool ContextMenuItemAddToBasketValidate( MenuCommand command ) + { + return Selection.objects.Length > 0; + } + #endregion + #region Isolated Hierarchy Buttons [MenuItem( "GameObject/Inspect+/Isolated Hierarchy/Open In New Tab", priority = 50 )] private static void ContextMenuItemOpenIsolatedHierarchyNewTab( MenuCommand command ) diff --git a/Plugins/InspectPlus/README.txt b/Plugins/InspectPlus/README.txt index 04d8083..33574fa 100644 --- a/Plugins/InspectPlus/README.txt +++ b/Plugins/InspectPlus/README.txt @@ -1,28 +1,4 @@ -= Inspect+ = += Inspect+ (v1.8.7) = -Online documentation & example code available at: https://github.com/yasirkula/UnityInspectPlus -E-mail: yasirkula@gmail.com - -1. ABOUT -This plugin helps you view an object's Inspector in a separate tab/window, copy&paste the values of variables in the Inspector, inspect all variables of an object (including non-serializable and static variables) in an enhanced Debug mode and more. - - -2. HOW TO -- You can open the Inspect+ window in a number of ways: - 1) right clicking an object in Project or Hierarchy windows - 2) right clicking an Object variable in the Inspector - 3) right clicking a component in the Inspector - 4) selecting Window/Inspect+/New Window menu item - 5) calling the InspectPlusNamespace.InspectPlusWindow.Inspect functions from your editor scripts -- You can right click an object in the History list to add it to the Favorites list -- You can drag&drop objects to the History and Favorites lists to quickly fill these lists -- You can right click the icons of the History and Favorites lists to quickly select an object from these lists -- You can right click variables or components in the Inspector to copy&paste their values. This supports all variable types: primitives, scene objects, assets, managed references, arrays, serializable objects and etc. Paste operation is quite flexible, as well; you can paste different vector types to each other and paste any component to another with no type restrictions, as long as these components have some variables with the same name. Note that copy/paste menu won't show up for variables that are not drawn with SerializedProperty -- You can right click a component and copy multiple components attached to that GameObject at once. Then, you can right click another component and paste multiple components at once -- You can right click an object in Hierarchy and copy its complete hierarchy (with or without its children). Then, you can paste these objects to another Unity project's hierarchy; Unity versions don't need to match (however, assets that don't exist on the other project will become missing references) -- You can right click the Inspect+ tab to enable Debug mode: you can inspect all variables of an object in this mode, including static, readonly and non-serializable variables -- You can right click an object in Hierarchy and select the "Isolated Hierarchy" option to open a Hierarchy window that displays only that object's children -- You can open a folder with Inspect+ to see its contents in an isolated Project view -- You can open Paste Bin via "Window/Inspect+/Paste Bin": this window lists the copied variables and is shared between all Unity projects (so, copying a value in Project A will make that value available in Project B). You can also right click variables, components or materials in the Inspector and select "Paste Values From Bin" to quickly select and paste a value from Paste Bin -- You can open Basket via "Window/Inspect+/Basket": this window stores the objects that you drag&drop inside it. You can right click the window's tab to save its contents to a file (on Unity 2019.1 or earlier, scene object contents aren't saved) -- You can open Object Diff Window via "Window/Inspect+/Diff Window": this window lets you see the differences between two objects in your project (diff of two GameObjects won't include their child GameObjects) \ No newline at end of file +Documentation: https://github.com/yasirkula/UnityInspectPlus +E-mail: yasirkula@gmail.com \ No newline at end of file diff --git a/package.json b/package.json index aa8aae6..66be047 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.yasirkula.inspectplus", "displayName": "Inspect+", - "version": "1.8.6", + "version": "1.8.7", "documentationUrl": "https://github.com/yasirkula/UnityInspectPlus", "changelogUrl": "https://github.com/yasirkula/UnityInspectPlus/releases", "licensesUrl": "https://github.com/yasirkula/UnityInspectPlus/blob/master/LICENSE.txt",