所用的插件为: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;//预制件数量;
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;
}
}