-2

Unity NGUI system uses ScrollRects to add scrollable elements to your game, but this scrollrect lacks the ability to restrict scrolling up past a certain point, something I determined to implement.

I had an idea: lets just check if ScrollRect.verticalNormalizedPosition is 1, and if it is, set it to 1 (1 being the top part of the ScrollRect.)

public class scrollrectclamper: MonoBehaviour {
    ScrollRect SR;
    void Start () {
        SR= gameObject.GetComponent<ScrollRect> ();
    }
    void Update(){
        if (SR.verticalNormalizedPosition == 1) {
            SR.verticalNormalizedPosition = 1; // WHY
        }
    }
}

This restricts scrolling even though I wouldn't even expect == 1 to work with a float, a type infamous for its imprecision.

The description of verticalNormalizedPosition says:

The vertical scroll position as a value between 0 and 1, with 0 being at the bottom.

But the function is broken only ever returns floats 1.0 if the scrollrect is at the top or above, and 0.0 if it's anywhere else.

31eee384
  • 2,748
  • 2
  • 18
  • 27
Zan
  • 21
  • 2
  • 3
    `==1` is probably causing an implicit cast to an integer. – Ron Beyer Sep 21 '15 at 21:04
  • 5
    How about posting a few lines of codes that we can use to reproduce your problem, instead of posting a long story? – Eser Sep 21 '15 at 21:06
  • Help us help you. Consider showing us the code snippet(s) you had and the result(s) you got, in clearly separated sections, instead of describing it in natural language. – Theodoros Chatzigiannakis Sep 21 '15 at 21:09
  • Is it possible that `verticalNormalizedPosition` is a property with something like `get { return Mathf.Clamp(verticalUnclampedPosition, 0, 1); }`, and with a `set { verticalUnclampedPosition = value; }`? That could explain the behaviour. I feel I may have misunderstood, sorry if this sounds silly. '~' – Serlite Sep 21 '15 at 21:14
  • What looks like a useless assignment may well trigger a setter with all sorts of side-effects or syn'ching. – TaW Sep 21 '15 at 21:52
  • 2
    @Zan I've edited your post to make it shorter and add a descriptive title. Please look it over and edit it again if I missed something, thanks. – 31eee384 Sep 21 '15 at 22:07

1 Answers1

0

Because the set clause of veritcalNormalizedPosition fires a method which actively changes the position of the scroll rect. As long as you dont mess with the value it will be clamped between 0 and 1 and get'ting the variable wont trigger any behaviors. But as soon as you set the variable, you are telling the NGUI engine, "i want this to be here." thus, a method is fired.

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37