工程下载
链接:https://pan.baidu.com/s/1AQSrZJjOpHZ4wdMonvBkig
提取码:sybj
效果图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Move : MonoBehaviour
{
public Scrollbar bar;
public GameObject[] itemlist;
private float distance = 0;
public int selectIndex = 0;
public int lastSelectIndex = 0;
private float time = 0;
private float target = 0;
// Use this for initialization
void Start()
{
Info();
}
public void Info()
{
distance = 1.0f / ((float)itemlist.Length - 1);
for (int i = 0; i < itemlist.Length; i++)
{
itemlist[i].GetComponent<Item>().x = distance * i;
}
int value = (int)(bar.value / distance + 0.5f);
for (int i = 0; i < itemlist.Length; i++)
{
if (i > value)
itemlist[i].transform.SetSiblingIndex(itemlist.Length - 1 - i + value);
else if (i < value)
itemlist[i].transform.SetSiblingIndex(i);
else if (i == value)
itemlist[i].transform.SetSiblingIndex(itemlist.Length - 1);
}
}
public void PointUp()
{
int value = (int)(bar.value / distance + 0.5f);
selectIndex = value;
target = distance * value;
itemlist[selectIndex].transform.SetSiblingIndex(itemlist.Length - 1);
time = 0;
lastSelectIndex = selectIndex;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
PointUp();
}
if (Input.GetMouseButton(0))
{
int value = (int)(bar.value / distance + 0.5f);
for (int i = 0; i < itemlist.Length; i++)
{
if (i > value)
itemlist[i].transform.SetSiblingIndex(itemlist.Length - 1 - i + value);
else if (i < value)
itemlist[i].transform.SetSiblingIndex(i);
else if (i == value)
itemlist[i].transform.SetSiblingIndex(itemlist.Length - 1);
}
time += Time.deltaTime;
return;
}
if (target != bar.value)
{
if (bar.value < target)
bar.value += Time.deltaTime / 5;
if (bar.value >= target)
bar.value -= Time.deltaTime / 5;
if (Mathf.Abs(bar.value - target) < 0.01f)
{
bar.value = target;
}
}
}
public GameObject Max;
public Image max_image;
public void PointClick()
{
if (time < 0.5f && selectIndex == lastSelectIndex)
{
Max.SetActive(true);
Debug.Log("selectIndex" + selectIndex);
max_image.sprite = itemlist[selectIndex].GetComponent<Image>().sprite;
}
}
public void OnClick_MaxCloseEvent()
{
Max.SetActive(false);
}
}
通过Scrollbar.value滑动值判定滑动了几个位置,然后改变物体索引值达到缩放效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Item : MonoBehaviour
{
public Scrollbar bar;
public float x;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.localPosition = new Vector3(-(bar.value - x) * 500, 0, 0);
float scale = 1 - Mathf.Abs(bar.value - x) * 0.5f;
transform.localScale = new Vector3(scale, scale, 1);
}
}