Skip to content

Commit

Permalink
add openPropertyEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
L1247 committed Mar 19, 2023
1 parent f5c2181 commit dbe598f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using System.Linq;
using System.Reflection;
using System.Text;
using JD.AssetizerEditor;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -55,6 +57,7 @@ public class BookmarkEverythingEditor : EditorWindow

private PingTypes _pingType;
private bool _visualMode;
private bool _openAsProperties;
private bool _autoClose;
private bool _showFullPath;
private bool _showFullPathForFolder;
Expand All @@ -64,6 +67,7 @@ public class BookmarkEverythingEditor : EditorWindow
private int _projectFinderTabIndex = 0;
private bool _visualModeChanged;
private bool _controlVisualMode;
private bool _controlOpenAsProperties;
private bool _controlAutoClose;
private bool _autoCloseChanged;
private bool _controlShowFullPath;
Expand Down Expand Up @@ -552,23 +556,39 @@ private void DrawInnerSettings()

EditorGUILayout.BeginHorizontal();

label = "Visual Mode(Experimental!) : ";
_controlVisualMode = _visualMode;
_visualMode = EditorGUILayout.Toggle(label , _visualMode);
label = "OpenAsProperties";
_controlOpenAsProperties = _openAsProperties;
_openAsProperties = EditorGUILayout.Toggle(label , _openAsProperties);

if (_controlVisualMode != _visualMode)
{
_visualModeChanged = true;
}
// if (_controlOpenAsProperties != _openAsProperties)
// {
// _visualModeChanged = true;
// }

if (_visualModeChanged)
Debug.Log($"{_openAsProperties}");
if (_controlOpenAsProperties != _openAsProperties)
{
VisualMode(_visualMode);
_currentSettings.VisualMode = _visualMode;
_currentSettings.OpenAsProperties = _openAsProperties;
_currentSettings.Save();
_visualModeChanged = false;
}

// label = "Visual Mode(Experimental!) : ";
// _controlVisualMode = _visualMode;
// _visualMode = EditorGUILayout.Toggle(label , _visualMode);
//
// if (_controlVisualMode != _visualMode)
// {
// _visualModeChanged = true;
// }
//
// if (_visualModeChanged)
// {
// VisualMode(_visualMode);
// _currentSettings.VisualMode = _visualMode;
// _currentSettings.Save();
// _visualModeChanged = false;
// }

EditorGUILayout.EndHorizontal();

EditorGUILayout.EndVertical();
Expand Down Expand Up @@ -663,12 +683,20 @@ private void DrawProjectFinderEntries(string category)
if (Selection.activeObject) Selection.activeObject = null;
if (Path.HasExtension(path))
{
var asset = AssetDatabase.LoadMainAssetAtPath(path);
var entryIsScene = asset is SceneAsset;
var prefabType = PrefabUtility.GetPrefabType(asset);
if (entryIsScene) SaveSceneDialog(path);
else if (prefabType == PrefabType.Prefab) AssetDatabase.OpenAsset(asset);
var asset = AssetDatabase.LoadMainAssetAtPath(path);
Selection.activeObject = asset;
Debug.Log($"{_openAsProperties}");
if (_openAsProperties)
{
OpenPropertiesEditorWindowDoubleClickListener.OpenInPropertyEditor(asset);
}
else
{
var entryIsScene = asset is SceneAsset;
var prefabType = PrefabUtility.GetPrefabType(asset);
if (entryIsScene) SaveSceneDialog(path);
else if (prefabType == PrefabType.Prefab) AssetDatabase.OpenAsset(asset);
}
}
else OpenDir(path);
}
Expand Down Expand Up @@ -1016,6 +1044,7 @@ private void LoadSettings()
_autoClose = _currentSettings.AutoClose;
_showFullPath = _currentSettings.ShowFullPath;
_showFullPathForFolder = _currentSettings.ShowFullPathForFolders;
_openAsProperties = _currentSettings.OpenAsProperties;
}

private void OnEnable()
Expand Down Expand Up @@ -1288,6 +1317,7 @@ public class SaveData
public bool ShowFullPath;
public bool ShowFullPathForFolders = true;
public bool VisualMode;
public bool OpenAsProperties;
public List<EntryData> EntryData = new List<EntryData>();
public PingTypes PingType;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

using System.Reflection;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace JD.AssetizerEditor
{
/// <summary>
/// Listens <see cref="OnOpenAssetAttribute"/> (order 100) for everything except folders.
/// Opens the asset in a new window (like an unchangeable inspector).
/// Forum thread: https://forum.unity.com/threads/alt-p-is-a-godsent.834703/
/// </summary>
public static class OpenPropertiesEditorWindowDoubleClickListener
{
private static MethodInfo openPropertyEditorInfo;
private static System.Type[] callTypes = new[] { typeof(Object), typeof(bool) };
private static object[] callOpenBuffer = { null, true };

/// <summary>
/// Listens <see cref="OnOpenAssetAttribute"/> (order 100) for everything except folders.
/// </summary>
/// <param name="instanceID"><see cref="OnOpenAssetAttribute"/></param>
/// <param name="line"><see cref="OnOpenAssetAttribute"/></param>
/// <returns>True if opening the asset is handled</returns>
[OnOpenAsset(100)]
private static bool HandleOpenAsset(int instanceID, int line)
{
Object obj = EditorUtility.InstanceIDToObject(instanceID);
if (obj == null)
{
return false;
}

if (IsFolder(obj))
{
return false;
}

return OpenInPropertyEditor(obj);
}

private static bool IsFolder(Object obj)
{
string assetPath = AssetDatabase.GetAssetPath(obj);
return !string.IsNullOrEmpty(assetPath) && AssetDatabase.IsValidFolder(assetPath);
}

public static bool OpenInPropertyEditor(Object asset)
{
if (openPropertyEditorInfo == null)
{
System.Type propertyEditorType = typeof(Editor).Assembly.GetType("UnityEditor.PropertyEditor");

// Get specific method, since there is an overload starting with Unity 2021.2
openPropertyEditorInfo = propertyEditorType.GetMethod(
"OpenPropertyEditor",
BindingFlags.Static | BindingFlags.NonPublic,
null,
callTypes,
null);
}


if (openPropertyEditorInfo != null)
{
callOpenBuffer[0] = asset;
openPropertyEditorInfo.Invoke(null, callOpenBuffer);
return true;
}

return false;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbe598f

Please sign in to comment.