Tasharen Entertainment Forum

Support => NGUI 3 Documentation => Topic started by: ArenMook on November 21, 2013, 03:46:51 PM

Title: UI2DSprite
Post by: ArenMook 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 (http://www.tasharen.com/forum/index.php?topic=6702), 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.

(http://www.tasharen.com/ngui/ui2dsprite.jpg)


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.
Title: I can’t use Unity 2D Sprite with Unity 4.3 sprite(packed by sprite packer(Develo
Post by: arumons on November 26, 2013, 06:15:12 AM
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
Title: Re: UI2DSprite
Post by: ArenMook 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.
Title: Re: UI2DSprite
Post by: arumons 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.
Title: Re: UI2DSprite
Post by: dominus85 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?
Title: Re: UI2DSprite
Post by: ArenMook on December 03, 2013, 10:39:02 AM
Stick to UISprites for now. UI2DSprites won't be very viable until 4.5 comes out.
Title: Re: UI2DSprite
Post by: Chris_E 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.
Title: Re: UI2DSprite
Post by: ArenMook 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.
Title: Re: UI2DSprite
Post by: dominus85 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 ? :)
Title: Re: UI2DSprite
Post by: ArenMook on January 18, 2014, 10:28:52 PM
You'd have to ask Unity. :P
Title: Re: UI2DSprite
Post by: imansueto 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.  
Title: Re: UI2DSprite
Post by: ArenMook 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.
Title: Re: UI2DSprite
Post by: newDev 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!
Title: Re: UI2DSprite
Post by: ArenMook 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.
Title: Re: UI2DSprite
Post by: Fliperamma 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

(http://fliperamma.com/downloads/pixelated.jpg)
Title: Re: UI2DSprite
Post by: ArenMook on June 04, 2014, 02:09:13 AM
The only way to make it smooth is to either use Anti-aliasing, or to give its texture a transparent border.
Title: Re: UI2DSprite
Post by: Fliperamma on June 04, 2014, 08:24:24 AM
The only way to make it smooth is to either use Anti-aliasing, or to give its texture a transparent border.
Thank you, that did the trick! I used a 2px margin in my PNGs and they look a lot better now.  ;)
Title: Re: UI2DSprite
Post by: DEngland on August 13, 2014, 07:02:55 PM
Hi ArenMook

Quote
When using Unity 4.5 or higher, the sprite will get 9-sliced according to the import settings.

I am very interested in figuring out how to set the 9-slice parameters in the import settings for UI2DSprites.
I'm using Unity 4.5.2f1 and NGUI v3.6.8 but after digging around in the import settings I haven't found those options. Am I looking in the wrong place, or has this functionality been pushed back?

Thanks
Title: Re: UI2DSprite
Post by: ArenMook on August 14, 2014, 08:14:24 AM
Unity pushed that feature back until 4.6 (uGUI release).

I got tired of waiting for it, which is why I added a way to specify it on the UI2DSprite itself.
Title: Re: UI2DSprite
Post by: DEngland on August 14, 2014, 03:01:04 PM
Unity pushed that feature back until 4.6 (uGUI release).

I got tired of waiting for it, which is why I added a way to specify it on the UI2DSprite itself.

Thanks for the info ArenMook; I'll be looking forward to 4.6 then. In the meantime it's going to be a mess to programmatically set various images (with different slicing) into the same UI2DSprite.
Title: Re: UI2DSprite
Post by: lijiangood@yeah.net on August 27, 2014, 09:10:20 PM
I can drag NGUI Sprite to the "Sprite"  groove!And I want to know which style Sprite can use on it!
Title: Re: UI2DSprite
Post by: Zoidster on November 27, 2014, 07:40:01 AM
Hi,
I'm using a 80x80 sprite and I'm upscaling it to 300x300. The image now looks blurry. Is there a way to have the UI2DSprite do upscaling with a different algorithm?
I'd liketo use a nearest neighbor interpolation method.
Title: Re: UI2DSprite
Post by: ArenMook on November 27, 2014, 09:05:52 PM
When you enlarge a smaller texture, it will look blurry. That's how graphics work. You can change the texture to use Point Filtering, but that will just make everything look pixelated. That's your only option.
Title: Re: UI2DSprite
Post by: Zoidster on November 28, 2014, 02:40:33 AM
That I knew, obviously. But can I apply point filtering to a graphic I have at runtime?
Or to a sprite I get from a pre-packed sprite-atlas?
Title: Re: UI2DSprite
Post by: ArenMook on November 30, 2014, 05:54:24 AM
You can change a texture's filtering method, can you not?
Title: Re: UI2DSprite
Post by: Zoidster on December 01, 2014, 04:22:50 AM
Is that possible at runtime?
Title: Re: UI2DSprite
Post by: ArenMook on December 02, 2014, 12:33:09 PM
Texture2D.filterMode
Title: Re: UI2DSprite
Post by: Zoidster on December 03, 2014, 06:02:36 AM
Thank you for answering. I know you aren't my personal unity api but I thought it was a ngui problem.
Sorry
Title: Re: UI2DSprite
Post by: Benzino07 on February 16, 2015, 11:52:45 AM
Hi,

Just wondering is there a reason why you cannot set Fixed Aspect for this sprite via code? Is there any way to get this functionality without modifying NGUI code?

Thanks
Title: Re: UI2DSprite
Post by: ArenMook on February 17, 2015, 10:35:47 PM
Hmm? All widgets have an aspect ratio field you can set. Sprites are widgets.
Title: Re: UI2DSprite
Post by: Benzino07 on February 18, 2015, 04:39:16 AM
I understand that, however UI2DSprite has a handy checkbox in the inspector called Fixed Aspect Ratio, which seems to maintain the aspect ratio of the sprite after you adjust the size of it. Unless I'm missing something, there doesn't seem to be any easy way to maintain the aspect ratio of a sprite that is not anchored in code (well mFixedAspect does it, but it's hidden by default).
Title: Re: UI2DSprite
Post by: ArenMook on February 18, 2015, 05:49:05 PM
If doing it via code, and setting the width, knowing the desired aspect ratio, calculating height is trivial.
Title: Re: UI2DSprite
Post by: fenixn0909 on April 05, 2015, 07:38:51 PM
Hi,

I'm new for Unity & NGUI, a fast question: how can I change Unity 2D Sprite at runtime?


Thanks,
Title: Re: UI2DSprite
Post by: ArenMook on April 06, 2015, 08:15:53 AM
UI2DSprite.sprite2D = somethingElse;
Title: Re: UI2DSprite
Post by: enhilljr on September 17, 2015, 06:52:39 PM
Hello!

We're experiencing a clipping problem with UI2DSprite.

We have a file list viewer created with NGUI that uses a UIPanel/UIScrollView to which we add our own "FileIconTemplate" prefabs (1 per file) at runtime.  Each "FileIconTemplate" contains a couple of UISprite elements to create a nice background, a UILabel that contains the filename text, and a UI2DSprite that contains a graphic thumbnail of the contents of the file.  All of the previously mentioned elements are children of a UISprite with a UIDragScrollView.cs attached to it that serves as the root of the prefab.

When we put enough of our FileIconTemplate items in the ScrollView and scroll up and down (Movement=Vertical), all of the sub-elements of the template get clipped properly except the UI2DSprite elements.  The UI2DSprite elements appear in front of the surrounding UI components (outside the ScrollView) until they are moved completely outside the ScrollView volume and are culled.

Is there some sort of setting, switch or depth value that we might not be setting properly or is boundry clipping not possible with UI2DSprite elements?

We're using Unity 5.1.0f3 and NGUI 3.7.7

Any insight would be greatly appreciated!
Title: Re: UI2DSprite
Post by: ArenMook on September 20, 2015, 03:47:02 AM
Check what material/shader is used by your UI2DSprites. You should be using the Unlit/Transparent Colored shader.
Title: Re: UI2DSprite
Post by: Bradamante3D on October 22, 2015, 03:09:03 PM
Is there any benefit of using UI2DSprite over the standard NGUI sprite component? Better performance? Less draw calls? Automated atlas management?

How does using UI2DSprite everywhere work with a dynamic Unity font? I wouldn't require any NGUI atlas at all in this case, right?
Title: Re: UI2DSprite
Post by: ArenMook on October 24, 2015, 04:56:33 AM
Automatic atlas creation is the only benefit.

Dynamic fonts create a separate set of draw calls, often more than one unless your widget depth is set just-right.

You will get better performance and fewer draw calls by using bitmap fonts and NGUI sprites.
Title: Re: UI2DSprite
Post by: skatola on April 30, 2016, 12:44:58 PM
Hi!
i found the "fixed aspect" function in the ispector very useful for displaying images, just one question:
There's a way to recalculate the "fixed aspect" after the screen orientation is changed?
Cause fixed aspect work like a charm when i open the image, but if rotate my device obviously is not updated, thank you!
Title: Re: UI2DSprite
Post by: ArenMook on May 03, 2016, 01:49:56 PM
Fixed aspect? You can make the aspect ratio be based on width or height -- is this what you mean? This ratio is based on the sprite and has nothing to do with screen size. If you want it to be based on screen size, you will have to write some code to do that.