当前位置:网站首页>[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);
}
}
}
边栏推荐
- Passing values between classes using delegates and events
- svg和canvas的区别
- Yolov5 torch installation
- Important knowledge points in unity3d
- Force buckle 349 Intersection of two arrays
- Some problems encountered in unity steamvr
- 力扣(LeetCode)180. 连续出现的数字(2022.06.29)
- Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
- Log writing specification
- Ripple effect of mouse click (unity & shader)
猜你喜欢

UE4 method of embedding web pages

LXC 和 LXD 容器总结

SCM learning notes: interrupt learning

HTC vive cosmos development - handle button event

Connect() and disconnect() of socket in C #

On mask culling of unity

Unreal 4 unavigationsystemv1 compilation error

Force buckle 977 Square of ordered array

LxC and LXD container summary

Unity3d realizes Google Digital Earth
随机推荐
Pytorchcnn image recognition and classification model training framework
Solution to 293 problems in the week of Li Kou
Configuration and use of controllers and routes in nestjs
Unity project hosting platform plasticscm (learn to use 1)
Unity profiler performance analysis
C # equipment synthesis
Unity a* road finding force planning
Unity multiple UI page turning left and right
Win10 vs2015 compiling curaengine
The difference between SVG and canvas
Tcp/ip protocol details Volume I (Reading Guide)
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Unity packaging failure solution
Read and save txt files
Leetcode 180 Consecutive numbers (2022.06.29)
003-JS-DOM-Attr-innerText
Unity3d Google Earth
Modbus protocol register
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Solution to the 292 week match of Li Kou