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

Use image from atlas in UI

Discussion in 'UGUI & TextMesh Pro' started by BusyRobot, Apr 20, 2015.

  1. BusyRobot

    BusyRobot

    Joined:
    Feb 22, 2013
    Posts:
    148
    I want to be able to use any frame from an atlas, in a UI object, and then switch to a specific frame during run-time, how can I do that?
     
  2. qq83833224

    qq83833224

    Joined:
    Jan 15, 2015
    Posts:
    4
    the only way is you make image into prefab which has a Image component, load it as gameobject and attach it at runtime
     
  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,332
    You first need some way of storing and accessing these frames. For example you could load them from Resources, or have some type of script that stores an array of them. Then you need to have a script access your Image (or whatever component), and replace the sprite with the desired one.

    The easiest way is to make a new script and attach it to your UI element GO, and have this script store a reference to your element (e.g. Image), and store an array of Sprites, then at runtime you can make this script set yourImage.sprite to whichever sprite in this array you want.