Could not create sprite from a 64x64 texture

The Code

RectTransform containerRect = gameObject.GetComponent<RectTransform> ();
        RectTransform itemRect = item.GetComponent<RectTransform> ();

        float itemHeight = containerRect.rect.height;
        float ratio = itemHeight / itemRect.rect.height;
        float itemWidth = itemRect.rect.width * ratio;

 GameObject newItem = Instantiate (item) as GameObject;
            Image image = newItem.GetComponentInChildren <Image> ();
            Texture2D preview = AssetPreview.GetMiniThumbnail (newItem);

            Rect rect = new Rect (0, 0, itemWidth, itemHeight);
            image.sprite = Sprite.Create (preview, rect, new Vector2 (0f, 0f), 125);

The Question
Im trying to get a preview thumbnail of a Prefab and insert it into a Image in a Panel.
It works fine when I run it in a small window, but if I run it in “Maximize on play” i get the following error

How do I fix this?

Hmm doesnt look like it change the source image at all

Which line is the console indicating is the problem?

The error is with this line:

 image.sprite = Sprite.Create (preview, rect, new Vector2 (0f, 0f), 125);

This fixed it:

    Rect rect = new Rect (0, 0, 64, 64);
image.sprite = Sprite.Create (preview, rect, new Vector2 (0f, 0f), 125);

Now i have another problem, the GetMiniThumbnail do not show the thumbnail as seen in the Project Tab, but just a thumbnail of a “standard” prefrab icon

I have tried another way, but still no luck

public GameObject prefab;
    void Start ()
    {
        Texture2D preview = AssetPreview.GetMiniThumbnail (prefab);
        RawImage image = GetComponent<RawImage> ();
        image.texture = preview;
    }

@drdenner

Just Use
image.overrideSprite = Sprite.Create(texture, new Rect(0,0, <texture.width> , <texture.height>),new Vector2(0.5f, 0.5f),100);
hope it will be ok. Thats it

1 Like

Hello, I’m making something like a “magic wand” that paints the drawing the original color.

For that I modified the PatternBrush as follows [attachment] and this way I get the texture of my assets.
However, the texture is coming in the wrong size/place.
Do you have any solution?

Screenshot by Lightshot7851702–996222–Captura de tela 2022-01-29 160040.png|977x452

In Build Settings set Max Texture Size - No override and click Apply button