Author Topic: UI2DSprite  (Read 56283 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
UI2DSprite
« on: November 21, 2013, 03:46:51 PM »
Overview

2D Sprite is a widget that's capable of drawing Unity 4.3 sprites. It inherits all the functionality of the UIWidget, and adds a visible component -- a sprite texture -- that gets stretched across its dimensions.

This component is only available when using Unity 4.3 or higher.



  • You can specify a 2D Sprite that will be drawn. A default material will be created for you when you do. If you don't specify a sprite, the drawn area will be white.
  • If you wish, you can specify a Material of your own. Even with the material specified, the sprite's texture will be used instead of the material's main texture.
  • If you don't want to make a material, you can also just specify a Shader instead. A default shader will be automatically chosen for you as soon as the 2D sprite gets created.
  • When using Unity 4.5 or higher, the sprite will get 9-sliced according to the import settings.

Choosing the "Make Pixel-Perfect" option from the context menu will make the UI2DSprite assume its original size. So if the source sprite happens to be 300 by 200 pixels, the width and height of your UI2DSprite will be adjusted to exactly those dimensions.

Pro-Tip

You don't need to create atlases beforehand when using 2D Sprites. Atlases will be generated for by by Unity, provided you've specified a Packing Tag in the texture's import settings.

Class Documentation

http://tasharen.com/ngui/docs/class_u_i2_d_sprite.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.

arumons

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Hallo, I updated NGUI to 3.0.6. And then tried to use Unity 2D sprite with Unity 4.3 sprite.
This Sprite Mode of Sprite is ‘Single’ and Packing Tag name is ‘foo’.
Finally sprite packed with ‘Window(above menu)’ >> ‘Sprite Packer’ >>  ‘Pack’.
This sprite is shown in scene view.
But When I Run Program, Error Message below is shown in console and not 2D sprite displayed…

———————————————————————————————————————————————
UnityException: Sprite is not rectangle-packed. TextureRect is invalid.
UI2DSprite.get_uvRect () (at Assets/NGUI/Scripts/UI/UI2DSprite.cs:187)
UI2DSprite.OnFill (.BetterList`1 verts, .BetterList`1 uvs, .BetterList`1 cols) (at Assets/NGUI/Scripts/UI/UI2DSprite.cs:232)
UIWidget.UpdateGeometry (Boolean forceVisible) (at Assets/NGUI/Scripts/Internal/UIWidget.cs:950)
UIPanel.UpdateWidgets () (at Assets/NGUI/Scripts/UI/UIPanel.cs:855)
UIPanel.LateUpdate () (at Assets/NGUI/Scripts/UI/UIPanel.cs:730)
——————————————————————————————————————————————-

I can use sprite whose sprite mode is Multiple or also use single sprite which is not packed.
But cannot use sprite which is packed with sprite packer..

Can I use packed sprite on NGUI?

Unity Version: 4.3.0f4

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite
« Reply #2 on: November 26, 2013, 06:25:15 AM »
Yup the UI sprites must be rectangle-packed. I guess the rect-packing was not added to 4.3 yet... Ah well. Unity 4.5 supports it, as well as sliced sprites, so it's just a matter of time.

arumons

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UI2DSprite
« Reply #3 on: November 26, 2013, 06:44:56 AM »
I see. I use NGUI Sprite and NGUI Atlas untill unity 4.5 coming!
thank you for reply.

dominus85

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 31
    • View Profile
Re: UI2DSprite
« Reply #4 on: December 03, 2013, 04:01:07 AM »
Would you recommend using the UI2dSprite or the UISprite for a new project?

Does the UI2dSprite update the panel draw calls correctly based on the unity generated atlases?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite
« Reply #5 on: December 03, 2013, 10:39:02 AM »
Stick to UISprites for now. UI2DSprites won't be very viable until 4.5 comes out.

Chris_E

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 37
    • View Profile
Re: UI2DSprite
« Reply #6 on: January 06, 2014, 02:06:09 PM »
When you place a regular sprite into the scene that contains trimmed transparent space the sprite is created maintaining that empty space.  When using the new UI2DSprite the empty space is cut off.  This is especially a problem when trying to use an animation since the UI2DSprite is being deformed as the animation plays because the frames are no longer the same size.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite
« Reply #7 on: January 06, 2014, 06:36:54 PM »
That's because Unity doesn't expose the necessary information needed to restore the trimmed space. It's one of the reasons why I suggest holding off on using 2D sprites until Unity 4.5.

dominus85

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 31
    • View Profile
Re: UI2DSprite
« Reply #8 on: January 18, 2014, 02:16:15 PM »
I see you talk a lot about 4.5.. Is it weeks away, months away or years away ? :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite
« Reply #9 on: January 18, 2014, 10:28:52 PM »
You'd have to ask Unity. :P

imansueto

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: UI2DSprite
« Reply #10 on: March 27, 2014, 04:14:57 PM »
Hello, would it be possible to have an option to use the pivot defined in Unity's Sprite Editor for UI2DSprites? In order to make it work I had to modify the offset used in drawingDimensions to this:

  1. Vector2 offset = new Vector2((sprite2D.bounds.extents.x - sprite2D.bounds.center.x) / sprite2D.bounds.size.x, (sprite2D.bounds.extents.y - sprite2D.bounds.center.y) / sprite2D.bounds.size.y);
  2.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite
« Reply #11 on: March 28, 2014, 01:11:02 AM »
Once Unity 4.5 hits RC1, I will add functionality to UI2DSprite that uses the functionality that's currently missing in Unity 4.3. Until then I won't be changing it as it's rather futile. Too much functionality is simply missing right now.

newDev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UI2DSprite
« Reply #12 on: April 13, 2014, 04:58:11 PM »
Hi! Its said that atlas would be automaticaly created, can you explain when and how does it happen, please? Does it mean that all the sprites marked with some unique Packing tag will be collected together in the build? How would it affect the game performance comparing with the usage of UIAtlases?

And one more question. In my game there is a necessity to use two complects of graphics - SD and HD, everything is clear in case of UIAtlases. What about graphics for UI2DSprites? Is there any way to set some kind of reference for it?

Thank you in advance!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite
« Reply #13 on: April 14, 2014, 03:45:23 AM »
Yes, that's how Unity 4.3+ works. I'd suggest consulting Unity's documentation here.

Just keep in mind that this particular feature (auto-atlas generation) is most likely going to break your NGUI's UI2DSprites because certain important functionality needed to draw them is missing in Unity 4.3 and requires Unity 4.6 which is not out yet.

I advise waiting for 4.6.
Quote
In my game there is a necessity to use two complects of graphics - SD and HD, everything is clear in case of UIAtlases. What about graphics for UI2DSprites? Is there any way to set some kind of reference for it?
Not to my knowledge. Unity did not consider this when writing their 2D system.

Fliperamma

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 13
    • View Profile
    • Fliperamma | Game On!
Re: UI2DSprite
« Reply #14 on: June 03, 2014, 07:39:26 PM »
Hi, NGUI newbie here  ;)
I'm having trouble fixing this pixelated aspect on a card game. The card on the left seems OK but I'm using NGUI Atlas on that one, the one on the right is using UI2DSprite.
Can you guys help me to get a smoother look using UI2DSprite, please?

The cards are rotated by 4 degrees, they are not scaled. I'm on Unity PRO 4.5 with NGUI 3.6.2