Unity之UGUI做翻页效果

所用的插件为:itween


image.png
image.png

image.png

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<Toggle>().onValueChanged.AddListener(delegate (bool isTrue) { OnBtnPage(btn.name); });
                    btn.GetComponent<Toggle>().group = parent2.GetComponent<ToggleGroup>();
                }
            }
            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<Toggle>().onValueChanged.AddListener(delegate(bool isTrue) { OnBtnPage(btn.name); });
                    btn.GetComponent<Toggle>().group = parent2.GetComponent<ToggleGroup>();
                }
            }
        }
    }

    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<index)
        {
            int chaju = index - preIndex;
            iTween.MoveTo(parent.gameObject, new Vector3(parent.position.x, parent.position.y+ chaju*yy, 0), chaju * 0.5f);
            //parent.GetComponent<RectTransform>().offsetMax = new Vector2(parent.GetComponent<RectTransform>().offsetMax.x, parent.GetComponent<RectTransform>().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<RectTransform>().offsetMax = new Vector2(parent.GetComponent<RectTransform>().offsetMax.x, parent.GetComponent<RectTransform>().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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,251评论 25 709
  • 不知不觉中加入刻意训练营已有一周,貌似在这一周每天的打卡里,我都是接近截止时间才提交的,其中一天还是补交的...
    徐徐向上阅读 1,720评论 0 0
  • 一、构思:立意、标题、内容(选裁、剪裁)、结构、语言(平实、华丽、辛辣) 二、腹稿:标题、开头(启)、中间(主体-...
    剑走偏锋独行侠阅读 3,003评论 0 1
  • 晚上占先生跟我说要出去吃饭,说今天6月初六吃狗肉的日子。 说实话,我是一口都没吃。虽然也不怎么喜欢狗,但是真心觉得...
    星光璀璨520阅读 2,817评论 2 2

友情链接更多精彩内容