所用的插件為:itween
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;//預(yù)制件數(shù)量;
public int numberByShow;//每次顯示做大個(gè)數(shù)
public Transform parent;//顯示的父物體
public Transform parent2;//按鈕的父物體
public GameObject prefab;//UI預(yù)制件
public GameObject prefab2;//按鈕預(yù)制件
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;
}
}
運(yùn)行效果見:
https://v.youku.com/v_show/id_XMzg1NDA2MTE3Ng==.html?spm=a2h0k.11417342.soresults.dtitle&_time=15.969