UGUI scrollView基本操作

滚动条 scrollRect,Mask,GridLayout

ScrollRect基本属性
content 需要滑动的对象
horizontal/vertical 滚动方式 水平/垂直
moveMentType 移动类型 unrestricted /Elastic/clamped 自由的/有弹性的/夹紧的
inertial 惯性值

Mask

控制scroll下面的字对象超出scroll的范围,不进行显示
UGUI scrollView基本操作_第1张图片

GridLayoutGroup 网格布局
Padding 偏移量,一般不需要调节
cellSize 每一字网格的尺寸
spacing 网格之际的间隔

startCorner 开始的角,左角,右角
startAxis 开始的轴向
childAligment 字对象 最对象的位置(可以把grid尺寸设置大一些,查看字对象的位置)
constraint 约束,限制字对象的排列方式,包括自适应,根据行数自适应,根据列数自适应。

UGUI scrollView基本操作_第2张图片

通过代码,动态添加字对象

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class scrowTest : MonoBehaviour {
    public GameObject itemPrefab;
    public ScrollRect scroll;
    public GridLayoutGroup grid;
    public int AllCount=30;
    // Use this for initialization
    void Start () {
        initScrollItem ();
    }

    // Update is called once per frame
    void Update () {

    }

    void initScrollItem(){

        for (int i = 0; i < AllCount; i++) {
            GameObject itemObj = Instantiate (itemPrefab,grid.transform) as GameObject;
            itemObj.GetComponent ().setData ("image"+i);
        }
        float colums = grid.constraintCount;//分4列显示

        int rows = Mathf.CeilToInt (grid.transform.childCount/colums);
        //设置grid的大小,如果grid太小,则无法滚动,因此grid的大小需要和所有字对象的大小保持一致
        grid.GetComponent ().SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical,(grid.cellSize.y+grid.spacing.y)*rows);
        grid.GetComponent ().SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal,(grid.cellSize.x+grid.spacing.x)*colums);
        scroll.verticalNormalizedPosition = 1;//初始化scroll的位置
        scroll.horizontalNormalizedPosition=0;

    }
}

运行效果
UGUI scrollView基本操作_第3张图片

UGUI scrollView基本操作_第4张图片

实现简单的分页效果,优化实例化对象个数

代码如下

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
enum ItemType{
    One,
    Two,
    Three,
    Four
}
public class scrowTest : MonoBehaviour {
    public GameObject itemPrefab;
    public ScrollRect scroll;
    public GridLayoutGroup grid;
    public int AllCount=30;
    public Button btn1,btn2,btn3,btn4;
    private ItemType iType;
    private int listCount=0;
    private List itemList = new List ();
    public Image img;
    // Use this for initialization
    void Start () {
        iType = ItemType.One;
//      initScrollItem ();
        btn1.onClick.AddListener (OnBtn1Click);
        btn2.onClick.AddListener (OnBtn2Click);
        btn3.onClick.AddListener (OnBtn3Click);
        btn4.onClick.AddListener (OnBtn4Click);
        OnBtn1Click ();
        img.transform.SetAsLastSibling ();
    }

    // Update is called once per frame
    void Update () {

    }

    void OnBtn1Click(){
        iType = ItemType.One;
        listCount = 18;
        ResetScrollList ();
    }

    void OnBtn2Click(){
        iType = ItemType.Two;
        listCount = 40;
        ResetScrollList ();
    }

    void OnBtn3Click(){
        iType = ItemType.Three;
        listCount = 25;
        ResetScrollList ();
    }

    void OnBtn4Click(){
        iType = ItemType.Four;
        listCount = 18;
        ResetScrollList ();
    }

    void ResetScrollList(){
        int offsetCount = listCount - itemList.Count;
        if (offsetCount <= 0) {
//          string data;
            for (int i = 0; i < itemList.Count; i++) {
                GameObject itemObj = itemList [i];
                if(itemObj!=null){
                    itemObj.GetComponent ().setData (iType.ToString()+"------"+i);
                    itemObj.SetActive (true);
                    string data = iType.ToString () + " this is " + i + " grid!";
                    itemObj.transform.FindChild ("btn").GetComponent

运行效果
UGUI scrollView基本操作_第5张图片

demo 下载:http://download.csdn.net/detail/u011484013/9842959

相关博客:http://blog.csdn.net/u011484013/article/details/72081793
http://www.cnblogs.com/zhaoqingqing/p/3973167.html?utm_source=tuicool

你可能感兴趣的:(unity-开发)