Skip to content

Commit

Permalink
Numerous bugfixes (especially those that occured when entering/exitin…
Browse files Browse the repository at this point in the history
…g play mode while inspecting an object with a preview)
  • Loading branch information
yasirkula committed Feb 14, 2020
1 parent b9d58e7 commit b866f4b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 16 deletions.
9 changes: 6 additions & 3 deletions Plugins/InspectPlus/Editor/CustomProjectWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
public delegate void ProjectWindowSelectionChangedDelegate( IList<int> newSelection );

Expand Down Expand Up @@ -148,7 +148,10 @@ public CustomProjectWindowDrawer( TreeViewState state, string rootDirectory ) :

protected override TreeViewItem BuildRoot()
{
return new TreeViewItem { id = GetInstanceIDFromPath( rootDirectory ), depth = -1 };
if( AssetDatabase.IsValidFolder( rootDirectory ) )
return new TreeViewItem { id = GetInstanceIDFromPath( rootDirectory ), depth = -1 };

return new TreeViewItem { id = -1, depth = -1 };
}

protected override IList<TreeViewItem> BuildRows( TreeViewItem root )
Expand Down Expand Up @@ -593,7 +596,7 @@ private bool MoveAssets( IList<Object> assets, string parentFolder )
string filename = Path.GetFileName( paths[i] );
for( int j = 0; j < entries.Length; j++ )
{
if( filename == entries[i] )
if( filename == entries[j] )
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Plugins/InspectPlus/Editor/DebugModeEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using UnityEngine;
using Object = UnityEngine.Object;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
public class DebugModeEntry
{
Expand Down
2 changes: 1 addition & 1 deletion Plugins/InspectPlus/Editor/InspectPlusSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEditor;
using UnityEngine;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
public class InspectPlusSettings : ScriptableObject
{
Expand Down
90 changes: 83 additions & 7 deletions Plugins/InspectPlus/Editor/InspectPlusWindow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#if UNITY_2019_3_OR_NEWER
#define APPLY_HORIZONTAL_PADDING // Inspector looks better with a padding in the new UI
#endif
//#define APPLY_HORIZONTAL_PADDING

using InspectPlusNamespace.Extras;
using System;
using System.Collections.Generic;
using System.Reflection;
Expand Down Expand Up @@ -98,6 +95,9 @@ private enum ButtonState { Normal = -1, LeftClicked = 0, RightClicked = 1, Middl
private bool syncProjectWindowSelection;
private Editor projectWindowSelectionEditor;

#if UNITY_2017_2_OR_NEWER
private bool changingPlayMode;
#endif
private bool shouldRepositionSelf;
private bool shouldRepaint;
private bool snapFavoritesToActiveObject;
Expand All @@ -114,9 +114,9 @@ private enum ButtonState { Normal = -1, LeftClicked = 0, RightClicked = 1, Middl
private float previewHeight;
private float previewLastHeight;
private bool previewHeaderClicked;
private GUIStyle previewHeaderGuiStyle;
private GUIStyle previewResizeAreaGuiStyle;
private GUIStyle previewBackgroundGuiStyle;
private static GUIStyle previewHeaderGuiStyle;
private static GUIStyle previewResizeAreaGuiStyle;
private static GUIStyle previewBackgroundGuiStyle;

private bool debugMode;
private double debugModeRefreshTime;
Expand Down Expand Up @@ -201,6 +201,10 @@ private void OnEnable()

Undo.undoRedoPerformed -= OnUndoRedo;
Undo.undoRedoPerformed += OnUndoRedo;
#if UNITY_2017_2_OR_NEWER
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
#endif

if( mainObject )
{
Expand Down Expand Up @@ -239,6 +243,9 @@ private void OnDisable()
windows.Remove( this );

Undo.undoRedoPerformed -= OnUndoRedo;
#if UNITY_2017_2_OR_NEWER
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
#endif

historyHolder.Clear();
favoritesHolder.Clear();
Expand Down Expand Up @@ -270,6 +277,40 @@ private void OnUndoRedo()
shouldRepaint = true;
}

#if UNITY_2017_2_OR_NEWER
private void OnPlayModeStateChanged( PlayModeStateChange state )
{
if( state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.ExitingPlayMode )
{
changingPlayMode = true;

DestroyImmediate( projectWindowSelectionEditor );
projectWindowSelectionEditor = null;
inspectorAssetDrawer = null;
}
else
{
changingPlayMode = false;

for( int i = 0; i < inspectorDrawerCount; i++ )
{
Editor editor = inspectorDrawers[i];
if( editor && editor.target )
{
Object target = editor.target;
DestroyImmediate( editor );
editor = Editor.CreateEditor( target );

inspectorDrawers[i] = editor;
}
}

if( projectWindow.GetTreeView() != null )
ProjectWindowSelectionChanged( projectWindow.GetTreeView().GetSelection() );
}
}
#endif

private void RefreshSettings()
{
favoritesHeight = GUILayout.Height( InspectPlusSettings.Instance.FavoritesHeight );
Expand Down Expand Up @@ -905,6 +946,11 @@ private void ProjectWindowSelectionChanged( IList<int> newSelection )
#region GUI Functions
private void OnGUI()
{
#if UNITY_2017_2_OR_NEWER
if( changingPlayMode )
return;
#endif

Event ev = Event.current;
if( ev.type == EventType.ScrollWheel )
{
Expand Down Expand Up @@ -962,8 +1008,14 @@ private void OnGUI()
{
GUILayout.Space( 0 ); // Somehow gets rid of the free space above the inspector header

float windowWidth = position.width;
bool originalWideMode = EditorGUIUtility.wideMode;
float originalLabelWidth = EditorGUIUtility.labelWidth;

if( inspectorAssetDrawer )
{
AdjustLabelWidth( windowWidth );

inspectorAssetDrawer.DrawHeader();
inspectorAssetDrawer.OnInspectorGUI();

Expand All @@ -974,18 +1026,24 @@ private void OnGUI()
Rect importedObjectHeaderRect = GUILayoutUtility.GetRect( 0, 100000, 21f, 21f );
GUI.Box( importedObjectHeaderRect, "Imported Object" );

#if !UNITY_2019_3_OR_NEWER
GUILayout.Space( -7 ); // Get rid of the space before the firstDrawer's header
#endif
}
}

if( inspectorDrawerCount > 0 )
{
AdjustLabelWidth( windowWidth );

Editor firstDrawer = inspectorDrawers[0];
firstDrawer.DrawHeader();
firstDrawer.OnInspectorGUI();

for( int i = 1; i < inspectorDrawerCount; i++ )
{
AdjustLabelWidth( windowWidth );

Object targetObject = inspectorDrawers[i].target;
if( targetObject )
{
Expand Down Expand Up @@ -1016,6 +1074,9 @@ private void OnGUI()
}
}

EditorGUIUtility.wideMode = originalWideMode;
EditorGUIUtility.labelWidth = originalLabelWidth;

if( showProjectWindow )
{
GUILayout.Space( -4 ); // Get rid of the free space above the project window's header
Expand Down Expand Up @@ -1279,7 +1340,13 @@ private void DrawPreview( Editor editor )
if( previewHeaderGuiStyle == null )
previewHeaderGuiStyle = EditorStyles.toolbar;
if( previewResizeAreaGuiStyle == null )
{
#if UNITY_2019_3_OR_NEWER
previewResizeAreaGuiStyle = GUI.skin.horizontalScrollbarThumb;
#else
previewResizeAreaGuiStyle = EditorStyles.helpBox;
#endif
}
if( previewBackgroundGuiStyle == null )
previewBackgroundGuiStyle = EditorStyles.toolbar;
}
Expand All @@ -1295,6 +1362,9 @@ private void DrawPreview( Editor editor )
{
Rect dragIconRect = new Rect( dragRect.x + PREVIEW_HEADER_PADDING, dragRect.y + ( PREVIEW_HEADER_HEIGHT - previewResizeAreaGuiStyle.fixedHeight ) * 0.5f - 1f,
dragRect.width - 2f * PREVIEW_HEADER_PADDING, previewResizeAreaGuiStyle.fixedHeight );
#if UNITY_2019_3_OR_NEWER
dragIconRect.y++;
#endif

previewResizeAreaGuiStyle.Draw( dragIconRect, GUIContent.none, false, false, false, false );
}
Expand Down Expand Up @@ -1380,6 +1450,12 @@ private void DrawPreview( Editor editor )
}
}

private void AdjustLabelWidth( float windowWidth )
{
EditorGUIUtility.wideMode = windowWidth > 330f;
EditorGUIUtility.labelWidth = windowWidth < 350f ? 130f : windowWidth * 0.4f;
}

private void DrawHorizontalLine()
{
GUILayout.Box( "", expandWidth, horizontalLineHeight );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine;
using Object = UnityEngine.Object;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
public static class SerializablePropertyExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion Plugins/InspectPlus/Editor/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using UnityEngine;
using Object = UnityEngine.Object;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
public static class Utilities
{
Expand Down
2 changes: 1 addition & 1 deletion Plugins/InspectPlus/Editor/VariableGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Reflection;
using System.Text;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
// Delegate to get the value of a variable (either field or property)
public delegate object VariableGetVal( object obj );
Expand Down
2 changes: 1 addition & 1 deletion Plugins/InspectPlus/Other/SceneFavoritesHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#endif
using UnityEngine;

namespace InspectPlusNamespace.Extras
namespace InspectPlusNamespace
{
[ExecuteInEditMode]
public class SceneFavoritesHolder : MonoBehaviour
Expand Down

0 comments on commit b866f4b

Please sign in to comment.