Skip to content

Commit

Permalink
Basket window's contents are now persistent and can also be saved/loa…
Browse files Browse the repository at this point in the history
…ded on demand (scene objects aren't persistent, unfortunately)
  • Loading branch information
yasirkula committed May 7, 2022
1 parent f9e078c commit 5abd10a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ There are 4 ways to install this plugin:

![screenshot](Images/PasteFromBin.png)

- You can open Basket via **Window/Inspect+/Basket**: this window temporarily stores the objects that you drag&drop inside it (once the window is closed, its contents will be lost)
- You can open Basket via **Window/Inspect+/Basket**: this window stores the objects that you drag&drop inside it (unfortunately, once the window is closed, its scene object contents will be lost). You can right click the window's tab to save its contents to a file

![screenshot](Images/Basket.png)

Expand Down
64 changes: 62 additions & 2 deletions Plugins/InspectPlus/Editor/BasketWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
Expand All @@ -8,6 +9,10 @@ namespace InspectPlusNamespace
{
public class BasketWindow : EditorWindow, IHasCustomMenu
{
private const string SAVE_FILE_EXTENSION = "basket";
private const string SAVE_DIRECTORY = "Library/BasketWindows";
private const string LAST_WINDOW_SAVE_FILE = SAVE_DIRECTORY + "/_ActiveWindow." + SAVE_FILE_EXTENSION;

#pragma warning disable 0649
private BasketWindowDrawer treeView;
[SerializeField]
Expand All @@ -16,7 +21,8 @@ public class BasketWindow : EditorWindow, IHasCustomMenu
#pragma warning restore 0649

private bool shouldRepositionSelf;
private int titleObjectCount = -1;
private bool hasContentsBeenModified;
private int titleObjectCount = 0;

public static new void Show( bool newInstance )
{
Expand All @@ -26,15 +32,45 @@ public class BasketWindow : EditorWindow, IHasCustomMenu

if( newInstance )
window.shouldRepositionSelf = true;
else if( window.treeViewState.objects.Count == 0 && File.Exists( LAST_WINDOW_SAVE_FILE ) )
window.LoadData( LAST_WINDOW_SAVE_FILE );

window.Show();
}

void IHasCustomMenu.AddItemsToMenu( GenericMenu menu )
{
if( treeView == null )
return;

if( treeViewState.objects.Count > 0 )
{
menu.AddItem( new GUIContent( "Save..." ), false, () =>
{
Directory.CreateDirectory( SAVE_DIRECTORY );

string savePath = EditorUtility.SaveFilePanel( "Save As", SAVE_DIRECTORY, "", SAVE_FILE_EXTENSION );
if( !string.IsNullOrEmpty( savePath ) )
SaveData( savePath );
} );
}
else
menu.AddDisabledItem( new GUIContent( "Save..." ) );

menu.AddItem( new GUIContent( "Load..." ), false, () =>
{
Directory.CreateDirectory( SAVE_DIRECTORY );

string loadPath = EditorUtility.OpenFilePanel( "Load", SAVE_DIRECTORY, SAVE_FILE_EXTENSION );
if( !string.IsNullOrEmpty( loadPath ) )
LoadData( loadPath );
} );

menu.AddSeparator( "" );

menu.AddItem( new GUIContent( "Synchronize Selection With Unity" ), treeViewState.syncSelection, () => treeViewState.syncSelection = !treeViewState.syncSelection );

if( treeView != null && treeViewState.objects.Count > 0 )
if( treeViewState.objects.Count > 0 )
{
menu.AddSeparator( "" );

Expand All @@ -52,6 +88,20 @@ void IHasCustomMenu.AddItemsToMenu( GenericMenu menu )
}
}

private void SaveData( string path )
{
File.WriteAllText( path, EditorJsonUtility.ToJson( treeViewState, true ) );
}

private void LoadData( string path )
{
EditorJsonUtility.FromJsonOverwrite( File.ReadAllText( path ), treeViewState );
treeViewState.objects.RemoveAll( ( obj ) => !obj );

if( treeView != null )
treeView.Reload();
}

private void Awake()
{
treeViewState.syncSelection = InspectPlusSettings.Instance.SyncBasketSelection;
Expand All @@ -62,6 +112,15 @@ private void OnEnable()
treeViewState.objects.RemoveAll( ( obj ) => !obj );
}

private void OnDestroy()
{
if( hasContentsBeenModified )
{
Directory.CreateDirectory( SAVE_DIRECTORY );
SaveData( LAST_WINDOW_SAVE_FILE );
}
}

private void OnGUI()
{
if( treeView == null )
Expand Down Expand Up @@ -93,6 +152,7 @@ private void OnGUI()
{
titleObjectCount = treeViewState.objects.Count;
titleContent = new GUIContent( "Basket (" + titleObjectCount + ")" );
hasContentsBeenModified = true;
}

if( shouldRepositionSelf )
Expand Down
2 changes: 1 addition & 1 deletion Plugins/InspectPlus/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ This plugin helps you view an object's Inspector in a separate tab/window, copy&
- 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 temporarily stores the objects that you drag&drop inside it (once the window is closed, its contents will be lost)
- You can open Basket via "Window/Inspect+/Basket": this window stores the objects that you drag&drop inside it (unfortunately, once the window is closed, its scene object contents will be lost). You can right click the window's tab to save its contents to a file
- 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)
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.3",
"version": "1.8.4",
"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 5abd10a

Please sign in to comment.