Add: PreviewAttribute

This commit is contained in:
Yusuke57 2024-03-17 18:23:47 +09:00
parent bcaeef5ea9
commit 036584f3ca
2 changed files with 65 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))] [CustomAttributeDrawer(typeof(HorizontalLineAttribute))]
public sealed class HorizontalLineDrawer : AlchemyAttributeDrawer public sealed class HorizontalLineDrawer : AlchemyAttributeDrawer
{ {

View File

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