Tasharen Entertainment Forum
Support => NGUI 3 Documentation => Topic started 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)
- 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.
-
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
-
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.
-
I see. I use NGUI Sprite and NGUI Atlas untill unity 4.5 coming!
thank you for reply.
-
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?
-
Stick to UISprites for now. UI2DSprites won't be very viable until 4.5 comes out.
-
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.
-
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.
-
I see you talk a lot about 4.5.. Is it weeks away, months away or years away ? :)
-
You'd have to ask Unity. :P
-
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:
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);
-
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.
-
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!
-
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.
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.
-
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)
-
The only way to make it smooth is to either use Anti-aliasing, or to give its texture a transparent border.
-
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. ;)
-
Hi ArenMook
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
-
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.
-
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.
-
I can drag NGUI Sprite to the "Sprite" groove!And I want to know which style Sprite can use on it!
-
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.
-
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.
-
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?
-
You can change a texture's filtering method, can you not?
-
Is that possible at runtime?
-
Texture2D.filterMode
-
Thank you for answering. I know you aren't my personal unity api but I thought it was a ngui problem.
Sorry
-
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
-
Hmm? All widgets have an aspect ratio field you can set. Sprites are widgets.
-
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).
-
If doing it via code, and setting the width, knowing the desired aspect ratio, calculating height is trivial.
-
Hi,
I'm new for Unity & NGUI, a fast question: how can I change Unity 2D Sprite at runtime?
Thanks,
-
UI2DSprite.sprite2D = somethingElse;
-
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!
-
Check what material/shader is used by your UI2DSprites. You should be using the Unlit/Transparent Colored shader.
-
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?
-
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.
-
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!
-
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.