How to use RenderStaticPreview with a user defined property?

I’m attempting to use the RenderStaticPreview for an Editor script, but no matter what I do it keeps crashing…so I must be doing something wrong. I want to know how it should work.

Here is the actual code I’m using which keeps crashing the editor:

public class Actor : ScriptableObject
{
	public Texture2D face;
}

[CustomEditor(typeof(Actor))]
public class ActorEditor : Editor
{

    public override Texture2D RenderStaticPreview (string assetPath, UnityEngine.Object[] subAssets, int width, int height)
    {
        Actor actor = target as Actor;

        return actor.face;
    }

}

It looks like I’ve found the answer to my own question! I guess no one else knows, so hopefully this should help anyone else who has a similar problem.

This function allowed me to load the Texture2D without the editor crashing:

So basically I replaced the code above with:

	public override Texture2D RenderStaticPreview (string assetPath, UnityEngine.Object[] subAssets, int width, int height)
	{
		Actor actor = target as Actor;
		
		if (actor == null || actor.face == null)
			return null;
		
		Texture2D cache = new Texture2D (width, height);
		EditorUtility.CopySerialized (AssetPreview.GetAssetPreview (actor.face.texture), cache);

		return cache;
	}

Although seeing that it requires the serialized version makes me realize that I could also use the: serializedObject.FindProperty of the editor class itself. However, I like this way better since it doesn’t require me to find the property by string.

To anyone using the answer by @Mako-Infused and getting null references from the GetAssetPreview method, this is because the GetAssetPreview is async, this has been mentioned somewhere in the forums before but it’s recommended to use AssetPreview.IsLoadingAssetPreview(instance ID), but this has the potential of causing the editor to freeze (always returning true even after the preview has been loaded)

Try the modification below which has been working great for me.

public override Texture2D RenderStaticPreview (string assetPath, UnityEngine.Object[] subAssets, int width, int height)
     {
         Actor actor = target as Actor;
         
         if (actor == null || actor.face == null)
             return null;

Texture2D previewTexture = null;

            while (previewTexture == null)
            {
                previewTexture = AssetPreview.GetAssetPreview(actor.face.texture);
            }
         
         Texture2D cache = new Texture2D (width, height);
         EditorUtility.CopySerialized (preview, cache);
         return cache;
     }