-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...rything/Assets/BookmarkEverything/Editor/OpenPropertiesEditorWindowDoubleClickListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ng/Assets/BookmarkEverything/Editor/OpenPropertiesEditorWindowDoubleClickListener.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.