主要功能:
1.content自動擴充(僅為初始化時)
2.實現(xiàn)左右按鈕點擊 翻頁查看
3.分頁展示,遵循從左往右匹表,從上往下順序排版
順序:分頁排版展示门坷,從左往右,從上往下順序
(類似Grid Layout Group 和 Content Size Fitter 組件結合實現(xiàn)袍镀,不同在于Grid Layout Group整個content從左往右的排版默蚌,此處實現(xiàn)為content分頁從左往右排版,例:content下有兩頁內容苇羡,實現(xiàn)了第一頁從左往右從上往下順序排版后绸吸,再根據(jù)從左往右從上往下順序排版第二頁內容)
代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ContentTestScript : MonoBehaviour
{
/*
[Header("RectTransform")]//顯示到inspector面板
[Tooltip("RectTransform")] //鼠標放上顯示注釋
編輯器模式下添加 Content 下子物體無法自動排版,可更改一下Inspector面板中的任意屬性的數(shù)值即可重寫排版
*/
[Header("RectTransform")]
[Tooltip("RectTransform")]
public RectTransform rect;
[Header("預制體")]
[Tooltip("預制體")]
public GameObject item;
[Header("左邊距離")]
[Tooltip("左邊距離")]
public float padding_left = 55; //左邊距離
[Header("頂部距離")]
[Tooltip("頂部距離")]
public float padding_top = 78; //頂部距離
[Header("元素寬")]
[Tooltip("元素寬")]
public float cell_x = 300; //元素寬
[Header("元素高")]
[Tooltip("元素高")]
public float cell_y = 180; //元素高
[Header("元素之間左右距離")]
[Tooltip("元素之間左右距離")]
public float spacing_x =55; //元素之間左右距離
[Header("元素之間上下距離")]
[Tooltip("元素之間上下距離")]
public float spacing_y =27; //元素之間上下距離
//每頁數(shù)量
int perPage = 0;
int imageCount = 67;
int row = 3; //行數(shù)
int column = 4;//每頁展示的列數(shù)
//當前展示總有多少頁
int pageCount = 0;
//移動邏輯
bool isMove = false;
//移動目標位置
Vector3 moveTarget = Vector3.zero;
//當前移動的頁數(shù)
int moveindex = 0;
//每一頁的寬度
float perPageWidth = 0;
void Start()
{
}
void Init() {
moveindex = 0;
perPage = row * column;
imageCount = transform.childCount;
SetCellSize();
SetRectPivotAndAnchors();
SetContentSize();
ShowImage();
}
/// <summary>
/// 當數(shù)值發(fā)生改變時執(zhí)行此方法
/// </summary>
void OnValidate()
{
Init();
}
void FixedUpdate () {
if (isMove)
{
if (Vector3.Distance(transform.localPosition, moveTarget) > 10f)
{
transform.localPosition = Vector3.Lerp(transform.localPosition, moveTarget, 0.1f);
}
else {
isMove = false;
}
}
}
/// <summary>
/// 設置元素大小
/// </summary>
void SetCellSize() {
for (int i = 0; i < imageCount; i++)
{
RectTransform newRect= transform.GetChild(i).GetComponent<RectTransform>();
newRect.sizeDelta = new Vector2(cell_x, cell_y); //設置item大小
}
}
/// <summary>
/// 設置軸點和錨點 左上
/// </summary>
void SetRectPivotAndAnchors() {
rect.pivot = new Vector2(0, 1); //軸點
//錨點
rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 1);
rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 1);
}
/// <summary>
/// 設置content大小
/// </summary>
void SetContentSize() {
pageCount = imageCount / perPage;//取整
int remain= imageCount % perPage;//取余
if (remain>0)
{
pageCount += 1;
}
float hight = padding_top * 2 + cell_y * row + spacing_y * (row - 1);//頂距離+底距離+每行的高+行之間的間距
float width = padding_left * 2 + pageCount * column * cell_x + (pageCount * column - 1) * spacing_x;
rect.sizeDelta = new Vector2(width, hight); //設置content大小
}
//臨時content子物體下標
int index=0;
/// <summary>
/// 加載圖片
/// </summary>
void ShowImage()
{
//content 的中心點為左上角
//此時content的左上角坐標為(0,0,0)设江,因此第一個圖片(左上角)位置為:
float start_x = padding_left + cell_x / 2;
float start_y = -(padding_top + cell_y / 2);
index = 0;
//當前頁數(shù) pageCount
for (int i = 0; i < pageCount; i++)
{
for (int m = 0; m < row; m++) //每行
{
for (int n = 0; n < column; n++) //每列
{
if (index >= imageCount)
{
break;
}
//每一頁的寬度 第二頁時寬需要加上第一頁的寬度
perPageWidth = column * cell_x + spacing_x * column;
Vector3 newPos = new Vector3(perPageWidth * i + start_x + (spacing_x + cell_x) * n, start_y - (spacing_y + cell_y) * m, 0);
//獲取當前圖片
transform.GetChild(index).localPosition = newPos;
index++;
}
}
}
}
public void LeftButtonClick() {
moveindex--;
if (moveindex >= 0)
{
isMove = true;
moveTarget = new Vector3(-moveindex * perPageWidth + 10, transform.localPosition.y, transform.localPosition.z); //+10為距離判斷誤差
//Debug.Log(string.Format("<color=yellow>{0}</color>", moveTarget ));
}
else {
moveindex=0;
}
}
public void RightButtonClick()
{
moveindex++;
//Debug.Log(moveindex < pageCount);
if (moveindex < pageCount)
{
Debug.Log("perPageWidth:" + perPageWidth);
isMove = true;
moveTarget = new Vector3(-(moveindex * perPageWidth + 10), transform.localPosition.y, transform.localPosition.z); //+10為距離判斷誤差
//Debug.Log(string.Format("<color=yellow>{0}</color>", moveTarget ));
}
else {
moveindex = pageCount - 1;
}
}
}
界面截圖:
UI搭建:
UI.png