Skip to content

Commit

Permalink
Added "Add to Basket" button to Inspect+ context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Sep 30, 2023
1 parent 97dd0a3 commit ec1bc33
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 59 deletions.
75 changes: 44 additions & 31 deletions Plugins/InspectPlus/Editor/BasketWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BasketWindow>() : GetWindow<BasketWindow>();
window.titleObjectCount = 0;
Expand All @@ -47,6 +47,7 @@ public class BasketWindow : EditorWindow, IHasCustomMenu
window.LoadData( ACTIVE_WINDOW_SAVE_FILE );

window.Show();
return window;
}

void IHasCustomMenu.AddItemsToMenu( GenericMenu menu )
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -180,8 +193,7 @@ private void LoadData()

private void OnGUI()
{
if( treeView == null )
treeView = new BasketWindowDrawer( treeViewState );
InitializeTreeViewIfNecessary();

if( searchField == null )
{
Expand Down Expand Up @@ -381,34 +393,7 @@ protected override DragAndDropVisualMode HandleDragAndDrop( DragAndDropArgs args
return DragAndDropVisualMode.None;

if( args.performDrop )
{
List<Object> objects = state.objects;
Object[] draggedObjects = DragAndDrop.objectReferences;
List<int> draggedInstanceIDs = new List<int>( 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;
}
Expand All @@ -434,6 +419,34 @@ protected override void CommandEventHandling()
base.CommandEventHandling();
}

public void AddObjects( Object[] objectsToAdd, int insertIndex )
{
List<Object> objects = state.objects;
List<int> addedInstanceIDs = new List<int>( 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<int> instanceIDs )
{
bool removedObjects = false;
Expand Down
30 changes: 30 additions & 0 deletions Plugins/InspectPlus/Editor/MenuItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
30 changes: 3 additions & 27 deletions Plugins/InspectPlus/README.txt
Original file line number Diff line number Diff line change
@@ -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)
Documentation: https://github.com/yasirkula/UnityInspectPlus
E-mail: yasirkula@gmail.com
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ec1bc33

Please sign in to comment.