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

Setting SpriteRenderer.sprite causes the sprite to dissapper.

Discussion in '2D' started by mrslunk, Oct 24, 2020.

  1. mrslunk

    mrslunk

    Joined:
    Sep 4, 2013
    Posts:
    2
    Hi All,

    Bit of a unity noob here. I'm trying to create a texture, then create a 2d sprite and assign it to a sprite renderer.

    What seems to be happening is that both the texture and sprite _seem_ to be correctly created and assigned to the renderer, but nothing appears on screen.
    Further, when i assign the sprite in the SpriteRenderer via the inspector (eg a stock triangle) i can see it the stock triangle just fine, but as soon as i hit play it's replaced (on screen) with nothing.

    I'm sure i'm missing something simple. Any help would be appreciated.

    I've attached the inspector view during execution and the camera settings.
    Here is the code.

    Cheers

    Code (CSharp):
    1.  
    2. public class ProceduarlSpriteRenderer : MonoBehaviour
    3. {
    4.     private SpriteRenderer m_SpriteRenderer;
    5.     public Sprite m_Sprite;
    6.     public Texture2D texture;
    7.  
    8.     void Awake()
    9.        {
    10.         texture = new Texture2D(size.x, size.y, TextureFormat.RGBA32, false);
    11.         Color32 red = new Color32(255, 0, 0, 255);
    12.         for (int x = 0; x < size.x; x++) {
    13.             for (int y = 0; y < size.y; y++) {
    14.                 texture.SetPixel(x, y, red);
    15.             }
    16.         }
    17.         texture.Apply();
    18.  
    19.         Rect rect = new Rect(0, 0, texture.width, texture.height);
    20.         Vector2 pivot = new Vector2(0.5f * texture.width, 0.5f * texture.height);
    21.  
    22.         m_Sprite = Sprite.Create(texture, rect, pivot);
    23.  
    24.  
    25.         m_SpriteRenderer = GetComponent<SpriteRenderer>();      
    26.         m_SpriteRenderer.sprite = m_Sprite;
    27.         }
    28.     }
     

    Attached Files:

  2. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    Your code is fine, and it works as expected, I think the issue is that you're creating a huge sprite (2048x2048) and you don't see anything because of that.

    I've Just tested the exact same code on my machine and it works as expected. If you want to scale your texture to say, 1 unity meter, use pixelsPerUnit and set it to 2048.

    Code (CSharp):
    1.  
    2. public class ProceduarlSpriteRenderer : MonoBehaviour
    3. {
    4.     private SpriteRenderer m_SpriteRenderer;
    5.     public Sprite m_Sprite;
    6.     public Texture2D texture;
    7.     public Vector2Int size;
    8.     public int i_PixelsPerUnit;
    9.  
    10.     void Awake()
    11.     {
    12.         texture = new Texture2D(size.x, size.y, TextureFormat.RGBA32, false);
    13.         Color32 red = new Color32(255, 0, 0, 255);
    14.         for (int x = 0; x < size.x; x++)
    15.         {
    16.             for (int y = 0; y < size.y; y++)
    17.             {
    18.                 texture.SetPixel(x, y, red);
    19.             }
    20.         }
    21.         texture.Apply();
    22.  
    23.         Rect rect = new Rect(0, 0, texture.width, texture.height);
    24.         Vector2 pivot = new Vector2(0.5f * texture.width, 0.5f * texture.height);
    25.  
    26.         m_Sprite = Sprite.Create(texture, rect, pivot, i_PixelsPerUnit);
    27.  
    28.  
    29.         m_SpriteRenderer = GetComponent<SpriteRenderer>();
    30.         m_SpriteRenderer.sprite = m_Sprite;
    31.     }
    32. }
    Try that, and set x,y and pixelsPerUnit to 1, to understand how it behaves

    Hope it helps, cheers!!
     
    mrslunk likes this.
  3. mrslunk

    mrslunk

    Joined:
    Sep 4, 2013
    Posts:
    2
    Thank you so much.

    So what was happening was that i misread how the pivot was supposed to be calculated; i.e. in percentages instead of pixels.

    The sprite was rendering, just way off-camera., and by following your suggestion i was able to see how it was behaving as i changed the texture size and notice that as the size increased, the sprite location moved in camera space.

    Thanks again!
     
    webox likes this.
  4. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    Awesome!! Glad it worked!