效果圖如下:
其實很簡單,在NGUI原有的滑動組件的基礎上處理一下比例系數就好,每個塊的位置是固定的,移動的是Panel。
所以呢用Panel的位置與塊的位置做差在比幾個塊不就成了比例系數了么。。自然就出來了,最后在處理一下層級就ok了。
代碼如下(我是生成的10個塊):
using UnityEngine; using System.Collections; public class test : MonoBehaviour { private GameObject mItem; private GameObject[] Items = new GameObject[11]; private Vector3 Pos = new Vector3(0, 0, 0); private bool begin = false; void Start() { mItem = transform.Find("Item").gameObject; for (int i = 9; i >= 0; i--) { Clone(i); } begin = true; } void Clone(int i) { GameObject _clone = Instantiate(mItem) as GameObject; Pos.x = 100 * i; _clone.transform.SetParent(gameObject.transform); _clone.transform.localPosition = Pos; _clone.transform.localScale = Vector3.one; _clone.SetActive(true); Items[i] = _clone; } void Update() { if (begin) { float max = 0; int j = 0; for (int i = 0; i < 10; i++) { Vector3 Ipos = Items[i].transform.localPosition; float factor = Mathf.Abs(Ipos.x + gameObject.transform.parent.transform.localPosition.x); factor = 1 - factor / 1000; Vector3 p = Vector3.one * factor; p.x = 1; Items[i].transform.localScale = p; if (factor > max) { max = factor; j = i; } } for (int i = 0; i < 10; i++) { if (j != i) { Items[i].GetComponent<UISprite>().depth = 1; } else { Items[i].GetComponent<UISprite>().depth = 2; } } } } }