Author Topic: UIWidget  (Read 41862 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIWidget
« Reply #15 on: March 12, 2014, 06:11:06 PM »
You can still animate the scale like before, or choose to animate the width and height. TweenWidth / TweenHeight can do that for example.

Anomalous Underdog

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UIWidget
« Reply #16 on: April 03, 2014, 05:31:48 AM »
Let's say for example I anchor the left part of my widget, but I want the width to be locked to a constant value even if the widget's x position is changed. Is there a way to do that?

Currently my experience is that the right edge of the widget stays where it is on the screen, causing the widget's width to lengthen/shorten when its x position is changed.

Isamson

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 52
    • View Profile
Re: UIWidget
« Reply #17 on: April 03, 2014, 03:33:03 PM »
Within the Widget's Anchor setting, select advanced. This will allow you to snap your object sides to different objects. Since you want your right side to stay the same, drag the object(self) in the right anchor. Select the "Target's Left" and enter the size you want this object to be.
Unity 4.5.3f
NGUI 3.7.0

AbsurdInteractive

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: UIWidget
« Reply #18 on: April 04, 2014, 05:30:22 PM »
I had a question regarding Anchors on UIWidgets and UIPanels. The way my game structure is setup, I set all my cameras in the first scene, then I load the separate UI scenes in with LoadLevelAdditive.

I'd like to base all my NGUI Sprites and elements positioning anchors off of my Camera's 4 corners, but because the camera is set at runtime, and not in the UI scenes ahead of time. I was wondering what's the best way to setup the UI scenes so that they can be anchored correctly.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIWidget
« Reply #19 on: April 04, 2014, 10:54:15 PM »
Easiest way would be to anchor everything to a panel, not the camera. The panel would automatically assume screen's dimensions if it's not clipped.

Aithoneku

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIWidget
« Reply #20 on: April 09, 2014, 02:42:40 AM »
I've got two problems with UIWidget so I would like to ask for help:

1.) As it was mentioned above, UIStretch is currently deprecated so I would like to ask how to achieve following functionality with anchoring system (I guess I'm missing something and cannot figure it out) - stretch a sprite so it fills whole screen but at the same time it's keeping aspect ratio AND it's visible completely regardless on whether screen aspect ratio is greater or less then aspect ratio of the sprite. (So it makes horizontal or vertical bars of space in order to keep the ratio). Similarly, how to achieve same thing, but this time the image should be cropped (horizontally or vertically depending on whether current aspect ratio of screen is greater or less then ratio of the sprite)? My current solution is my own custom Stretch script, but I would like to know the "correct" way how to achieve this with anchors if they should replace old anchors and stretch.

2.) Both width and height properties of UIWidget contains condition "keepAspectRatio != AspectRatioSource.BasedOnHeight"
  1. // width property, line 169
  2. if (mWidth != value && keepAspectRatio != AspectRatioSource.BasedOnHeight)
  3.  
  4. // height property, line 216
  5. if (mHeight != value && keepAspectRatio != AspectRatioSource.BasedOnHeight)
  6.  
Is there reason for that or is it a bug? I noticed following behavior with this when I set Aspect Ratio of a sprite to BasedOnHeight:
  • When changing height of the sprite through Unity Editor, the blue point for the configuration is not keeping under mouse, but instead it's moving too fast in direction of mouse move making it extremely hard to configure.
  • When I layout the item through my own code (your layouts are insufficient for me) by configuring width and height properties, the values are ignored.
But when I change height property to
  1. if (mHeight != value && keepAspectRatio != AspectRatioSource.BasedOnWidth)
both works just fine.

Note: as this is my first post on this forum, I would like to thank you for your hard work with NGUI - it's really good GUI library, I guess best for Unity. I like it's both simplicity and ability to edit GUI in Unity Editor. It has it's bugs (for example when I use sprite with a lot of transparent space around and turn on "Trim Alpha" in atlas, the sprite is quite badly deformed in GUI screen when changing it's size), but still pretty much best solution I encountered.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIWidget
« Reply #21 on: April 09, 2014, 04:20:03 AM »
#2 tells me that your version of NGUI is not up to date.

