当前位置:网站首页>[notes] unity Scrollview button page turning
[notes] unity Scrollview button page turning
2022-06-30 05:08:00 【zjh_ three hundred and sixty-eight】

Illustration above : Generate buttons through code , One page 6 Button , Switch left and right to turn pages .

Illustration above : add to GridLayoutGroup Components can automatically arrange elements ( In addition, there are parallel and vertical sorting components HorizontalLayoutGroup ,VerticalLayoutGroup), You can adjust the arrangement spacing by adjusting the attributes . Upper figure 400 Is the width of one page ,200 Is the page width occupied by a single column element , Because my case is 2 Column buttons , Therefore, the multiple of the width of the single column element must be equal to the width of one page , Otherwise, there will be a similar phenomenon of insufficient or excessive page turning .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestScroolView : MonoBehaviour {
public GameObject objBtn;
private RectTransform contents;
private ScrollRect scrollrect;
// Set according to the demand
private int btnAllcount = 12;// Total number of buttons
private int btnCount = 6;// Number of buttons per page
private float contentsWidth;
private float delta_X;
private float targePoint;
private bool isRoll; // Turn page
void Start()
{
contents = transform.Find("Viewport/Content") as RectTransform;
scrollrect = transform.GetComponent<ScrollRect>();
contentsWidth = contents.sizeDelta.x;
// To obtain by calculation contents The length of
delta_X = Mathf.CeilToInt(btnAllcount / btnCount) * contentsWidth;
// to contents Set the size
contents.sizeDelta = new Vector2(delta_X, contents.sizeDelta.y);
// to contents Add a button
for (int i = 0; i < btnAllcount; i++)
{
GameObject obj = Instantiate(objBtn, contents);
//obj.transform.GetChild(0).GetComponent<Text>().text = i.ToString();
}
}
private void OnGUI()
{
// Left button
if (GUI.Button(new Rect(200, 80, 100, 30), " The previous page "))
{
isRoll = true;
targePoint -= 1 / (delta_X / contentsWidth - 1);
if (targePoint < 0)
targePoint = 0;
}
// Right button
if (GUI.Button(new Rect(420, 80, 100, 30), " The next page "))
{
isRoll = true;
targePoint += 1 / (delta_X / contentsWidth - 1);
if (targePoint > 1)
targePoint = 1;
}
// Page turning
if (isRoll)
{
print(" Page turning ");
if (Mathf.Abs(scrollrect.horizontalNormalizedPosition - targePoint) < 0.01f)
{
scrollrect.horizontalNormalizedPosition = targePoint;
isRoll = false;
return;
}
// Set the horizontal scroll position
scrollrect.horizontalNormalizedPosition = Mathf.Lerp(scrollrect.horizontalNormalizedPosition, targePoint, Time.timeScale * 0.1f);
}
}
}
边栏推荐
- JPA composite primary key usage
- Configuration and use of controllers and routes in nestjs
- Special folders in unity3d and their meanings
- Force buckle 977 Square of ordered array
- 2021-06-17 solve the problem of QML borderless window stretching, window jitter and flicker when stretching and shrinking
- Redis cluster concept
- Chapter 10 of OpenGL super classic (7th Edition) calculation shader
- 【VCS+Verdi联合仿真】~ 以计数器为例
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- 003-JS-DOM-Attr-innerText
猜你喜欢

Unity3d Google Earth

Procedural animation -- inverse kinematics of tentacles

中文版PyCharm改为英文版PyCharm

One command to run rancher

Harbor API 2.0 query

Unity is associated with vs. there is a compiler problem when opening

Records of some problems encountered during unity development (continuously updated)

Autowired注解警告的解决办法

Preorder traversal of Li Kou 589:n fork tree

Writing unityshader with sublimetext
随机推荐
svg和canvas的区别
Ripple effect of mouse click (unity & shader)
[vcs+verdi joint simulation] ~ take the counter as an example
力扣589:N 叉树的前序遍历
Network communication problem locating steps
Oracle-数据的基本操作
Parkour demo
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Singleton mode in unity
Unity camera control
Sourcetree usage
力扣27. 移除元素
ParticleSystem in the official Manual of unity_ Collision module
Unity ontriggerenter does not call
力扣59. 螺旋矩阵 II
Pycharm database tool
Chapter 11 advanced data management of OpenGL super classic (version 7)
Unity a* road finding force planning
Force buckle 704 Binary search
力扣704. 二分查找