I want to get image of any asset from code. Unity can display assets as with images of those assets. Is there a way to get that image from code? I need it to be able to display it in the inspector.
public class AssetIconDisplayAttribute : MultiSupportPropertyAttribute
{
#if UNITY_EDITOR
public override void DrawInInspector(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUILayout.ObjectField(new GUIContent(property.displayName), property.objectReferenceValue, property.GetContainingObjectFieldType(), true);
EditorGUI.DrawPreviewTexture(position, @that asset image here@);
}
#endif
}
Is there a better way to achieve this? The ideal result I want is object field that is displayed like a Texture2D field in inspector but is not a type of Texture2D but stays its own type.
EDITS:
With AssetPreview.GetMiniThumbnail:

With AssetPreview.GetAssetPreview:
Gives null if the object reference is a component, you have to pass GameObject in order to get the value.
Fix:
AssetPreview.GetAssetPreview((serializedProperty.objectReferenceValue as Component).gameObject)
With AssetDatabase.GetCachedIcon:

@EDIT - note: the code won't work out of the box as some elements which are not important/related for/to this task are custom made. This is what I currently have and what has been working for me before in the old project. Because of some non-related errors after upgrading to new unity version I can't get an example of how it looked but anyway I wanted to share it.
public class AssetIconDisplayAttribute : MultiSupportPropertyAttribute
{
#if UNITY_EDITOR
public override float GetPropertyExtensionHeight()
{
if (this.serializedProperty != null && this.serializedProperty.objectReferenceValue != null)
{
return EditorGUIUtility.singleLineHeight + 80f;
}
return EditorGUIUtility.singleLineHeight;
}
public override void DrawInInspector(Rect rect, SerializedProperty serializedProperty, GUIContent label)
{
EditorGUI.PropertyField(new Rect(rect.position, new Vector2(rect.size.x, EditorGUIUtility.singleLineHeight)), serializedProperty);
if (serializedProperty.objectReferenceValue != null)
{
if (AssetPreview.IsLoadingAssetPreview(serializedProperty.objectReferenceValue.GetInstanceID()))
{
EditorGUI.DrawPreviewTexture(new Rect(new Vector2(rect.max.x - 82f, rect.position.y + EditorGUIUtility.singleLineHeight * 1.5f), new Vector2(64f, 64f)), AssetPreview.GetMiniThumbnail(serializedProperty.objectReferenceValue), null, ScaleMode.ScaleToFit);
}
else
{
EditorGUI.DrawPreviewTexture(new Rect(new Vector2(rect.max.x - 82f, rect.position.y + EditorGUIUtility.singleLineHeight * 1.5f), new Vector2(64f, 64f)), AssetPreview.GetAssetPreview((serializedProperty.objectReferenceValue as Component).gameObject), null, ScaleMode.ScaleToFit);
}
}
}
#endif
}



null, I noticed this in the docs: "Since previews are loaded asynchronously methods are provided for requesting if all previews have been fully loaded" so you might need to request the preview, tolerate an empty result for a little while, then check back for the real texture once it's finished loading. \$\endgroup\$