Search Unity

Best way to creat a UI using a sprite sheet

Discussion in '2D' started by Trebor Zaid, Nov 26, 2013.

  1. Trebor Zaid

    Trebor Zaid

    Joined:
    Nov 21, 2013
    Posts:
    5
    I have a single sprite sheet with all the elements for my UI. I set the texture type to sprite and the sprite mode to multiple so that I can cut it up into the individual parts. The problem is that I can't create a GUITexture that references one of the sub-sprites. I can drag the base sprite to display the whole sprite sheet but not any of the individual sprites. Is there a way to do this? As an alternate solution I was thinking of creating sprite prefabs and then spawning them via a script and setting their parent to the camera, the problem is trying to do the layout this way is hard because the coordinates will differ based on the resolution of the device since its is not relative like when using GUITexture where for example 0 is left and 1 is right of the screen regardless of resolution. Any suggestions as to how best to implement this is appreciated. Obviously the last thing I could do is cut up the spritesheet into individual files, then I would be able to use GUITexture, but that would increase draw calls (I think, since non-pro doesn't combine individual sprites into an atlas).
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    If you cut up the sprites in the editor you will be able to drag them individually on to your scene from the project view. Just put the sprites on their own UI layer that's displayed above all others.
     
  3. Trebor Zaid

    Trebor Zaid

    Joined:
    Nov 21, 2013
    Posts:
    5
    Yeah I know you can do that, and in fact that's how I decided to currently do it, I had to use camera.ViewportToWorldPoint() along with transform.InverseTransformPoint() in order to get the local bounds within the camera's view and then use Mathf.Lerp() between the min/max values to get the functionality I wanted. Still it seems odd that there doesn't seem to be a way to easily use a subsprite in a GUITexture component or in the OnGUI() method.