Merge pull request #70 from Yusuke57/add-preview-attribute

Add:  PreviewAttribute
This commit is contained in:
Annulus Games 2024-03-21 10:50:00 +09:00 committed by GitHub
commit da23ce5eee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 130 additions and 0 deletions

View File

@ -239,6 +239,68 @@ namespace Alchemy.Editor.Drawers
}
}
[CustomAttributeDrawer(typeof(PreviewAttribute))]
public sealed class PreviewDrawer : TrackSerializedObjectAttributeDrawer
{
private Image image;
private const float PreviewSize = 40f;
private const float BorderWidth = 1f;
private static readonly Color borderColor = new Color(0f, 0f, 0f, 0.3f);
public override void OnCreateElement()
{
if (SerializedProperty.propertyType != SerializedPropertyType.ObjectReference) return;
image = new Image
{
scaleMode = ScaleMode.ScaleToFit,
style = {
width = PreviewSize,
height = PreviewSize,
marginTop = EditorGUIUtility.standardVerticalSpacing,
marginBottom = EditorGUIUtility.standardVerticalSpacing * 4f,
alignSelf = Align.FlexEnd,
borderTopWidth = BorderWidth,
borderBottomWidth = BorderWidth,
borderLeftWidth = BorderWidth,
borderRightWidth = BorderWidth,
borderBottomColor = borderColor,
borderTopColor = borderColor,
borderLeftColor = borderColor,
borderRightColor = borderColor,
}
};
image.RegisterCallback<MouseDownEvent>(x =>
{
using var mouseDownEvent = MouseDownEvent.GetPooled(x);
var objectFieldSelector = TargetElement.Q(className: "unity-object-field__selector");
mouseDownEvent.target = objectFieldSelector;
objectFieldSelector.SendEvent(mouseDownEvent);
});
var parent = TargetElement.parent;
parent.Insert(parent.IndexOf(TargetElement) + 1, image);
base.OnCreateElement();
}
protected override void OnInspectorChanged()
{
if (SerializedProperty.objectReferenceValue == null)
{
image.image = null;
return;
}
TargetElement.schedule.Execute(() =>
{
var texture = AssetPreview.GetAssetPreview(SerializedProperty.objectReferenceValue);
image.image = texture;
}).Until(() => image.image != null);
}
}
[CustomAttributeDrawer(typeof(HorizontalLineAttribute))]
public sealed class HorizontalLineDrawer : AlchemyAttributeDrawer
{

View File

@ -136,6 +136,9 @@ namespace Alchemy.Inspector
public HelpBoxMessageType MessageType { get; }
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
public sealed class PreviewAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
public sealed class HorizontalLineAttribute : Attribute
{

View File

@ -1120,6 +1120,7 @@ Transform:
- {fileID: 786074858}
- {fileID: 306442429}
- {fileID: 535909118}
- {fileID: 2046093442}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &978336615
@ -2085,6 +2086,54 @@ Transform:
- {fileID: 565026220}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2046093441
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2046093442}
- component: {fileID: 2046093443}
m_Layer: 0
m_Name: '[Preview]'
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2046093442
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2046093441}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 801201067}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2046093443
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2046093441}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 36abc8eeae5f4d0482fe8f93429ee9f7, type: 3}
m_Name:
m_EditorClassIdentifier:
foo: {fileID: 0}
bar: {fileID: 0}
baz: {fileID: 0}
qux: {fileID: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,13 @@
using UnityEngine;
using Alchemy.Inspector;
namespace Alchemy.Samples
{
public class PreviewSample : MonoBehaviour
{
[Preview] public Sprite foo;
[Preview] public Texture bar;
[Preview] public Material baz;
[Preview] public GameObject qux;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 36abc8eeae5f4d0482fe8f93429ee9f7
timeCreated: 1710665552