Skip to content

Commit

Permalink
Merge pull request #70 from Yusuke57/add-preview-attribute
Browse files Browse the repository at this point in the history
Add:  PreviewAttribute
  • Loading branch information
AnnulusGames authored Mar 21, 2024
2 parents 9b53c07 + 1e27c65 commit da23ce5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Alchemy/Assets/Alchemy/Editor/BuiltinAttributeDrawers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,68 @@ public override void OnCreateElement()
}
}

[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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public HelpBoxAttribute(string message, HelpBoxMessageType messageType = HelpBox
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
{
Expand Down
49 changes: 49 additions & 0 deletions Alchemy/Assets/Alchemy/Samples~/Samples/Samples.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

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

0 comments on commit da23ce5

Please sign in to comment.