Unity control ScrollView using problem logging

Source: Internet
Author: User
Tags abs

Unity version: 5.6.2

The control scroll view is made up of 4 parts,

1. Root node containing Scroll rect component: Scroll View

2. Node containing the Mask component: Viewport

3. Contents of parent node for all content, often containing layout controls

4. Scroll bar, including landscape and portrait

Specific node details can be used in the official documentation:
Https://docs.unity3d.com/560/Documentation/Manual/script-ScrollRect.html

Problem records encountered when using:

1. How is the display area controlled?
The width and height of the component Rect transform in the node scroll view control the entire area size, and the scrollbar setting of the component scroll rect also affects whether the bounding position of the display area is complete;

The image Type property in the component image of the node viewport affects the displayed area;

The layout and height of the node content's component Rect transform affect the displayed area.

2. How do I remove the scroll bar?

The components in the node scroll view scroll the properties in rect horizontal scrollbar and vertical scrollbar are set to none, and their child nodes scrollbar horizontal and scrollbar Vertical deleted.

3. How is the content laid out?

Add the corresponding layout component to the node content.

4. Why can't I swipe or bounce back to the original place automatically?

This can happen if the width or height of the node content is less than the width or height of the actual contents. At this point, you need to adjust the content width, or add the component content size fitter to set the corresponding direction to preferred size from the Fit wide height.

5. After the sliding end, often can not put the current elements of the picture full display of the situation, how to solve?

This situation needs to be implemented for the node scroll view Mount script: The ScrollRectCenterChild.cs code is as follows:

Note: The Pivot property in the Recttransform component of the content node must be set to 0,1

content nodes can be laid out in vertical layout group, horizontal layout group, or Grid layout group, but only one direction of sliding centering is supported.

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine;usingUnityengine.ui;usingUnityengine.eventsystems;usingSystem; Public enumscrolldir{Horizontal, Vertical} Public classscrollrectcenterchild:monobehaviour,ienddraghandler,idraghandler,ibegindraghandler{ PublicScrolldir Dir =scrolldir.vertical; Private BOOL_iscentering =false;  Public floatMovetocenterspeed =10f; PrivateScrollRect _scrollview; PrivateTransform _content; //The coordinate value used to hold the child element    Privatelist<float> _childrenpos =Newlist<float>(); Private float_targetpos; Private int_curcenterchildindex =-1;  Publicgameobject Curcenterchilditem {Get{gameobject Centerchild=NULL; if(_content! =NULL&&0<= _curcenterchildindex && _curcenterchildindex <_content.childcount) {Centerchild=_content.            Getchild (_curcenterchildindex). Gameobject; }            returnCenterchild; }    }    Private floatGetchilditemwidth (intindex) {        return(_content. Getchild (Index) asrecttransform). sizedelta.x; }    Private floatGetchilditemheight (intindex) {        return(_content. Getchild (Index) asrecttransform). Sizedelta.y; }    voidAwake () {_scrollview= getcomponent<scrollrect>(); if(NULL==_scrollview) {Debug.logerror ("ScrollRect is null."); return; } _content=_scrollview.content; Layoutgroup Layoutgroup=NULL; Layoutgroup= _content. Getcomponent<layoutgroup>(); if(NULL==Layoutgroup) {Debug.logerror ("Layoutgroup comment is null."); return; }        floatspacing =0f; Switch(Dir) { Casescrolldir.horizontal:floatScrollviewrectwidth = _scrollview.getcomponent<recttransform>(). Rect.width; if(Layoutgroup isHorizontallayoutgroup) {                    floatCHILDPOSX = Scrollviewrectwidth *0.5f-Getchilditemwidth (0) *0.5f;                    _childrenpos.add (CHILDPOSX); Spacing= (Layoutgroup ashorizontallayoutgroup). spacing;  for(inti =1; i < _content.childcount; i++) {Childposx-= Getchilditemwidth (i) *0.5f+ Getchilditemwidth (i-1) *0.5f+spacing;                    _childrenpos.add (CHILDPOSX); }                }                Else if(Layoutgroup isGridlayoutgroup) {Gridlayoutgroup grid= Layoutgroup asGridlayoutgroup; floatCHILDPOSX = Scrollviewrectwidth *0.5f-Grid.cellsize.x *0.5f;                    _childrenpos.add (CHILDPOSX);  for(inti =1; i < _content.childcount; i++) {Childposx-= grid.cellsize.x +grid.spacing.x;                    _childrenpos.add (CHILDPOSX); }                }                Else{debug.logerror ("horizontal ScrollView is using Verticallayoutgroup."); }                 Break;  Casescrolldir.vertical:floatScrollviewrectheight = _scrollview.getcomponent<recttransform>(). Rect.height; if(Layoutgroup isVerticallayoutgroup) {                    floatChildposy = Scrollviewrectheight *0.5f-Getchilditemheight (0) *0.5f;                    _childrenpos.add (Childposy); Spacing= (Layoutgroup asverticallayoutgroup). spacing;  for(inti =1; i < _content.childcount; i++) {Childposy+ = Getchilditemheight (i) *0.5f+ Getchilditemheight (i-1) *0.5f+spacing;                    _childrenpos.add (Childposy); }                }                Else if(Layoutgroup isGridlayoutgroup) {Gridlayoutgroup grid= Layoutgroup asGridlayoutgroup; floatChildposy = Scrollviewrectheight *0.5f-Grid.cellsize.y *0.5f;                    _childrenpos.add (Childposy);  for(inti =1; i < _content.childcount; i++) {Childposy+ = Grid.cellsize.y +grid.spacing.y;                    _childrenpos.add (Childposy); }                }                Else{debug.logerror ("Vertical ScrollView is using Horizontallayoutgroup."); }                 Break; }    }    voidUpdate () {if(_iscentering) {Vector3 v=_content.localposition; Switch(Dir) { Casescrolldir.horizontal:v.x= Mathf.lerp (_content.localposition.x, _targetpos, Movetocenterspeed *time.deltatime); _content.localposition=v; if(Math.Abs (_content.localposition.x-_targetpos) <0.01f) {_iscentering=false; }                     Break;  Casescrolldir.vertical:v.y= Mathf.lerp (_CONTENT.LOCALPOSITION.Y, _targetpos, Movetocenterspeed *time.deltatime); _content.localposition=v; if(Math.Abs (_CONTENT.LOCALPOSITION.Y-_targetpos) <0.01f) {_iscentering=false; }                     Break; }        }    }     Public voidOndrag (Pointereventdata eventData) {} Public voidOnenddrag (Pointereventdata eventData) {Switch(Dir) { Casescrolldir.horizontal: _targetpos= Findclosestchildpos (_content.localposition.x, out_curcenterchildindex);  Break;  Casescrolldir.vertical: _targetpos= Findclosestchildpos (_CONTENT.LOCALPOSITION.Y, out_curcenterchildindex);  Break; } _iscentering=true; }     Public voidOnbegindrag (Pointereventdata eventData) {_iscentering=false; _curcenterchildindex= -1; }    Private floatFindclosestchildpos (floatCurrentpos, out intCurcenterchildindex) {        floatclosest =0; floatDistance =mathf.infinity; Curcenterchildindex= -1;  for(inti =0; i < _childrenpos.count; i++)        {            floatp =_childrenpos[i]; floatd = Mathf.abs (P-Currentpos); if(D <distance) {Distance=D; Closest=p; Curcenterchildindex=i; }        }        returnclosest; }}

After changing the effect:

Your reward is my greatest support and affirmation.

Unity control ScrollView using problem logging

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.