Discussion in 'Assets and Asset Store' started by ArenMook, Dec 8, 2011.
@ArenMook, will you consider to add this fix ?
Ideally the border scale parameter would need to be specified on the atlas rather than on the sprite. It's also not the best solution... I'm thinking of having some sort of a parameter on the UIRoot that would address this instead, but I haven't quite figured out the best way just yet. I'm letting it sit at the back of my mind while I'm working on other things.
Can anyone advise me on how to stop sprites from automatically scaling when I press play?
I am trying to use NGUI for simple in-game sprites(as well as UI) rather than integrate a second additional 2d solution. I want the sprites to approximate real-world sizes rather than pixel sizes, so I'd like the size of the sprite in the game view to be consistent with how I set it up in the scene view.
I tried adding my sprite underneath a Panel (no UI Root or Camera) and setting all transform scales manually, but I when I press play it jumps up to "pixel perfect" size. I want it to stay the size I make it in the scene view.
Additionally, I read that moving the sprite widget itself causes an expensive mesh rebuild; is this also the case if I give the widget a parent gameobject and move that instead?
Moving the parent is not as expensive, but still much more expensive than moving a panel. Sprites shouldn't resize to pixel-perfect coordinates on Play, and as far as I can tell they don't. I use a stretched UISprite for a background in more than one example (for example, UISprite - Backdrop in Example 2). Hitting Play doesn't resize it. I don't think they ever did... what version of NGUI do you have?
It is version 1.83.
I guess I must be doing it wrong - the Sprite was set to 128 scale in XY when NGUI created it, and if I change that it gets set back to 128 when I press play. Changing the parent gameobject works though, so I think I am not supposed to be scaling the sprite Widget directly?
I am looking to have hundreds of sprites (bullets, simple animated units) moving individually so I think a panel for each one is not ideal. At the moment my prototype is just using mesh quads and lots of drawcalls.
The only reason it would snap back to its original size would be if you had a UISpriteAnimation script attached. Is that the case?
Ah yes I do
Is there anything I can do to that animation script to stop that behaviour?
Or do you think it is not worth me trying to use NGUI for this kind of thing performance-wise, since I am moving quite a lot of sprites around? All I am really trying to do is leverage the existing atlas and sprite tools so I don't have to do it all manually myself by mapping quads.
ex2D is only $15 at the moment, I could just get that for the in-game stuff; I just wanted to avoid another atlas/sprite system if possible and thought I'd try NGUI first since I already use it for UI.
Do you need the sprite to be animated like that? If you want something simple like changing a color or growing in size, tweens are a better way to go here. If you need something more complex, then you can use an image button. UISpriteAnimation is mainly for playing out animations of many sprites (for example sparkle effects over a button). You can still scale it by scaling the parent. Anything more complicated, such as skeletal animation for a character, and I suggest creating it in a modelling package.
Hello, I have just purchased this UI Tool and it is very impressive.
I have been going through some of the basic documentation, but I have not found anything dealing with including building NGUI's entities into an existing project, using existing camera's, etc..
Is there any specific documentation on this?
Edit: For anyone else, this is explained in some previous posts ( a little outdated, given the Widget Tools though ) :
How would I go about detecting when UIDragObject (or Camera) is released from dragging?
It's pretty much exactly the same. You will use the Create UI tool to create your UI, making sure that the game's camera doesn't see the UI layer, and that the UI camera only sees the UI layer. You then have an option to create a UI right there, or create it in another scene and just save it as a prefab that will be bright into the main game.
I personally just use the former approach. My UI is generally created off to the side somewhere (or above the rest of the game, with the Y of 1000 or so). So when working there I just switch the camera to ortho, navigate to the UI, and modify what I need.
Whatever is being dragged will receive OnPress(false).
I see. Thanks.
Last few revisions, since I haven't updated the thread in a while...
- NEW: Added Example 12: Better Scroll View.
- NEW: Added a script that can be used to efficiently drag the contents of the panel: UIDragPanelContents.
- NEW: Added a function replacement for SetActiveRecursively (NGUITools.SetActive), since the former has rare odd issues.
- FIX: Changed the way the font data is stored, resulting in potentially better loading performance on mobile devices.
- FIX: UIPanel.Start() should now find cameras faster.
- FIX: UIPanel will no longer use the clipping softness value unless soft clipping is actually used.
- FIX: The way click / drag was handled has been changed a bit. It should now be easier to click buttons on retina screens.
- FIX: Rebuilding an atlas was not updating fonts correctly.
- FIX: Couple of tweaks to UIAtlas and UIFont's replacement feature.
- NEW: Added a simple script that can save the state of the checkbox (or a group of checkboxes) to player prefs.
- FIX: A variety of minor tweaks.
What is special about UIDragPanelContents?
Instead of moving the widgets, it moves the panel (which is very efficient) and adjusts the clipping rect in the opposite direction, thus creating the effect of moving the contents, when in fact they lie still. Since the contents lie still, the buffers don't get rebuilt, which results in much better performance when scrolling through a long list on mobile devices.
Can this be used with UIGrid?
UIGrid can be a child of the panel, sure. That's how example 12 is set up. I just took example 7 and changed it to use the new script instead.
Maybe it is just me but I am getting this when updating the package:
Assets/Editor/Editor/NGUIJson.cs(60,57): error CS1061: Type `UIAtlas' does not contain a definition for `spriteList' and no extension method `spriteList' of type `UIAtlas' could be found (are you missing a using directive or an assembly reference?)
Sounds like something didn't get updated correctly for you. File -> New Scene, delete NGUI, then import from the updated package.
Since I cannot check it now, I must ask: the momentum and spring properites are still working with this new feature, I assume.
Btw, performance-wise on a mobile device, sorted in fastest to slowest order:
1. UIDragCamera (2D only, can't be moved off-screen)
So if you're using the camera approach, keep it.
Yes, the momentum and spring all work as expected.
Yes, this is very useful info. I am concerned because of the mobile performance. As of now I use the DragObject, but then I will switch to the Panel solution. Re-do it for camera use is too much work, but who knows, if I can get 10 fps more, I might consider...
Ah then yes, you will get a noticeable performance boost by switching to UIDragPanelContents.
Since you are there, what is the proper way to scale a UI based on device screen properties(size and dpi)?
Depends on what you want to do with it. If you want pixel-perfect results, keep UIRoot set to 'automatic'. If you want predictable results, turn off the automatic option and set the target resolution. The UI will scale as the resolution changes, staying the same size relative to the height of the screen. In the next version I'm also going to add something similar to a "dpi" setting to the UIRoot, as some guys (yuewah) need it for what they're trying to achieve.
Btw, if anyone knows what "bugs" the last two asset store reviewers were talking about, I'd love to know so i can fix them. It's so disappointing when people don't even try to seek assistance... :|
Well, I'm going to get some sleep.
I'm seeing the following problem with the uipopuplists that I create. The list items and the hilite for the items are working correctly, but the underlying slicedsprite for the background of the dropdown list seems to be off:
Not sure what could be causing it... The same slicedsprite is being used for everything. It's like the box starts halfway down the first item.
Having some issues setting alpha values of UISlicedSprite through script.
foreach( GameObject go in obs )
go.GetComponentInChildren<UISlicedSprite>().color = new Color( 255, 255, 255, 100);
go.GetComponentInChildren<UILabel>().text = "";
Basically I have a co-routine which goes through an array of game objects and resets the label text and colour in the child objects. It works perfectly with the exception of the alpha value. It seems it can only ever be 255 or 0 and nothing in between. It also works perfectly if I do something like color = Color.red; or even new Color(255, 40, 205, 255); or something similar.
@Jaimi: Does that sprite have padding?
@CJO: Color is specified in 0-1 range, not 0-255.
I see. Ty. The values in the editor are misleading.
It does have a 1 and 2 pixel border padding - strangely enough. Not sure why that cuases it to be off so far. But I have added new sprites with no padding, and it does indeed work right now. Thanks!
This one's for @yuewah:
- NEW: UIAtlas now has a "pixel size" property that affects MakePixelPerfect logic as well as sliced sprite's border size.
- FIX: UISprite will now always ensure it has a sprite to work with, if at all possible.
- FIX: UIDragPanelContents should now work correctly on mobile devices.
Great man, ArenMook. Just tested with 1.86, Sliced sprite border size fixed, but Tiled sprite is not fixed.
Can't "fix" a new feature, yuewah. I simply didn't change the tiled sprite. You can fix it locally by opening UITiledSprite, navigating to line 53, and taking pixelSize into account like so:
float pixelSize = atlas.pixelSize;
float width = Mathf.Abs(rect.width / scale.x) * pixelSize;
float height = Mathf.Abs(rect.height / scale.y) * pixelSize;
Thank a lot.
No worries. I've also uploaded 1.86b with this tweak.
Btw, the prefab is instantiated in runtime, the sprite cannot change to HD, can you add a option that allow to Find the prefab instead of the active scene objects only
static public T FindActive<T> () where T : Component
//return GameObject.FindSceneObjectsOfType(typeof(T)) as T;
return GameObject.FindObjectsOfTypeIncludingAssets(typeof(T)) as T;
The sprites are referenced by names, so there is no need to change prefabs. Assuming you have them use the reference atlas, when this sprite is brought into the scene it will automatically pull from whatever atlas it's pointing to.
@ArenMook, that's brilliant. +1 on the pixel size solution
The prefab is in the Asset Folder, not in the Scene View.
I understand that, yuewah... but nothing should be referencing your final HD / SD atlases. All widgets should be referencing the "main" atlas. This way when the main atlas's "reference" value gets changed, all widgets using it will update automatically, regardless of where they are. Only active scene objects need to know about this change because they need to be notified so they can re-draw themselves. Widgets that are not in the scene or are inactive don't need to be notified.
If you are having some issue with this, can you post some more info with what's going wrong and where?
Before that, I find another bug about the prefab. Based on the tutorial 2, I make the one of the UISprite to be prefab in Asset Folder, then call NGUITools.AddChild( panel, prefab ); In 1.86c, it raise the following exception, but 1.86 works without problem.
I can confirm that there is a regression change there. I'll fix it, thanks for reporting it!
1.86d fixes that. Thanks, yuewah.
Thx a lot. Another problem, When using NGUITools.AddChild( panel, prefab ), the new UISprite (Clone) local positon, scale and rotation is not the same as UISprite prefab. I think it is better to have a option to allow keep them the same or reset. What do you think ?
That's intentional. NGUITools.AddChild does that. You can instantiate the prefab yourself using GameObject.Instantiate if you don't want this behavior.
Hi ArenMook just downloaded ngui 1.86d2 !
I have a small problem when i add a button the collider seems to be translated on x to 59.5 and on y -15 and are slightliy oversized so i manually have to set it to x=0 and y=0 and the size correctly.
i tried changing the handle mode to pivot or center and add another button to test but that did not see to affect it... i also tried the global and local coordinates but i still see the same thing
When i manually add a collider then everything is ok
Could you please let me know of a quick fix so i dont have to manually add colliders to each button ?