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. Dismiss Notice

How to create a Sprite based on the Sprite name ?

Discussion in '2D' started by bali33, Aug 27, 2014.

  1. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Hi guys,

    I want to create empty GameObject, add a SpriteRenderer component into it and then be able to set the Sprite property of the SpriteRenderer component. I have the name of the Sprite that I want to create (the name which is display in the Assets folder) - is there a way to create/instantiate a Sprite with that information ?

    Thank you.
     
  2. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    I think you shall create a prefab as per your requirement and then Instantiate that prefab whenever you want. Alternatively if you want to completely make it dynamic then since you have the name of the sprite, you can load it from Resources.Load method and assign it to the sprite property of the SpriteRenderer.
     
  3. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Does Resources.Load allow to load texture which is actually a subTexture from a Sprite packed as Multiple ?
     
  4. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    You can also make a public Sprite variable in your script and assign the sprite (a subTexture from a Sprite packed as Multiple) to it. Or even a list of Sprite's.
     
  5. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    The thing is that I have tons of Sprite (sub-texture) - it would take me so long to drag-&-drop each one into a script.
    Is there a way to directly use the Sprite which is "multipack" into a script ? What type of object it is ?
     
  6. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
  7. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Thank you
     
  8. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    164
    That's impossible as once you try to select several files, the IDE loses focus on object and you cannot drag and drop. Resources does not work either... just for small projects. Steaming resources also s@ck because there are some universal cases (thanks to google) when android fails and you will end up with 2% crashes which you are unable to resolve... So the only way to do it, just add all sprites via properties but doing that for hundreds of sprites is wrong path for sure... if anyone knows better solution please post. Thanks