Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Load PNG Into Sprite & Then Draw Sprite Onto Screen?

Discussion in 'Editor & General Support' started by JeZxLee, Sep 27, 2016.

  1. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Hi,

    How would we load a PNG into a sprite and then draw the sprite onto the screen?
    A simple C# code example would be appreciated, thanks!
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Are you talking about a sprite for the UI or for something like a 2D environment?
    Unity has some awesome video tutorials that get you started on both areas.

    About sprites (for 2D suff)
    https://unity3d.com/learn/tutorials/topics/2d-game-creation/sprite-type?playlist=17093
    and rendering the sprite on the screen.
    https://unity3d.com/learn/tutorials/topics/2d-game-creation/sprite-renderer?playlist=17093

    and using a sprite in the ui
    https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-image?playlist=17111
     
  3. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Hi,

    We have looked through the API documentation
    but don't see a function that would load a PNG into a sprite
    and another function to display sprite on the screen?
    (we want to do everything in code)

    Can someone give us a URL to the above in the API documentation?
    Thanks!
     
  4. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    If you're using Unity's native sprite system and not an external 2d library or framework then you don't really get clean code-based api access to the spritesheets. You'll have to do some plumbing with the 2d pipeline and spritesheet metadata that Unity creates for you during builds and the 2d materials if you want to mess with sprite references like that. Instead, to easily reference sprites from spritesheets just make Sprite variables in your behaviours and drop sprite references onto them.

    You can load textures at runtime;
    https://docs.unity3d.com/ScriptReference/Texture2D.LoadImage.html
    https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html

    You can pack textures into spritesheets;
    https://docs.unity3d.com/ScriptReference/Texture2D.PackTextures.html

    You can blit graphics onto the screen using the low-level graphics apis like;
    https://docs.unity3d.com/ScriptReference/Graphics.Blit.html

    You should take a look at other code-based sprite systems if you want to see how they approach things. Check out something like Futile: https://github.com/MattRix/Futile


    From this and a few of your other questions, you should probably re-read the official Unity manual and scripting reference, go through the various tutorials, and get a good c# reference to help you out with the language. At the moment you seem to be approaching Unity at a very low-level, wanting to control framerates, load raw sprite data, have a single 'main' entry point, and done all in code.

    Sure, you can sort of work like this if you set up some systems and wrappers and it might work fine for you, but I'd recommend first learning and using Unity the way they encourage you to use so you know what you're dealing with. As others have mentioned in some other replies to you, Unity uses a component-based architecture and has a very modular and editor-based approach to building scenes and adding logic. Your approach is immediately going against this intended workflow. If you want to go against it (and no one's stopping you!) then learn a bit about how it all works first :)
     
    shaktip2014 and ReDarkTechnology like this.
  5. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,171
    Are you wanting to load the sprite at runtime for some reason? Or are you just wanting to use a sprite?

    If you are just wanting to draw a sprite on the screen (and have it move around), do the following:

    With a new project:

    1. drag your PNG file into the assets folder.
    2. select it, and in the inspector, change the texture type to "Sprite"
    3. right click in the scene hierarchy, and choose UI/Image. This will create a Canvas, an EventSystem and an Image (under the Canvas).
    4. Click on the image in the Hierarchy. On the "Image" script, rename it to "MySprite" or something like that. On the image component, assign the sprite you created by dragging it from the project window and dropping it on the field that has the "Source Image" label.

    if your scene view is not in "2d" mode, press 2d on the scene toolbar, and then zoom out to see your sprite.
    you can reuse this by making it a prefab.

    If you are wanting to dynamically load the sprite, then usually you create a "Resources" folder, and place your PNG in that folder. Be sure to set the type as Sprite. Then you can load the sprite using Resources.Load<Sprite>("PngFileNameWithNoExtension");
     
    project_imp, Moyojs, ThaiRoi and 21 others like this.
  6. ajump10

    ajump10

    Joined:
    Apr 26, 2019
    Posts:
    2
    Thank you so much.
     
    haashika_shah and Steve-3d like this.
  7. Fairyessfeli

    Fairyessfeli

    Joined:
    Aug 30, 2020
    Posts:
    6
    2. select it, and in the inspector, change the texture type to "Sprite"

    I wouldn't have clicked on it if it hadn't been for you... would be wondering why my png's just don't import!
     
    project_imp, Jaimi and Steve-3d like this.
  8. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    45
    I don't think this works for if the image itself isn't downloaded until runtime?