Unity之UGUI做翻页效果

所用的插件为:itween


Unity之UGUI做翻页效果_第1张图片
image.png
Unity之UGUI做翻页效果_第2张图片
image.png

Unity之UGUI做翻页效果_第3张图片
image.png

Unity之UGUI做翻页效果_第4张图片
image.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FengjiManager : MonoBehaviour {
    public static FengjiManager Instance { get {return instance; }}
    private static FengjiManager instance;

    public int listNumber;//预制件数量;
    public int numberByShow;//每次显示做大个数
    public Transform parent;//显示的父物体
    public Transform parent2;//按钮的父物体
    public GameObject prefab;//UI预制件
    public GameObject prefab2;//按钮预制件


    public int yy;
  
    // Use this for initialization
    void Start () {
        instance = this;       
        GetAllFjType(listNumber, numberByShow, prefab, parent, prefab2, parent2);
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    
    public void GetAllFjType(int number,int showNumber,GameObject prefab,Transform parent, GameObject prefab2, Transform parent2) {

        for (int i = 0; i < number; i++)
        {
            GameObject go = Instantiate(prefab).gameObject;
            go.transform.SetParent(parent);
            go.transform.localScale = Vector3.one;
            go.transform.localPosition = Vector3.zero;
        }

        if (number/ showNumber > 0)
        {
            if (number<= showNumber)
            {
                return;
            }
           else if(number% showNumber == 0)
            {
                for (int i = 0; i < (number / showNumber); i++)
                {
                    GameObject btn = Instantiate(prefab2).gameObject;
                    btn.name = "image"+i.ToString();                                 
                    btn.transform.SetParent(parent2);
                    btn.transform.localScale = Vector3.one;
                    btn.transform.localPosition = Vector3.zero;
                    btn.GetComponent().onValueChanged.AddListener(delegate (bool isTrue) { OnBtnPage(btn.name); });
                    btn.GetComponent().group = parent2.GetComponent();
                }
            }
            else
            {               
                for (int i = 0; i <= (number / showNumber); i++)
                {
                    GameObject btn = Instantiate(prefab2).gameObject;
                    btn.name = "image" + i.ToString();
                    btn.transform.SetParent(parent2);
                    btn.transform.localScale = Vector3.one;
                    btn.transform.localPosition = Vector3.zero;
                    btn.GetComponent().onValueChanged.AddListener(delegate(bool isTrue) { OnBtnPage(btn.name); });
                    btn.GetComponent().group = parent2.GetComponent();
                }
            }
        }
    }

    int preIndex = 0;

    public void OnBtnPage( string name) {

       
        int index =int.Parse(name.Substring(5, 1));
        if (preIndex == index)
        {
            return;
        }     
       
        Debug.Log(parent.position.y);
        if (preIndex().offsetMax = new Vector2(parent.GetComponent().offsetMax.x, parent.GetComponent().offsetMax.y + chaju*yy);
        }
        else
        {
            int chaju = preIndex - index;
            iTween.MoveTo(parent.gameObject, new Vector3(parent.position.x, parent.position.y - chaju*yy, 0), chaju * 0.5f);
            //parent.GetComponent().offsetMax = new Vector2(parent.GetComponent().offsetMax.x, parent.GetComponent().offsetMax.y- chaju*yy);
        }
        Debug.Log(index);
        preIndex = index;
    }
}

运行效果见:
https://v.youku.com/v_show/id_XMzg1NDA2MTE3Ng==.html?spm=a2h0k.11417342.soresults.dtitle&_time=15.969


https://v.qq.com/x/page/pasdfgzxcvyjsy7.html

你可能感兴趣的:(Unity之UGUI做翻页效果)