Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Could not create sprite from a 64x64 texture

Discussion in 'Getting Started' started by drdenner, Dec 21, 2015.

  1. drdenner

    drdenner

    Joined:
    Jun 24, 2014
    Posts:
    32
    The Code

    Code (CSharp):
    1.  
    2. RectTransform containerRect = gameObject.GetComponent<RectTransform> ();
    3.         RectTransform itemRect = item.GetComponent<RectTransform> ();
    4.  
    5.         float itemHeight = containerRect.rect.height;
    6.         float ratio = itemHeight / itemRect.rect.height;
    7.         float itemWidth = itemRect.rect.width * ratio;
    8.  
    9.  GameObject newItem = Instantiate (item) as GameObject;
    10.             Image image = newItem.GetComponentInChildren <Image> ();
    11.             Texture2D preview = AssetPreview.GetMiniThumbnail (newItem);
    12.  
    13.             Rect rect = new Rect (0, 0, itemWidth, itemHeight);
    14.             image.sprite = Sprite.Create (preview, rect, new Vector2 (0f, 0f), 125);
    15.  
    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?
     
  2. drdenner

    drdenner

    Joined:
    Jun 24, 2014
    Posts:
    32
    Hmm doesnt look like it change the source image at all
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Which line is the console indicating is the problem?
     
  4. drdenner

    drdenner

    Joined:
    Jun 24, 2014
    Posts:
    32
    The error is with this line:
    Code (CSharp):
    1.  image.sprite = Sprite.Create (preview, rect, new Vector2 (0f, 0f), 125);
     
  5. drdenner

    drdenner

    Joined:
    Jun 24, 2014
    Posts:
    32
    This fixed it:
    Code (CSharp):
    1.     Rect rect = new Rect (0, 0, 64, 64);
    2. 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
     
  6. drdenner

    drdenner

    Joined:
    Jun 24, 2014
    Posts:
    32
    I have tried another way, but still no luck

    Code (CSharp):
    1. public GameObject prefab;
    2.     void Start ()
    3.     {
    4.         Texture2D preview = AssetPreview.GetMiniThumbnail (prefab);
    5.         RawImage image = GetComponent<RawImage> ();
    6.         image.texture = preview;
    7.     }
     
  7. RediceZone

    RediceZone

    Joined:
    Oct 1, 2014
    Posts:
    2
    @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
     
    Kunhi likes this.
  8. LucasBrum

    LucasBrum

    Joined:
    Oct 22, 2019
    Posts:
    7
    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?

    https://prnt.sc/26lj4vz Captura de tela 2022-01-29 160040.png
     
  9. valerypopov

    valerypopov

    Joined:
    Mar 27, 2020
    Posts:
    3
    In Build Settings set Max Texture Size - No override and click Apply button