Ugui scrollrect sliding positioning optimization

Source: Internet
Author: User

Ugui Sliding Components Although the performance and Ngui ScrollView consistent, but it is more beautiful open source, do not understand the principle of the time to directly check the source is OK.
in the use of Scrollrect, want to slide smooth and positioning, both for the inertia of the scrolling settings, such as the figure Movementtype is specified as elastic is the sliding mode with inertia, while adjusting the inertia is the inertia coefficient, the higher the value, the greater the inertia performance, That is, the longer it takes for the slide to move freely to stop after the hands are gone. When inertia=0, it means no inertia, and when sliding, the hand is stopped by sliding.

But since we're going to use this component we want it to experience good when it comes to functionality, to exclude special needs, and we certainly don't want it to scroll down and stop when it stops. But if you check the source code of the Scrollrect or directly will slide in the scroll area coordinate changes to print out will find this coordinate refresh time is too long, with inertia of the rolling slide will be refreshed 2 or more, coordinate changes will be completely ended, if it is natural to end we do something like "positioning" operation , it's really taking a long time. So I want to optimize the sliding with inertia, such as showing that it does not slide so long, adding a limit time, such as how long the hand left to stop sliding.
The Scrollrectme component adds two properties on top of Scrollrect, inertiamaxtime the maximum free sliding time and sliding the event stopscrollcallback that completely ends, Because the Ondrag event does not meet the requirements when the inertia is rolling, it is just an event that will respond when the hand leaves.

2017-6-27 update added a little bit of sliding or inertia time is insufficient set value Inertiamaxtime does not callback problem.

Finally attach the source code:

Using Unityengine;
Using Unityengine.ui;
Using Unityengine.eventsystems;

Using System; public class Scrollrectme:scrollrect {[Serializefield] public float inertiamaxtime = 0.5f;//limit inertia duration publi
    C action<gameobject > Stopscrollcallback = null;//sliding End callback Private float _scrolledtime = 0f;

   Private action<gameobject> _stopscrollcallback = null;

    Private Vector2 _lastpostion = Vector2.zero; public override void Onenddrag (Pointereventdata eventData) {base.
        Onenddrag (EventData);
        _stopscrollcallback = Stopscrollcallback;
        _scrolledtime = 0f;
    _lastpostion = Vector2.zero; } public override void Ondrag (Pointereventdata eventData) {base.
        Ondrag (EventData);
        _scrolledtime = 0f;
    _lastpostion = Vector2.zero; } public override void Stopmovement () {base. 
    Stopmovement (); } protected override void Lateupdate () {//base.
    Lateupdate (); } Private void Update () {base.
    Lateupdate (); } protected override void Setcontentanchoredposition (Vector2 position) {//2017-6-27 Modify supplemental condition if (_scro Lledtime >= Inertiamaxtime | | (position.
                ToString ("0.0") = = _lastpostion.tostring ("0.0")) {if (_stopscrollcallback! = null) {
                _stopscrollcallback (Transform.gameobject);
            _stopscrollcallback = null;
            } _scrolledtime = Inertiamaxtime;
        Return } base.

        Setcontentanchoredposition (position);
        _scrolledtime + = Time.unscaleddeltatime;
    _lastpostion = position;
 }  
}

Finally attach the resource pack download address on CSDN:
Scrollrect Optimization link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.