当前位置:网站首页>[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);
}
}
}
边栏推荐
- Output directory of log files after unity3d packaging
- Unity + hololens2 performance test
- Basic operations of Oracle data
- Unity3d lookat parameter description
- Unity Logitech steering wheel access
- 003-JS-DOM-Attr-innerText
- Four methods of unity ugui button binding events
- Unity project hosting platform plasticscm (learn to use 1)
- Redis cluster concept
- Unity ontriggerenter does not call
猜你喜欢

Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor

力扣209. 长度最小的子数组

MinGW-w64下载文件失败the file has been downloaded incorrectly!

Chinese pycharm changed to English pycharm

Easyrecovery data recovery software recovers my photo and video data two years ago

Force buckle 704 Binary search

Introduction to some representations, neighbors and degrees of Graphs

Create a simple battle game with photon pun

Unity3d packaging and publishing APK process

Records of problems encountered in unity + hololens development
随机推荐
Unity is associated with vs. there is a compiler problem when opening
Under what conditions does the Z-index attribute expire?
Records of some problems encountered during unity development (continuously updated)
Nestjs入门和环境搭建
Unity a* road finding force planning
Force buckle 59 Spiral matrix II
Important knowledge points in unity3d
Preorder traversal of Li Kou 589:n fork tree
Deeply understand the function calling process of C language
Unity3d realizes Google Digital Earth
JPA composite primary key usage
MinGW-w64下载文件失败the file has been downloaded incorrectly!
Unreal 4 learning notes - data storage using blueprints
Solution to Autowired annotation warning
Initial environment configuration of the list of OpenGL super classic (version 7) vs2019
The difference between SVG and canvas
力扣2049:统计最高分的节点数目
力扣292周赛题解
What is multimodal interaction?
How does unity use mapbox to implement real maps in games?