#1 -- Anchor sprite's top to screen's top. Anchor sprite's bottom to screen's bottom. Don't anchor left or right. Set the sprite to have an aspect ratio of your choice, based on height. If you want it to be based on width instead, set left and right anchors instead.

Aithoneku

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIWidget
« Reply #22 on: April 09, 2014, 08:06:25 AM »
#2 Ah, sorry, I thought it is. (It's company's and I thought we're keeping it updated.) Sorry about that.

#1 Sorry, but no, that won't work. This would work if I would define my problem as "I want to display whole image and keep it's aspect ratio AND I know that aspect ratio of screen will be ALWAYS greater then AR of the image" or as "I want to display image stretched vertically and crop it OR add spaces on sides according current screen AR keeping AR of the image", but I defined it as "stretch a sprite so it fills whole screen but at the same time it's keeping aspect ratio AND it's visible completely regardless on whether screen aspect ratio is greater or less then aspect ratio of the sprite". Your solution wouldn't work if the AR of screen would be less then AR of image. Example: image is 16:9, but screen is 4:3 - left and right sides of image would be cropped. Using horizontal (left and right) anchors instead of vertical anchors won't help either - what if image is 4:3 and screen is 16:9? Image would be cropped above and under the screen.

(Note: if you're going to say something like "then use image which is so wide that it's AR is greater then highest AR you're ever expecting - it's not up to me to decide that. Also, screens are getting wider and wider - 5 years ago, I would expect 1280x800 to be widest - that would be silly now, wouldn't be...)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIWidget
« Reply #23 on: April 10, 2014, 08:10:35 AM »
Different aspect ratios mean something will be cropped, or padding will be visible, depending on how you do it. It's the only way to do it without skewing the image's aspect ratio. Either you anchor to top/bottom and get some padding on the sides, or you anchor left/right and get cropped top & bottom. If you want the image to always be 100% visible on the screen, implying that it gets smaller to always have all 4 sides visible, then you shouldn't be using anchors as it involves custom logic at this point. Write some code to reduce the size if it doesn't fit.

Aithoneku

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIWidget
« Reply #24 on: April 10, 2014, 09:41:29 AM »
I see, ok, so it's not possible, thanks for the info. I was just little confused, because I read somewhere that new anchor system is replacing functionality of UIStretch and UIAnchor - I wrongly thought it is meant that it replaces the functionality completely, not partially, sorry about that.

Just for the record, if you're interested, my solution is then using something similar to UIStretch. I'm aware, that it's deprecated, so I created my custom class which inspires by the UIStretch somehow.

artician

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: UIWidget
« Reply #25 on: August 23, 2014, 07:34:20 PM »
I'm using PlayMaker with NGUI, and I'm unable to figure out how to access the Anchor property on a UIWidget object.  PlayMaker has a Set Property function that exposes the properties in an object, but I can't find anything relating to UIAnchor anywhere.  Can anyone clear this up for me?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIWidget
« Reply #26 on: August 23, 2014, 08:16:54 PM »
UIRect.leftAnchor, UIRect.rightAnchor, etc. You likely do need to write something for PlayMaker to understand them though. I'm not too familiar with it.

serioustommy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UIWidget
« Reply #27 on: October 08, 2014, 12:19:22 PM »
I'm trying to replicate what I used to do in UIAnchor in UIWidget but it doesn't seem possible (at least not without some custom scripts).

Basically I'm just trying to anchor relative to the screen size like setting the "relative offset" values in UIAnchor. If we want to be able to anchor to the screen is the best practice now to create a dummy panel/widget and attach some custom script to resize it to fit the screen size?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIWidget
« Reply #28 on: October 08, 2014, 01:38:11 PM »
Instead of using fixed anchor points such as "Left", "Right", "Center" etc, use "Custom", then specify a relative value (generally in 0 to 1 range). Value of 0 means 'left' or 'bottom', depending on the axis. 0.5 is center, 1.0 is right/top. 0.25 means 25% from the left/bottom, etc.

It's all explained here: http://www.tasharen.com/forum/index.php?topic=7013.0

serioustommy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UIWidget
« Reply #29 on: October 09, 2014, 06:27:03 AM »
But a "target" is needed for that. What is the target when I want the target to be the whole screen? Do I need to create a dummy transparent panel that resizes to represent the whole screen?