【Unity-学习-005】自制Unity UGUI的 ScrollView 横向滚动功能

 自定义 content 中显示个数,一次向content中间移动一个,支持缩放,支持透明度更改。注意content中的子物体需要是单数个。还有瑕疵,以后完善。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HandleMove : MonoBehaviour
{
    public RectTransform content;
    public Transform[] items;
    public Vector2[] pos;
    public float[] scaleBaseTime;
    public float[] alphaBaseTime;
    private Vector2 tempPos;
    public AudioClip TempClip;


    [Range(3, 10, order = 5)]
    public int viewfieldCount = 5;
    public float itemY = 0;

    public float itemEmptyW = 0;
    public int itemCount = 0;
    public float itemWidth;
    public float contentWidth;
    public float screenWidth;
    public int index = 10000;
    public float speed = 1;

    public bool scaleSize = false;
    public bool changeAlpha = false;

    public Transform toggleParent;
    private void Start()
    {
        itemCount = content.childCount;
        pos = new Vector2[itemCount];
        items = new Transform[itemCount];
        scaleBaseTime = new float[itemCount];
        alphaBaseTime = new float[itemCount];

        for (int i = 0; i < itemCount; i++)
        {
            items[i] = content.GetChild(i);
        }
        if (scaleSize)
        {
            for (int i = 0; i < itemCount; i++)
            {
                if (i < itemCount / 2)
                {
                    scaleBaseTime[i] = scaleBaseTime[itemCount - 1 - i] = (float)i * 2 / (itemCount - 1);
                }
                else if (i == itemCount / 2)
                {
                    scaleBaseTime[i] = 1;
                }
            }
        }
        else
        {
            for (int i = 0; i < itemCount; i++)
            {
                scaleBaseTime[i] = 1;
            }
        }

        if (changeAlpha)
        {
            for (int i = 0; i < itemCount; i++)
            {
                if (i < itemCount / 2)
                {
                    alphaBaseTime[i] = alphaBaseTime[itemCount - 1 - i] = (float)i * 2 / (itemCount - 1);
                }
                else if (i == itemCount / 2)
                {
                    alphaBaseTime[i] = 1;
                }
            }
        }
        else
        {
            for (int i = 0; i < itemCount; i++)
            {
                alphaBaseTime[i] = 1;
            }
        }


        contentWidth = content.rect.width;
        itemWidth = items[0].GetComponent<RectTransform>().rect.width;
        screenWidth = Screen.width;
        float gridWidth = screenWidth / viewfieldCount;
        float gridEmptyWidth = gridWidth - itemWidth;
        float realContentWidth = gridWidth * itemCount;
        float outViewContentWidth = realContentWidth - screenWidth;
        content.offsetMax = new Vector2(outViewContentWidth / 2, 0);
        content.offsetMin = new Vector2(-outViewContentWidth / 2, 0);

        for (int i = 0; i < itemCount; i++)
        {
            pos[i] = new Vector2(i * gridWidth + gridWidth / 2, itemY);
        }
        for (int i = 0; i < itemCount; i++)
        {
            int ind = Mathf.Abs((i + index) % itemCount);
            items[i].GetComponent<SelfItem>().SetSelfPos(pos[ind], scaleBaseTime[ind], alphaBaseTime[ind]);
        }



        if (toggleParent != null)
        {
            GameObject toggleOri = toggleParent.GetChild(0).gameObject;
            for (int i = 1; i < itemCount; i++)
            {
                GameObject temp = Instantiate(toggleOri, toggleParent);
            }
            toggleOri.GetComponent<Toggle>().isOn = true;
        }
    }

    public int toggleIndex = 9999;

    public void MoveToggle(int dir)
    {
        toggleIndex += dir;
        toggleParent.GetChild(toggleIndex % itemCount).GetComponent<Toggle>().isOn = true;
    }




    private void Update()
    {
        if (count > 0)
        {
            count -= Time.deltaTime;
            return;
        }
        if (HLC_GeographyManager.Instance.inputManager.MoveLeft)
        {
            count = 0.5f;
            if (TempClip != null)
            {
                HLC_GeographyManager.Instance.audioManager.PlayClickOneShot(TempClip);
            }
            Down();
            MoveToggle(1);
        }
        else if (HLC_GeographyManager.Instance.inputManager.MoveRight)
        {
            count = 0.5f;
            if (TempClip != null)
            {
                HLC_GeographyManager.Instance.audioManager.PlayClickOneShot(TempClip);
            }
            Up();
            MoveToggle(-1);
        }
    }

    float count = 1;

    public void Up()
    {
        index++;
        for (int i = 0; i < itemCount; i++)
        {
            int ind = Mathf.Abs((i + index) % itemCount);

            tempPos = pos[ind];
            items[i].GetComponent<SelfItem>().SetSelfPos(tempPos, speed, scaleBaseTime[ind], alphaBaseTime[ind]);
        }

    }


    public void Down()
    {
        index--;
        for (int i = itemCount - 1; i >= 0; i--)
        {
            int ind = Mathf.Abs((i + index) % itemCount);

            tempPos = pos[ind];
            items[i].GetComponent<SelfItem>().SetSelfPos(tempPos, speed, scaleBaseTime[ind], alphaBaseTime[ind]);
        }
    }
}

SelfItem代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelfItem : MonoBehaviour
{
    public RectTransform mTrans;
    public Vector2 targetPos;
    public Vector3 targetScale = Vector3.one;
    public CanvasGroup cg;
    public GameObject grid;
    public BoxCollider2D myCollider;
    private void Awake()
    {
        mTrans = GetComponent<RectTransform>();
        grid = transform.Find("Grid").gameObject;
        cg = GetComponent<CanvasGroup>();
        myCollider = GetComponent<BoxCollider2D>();
        myCollider.enabled = false;

    }

    public bool moveing = false;
    public float moveSpeed = 0;
    public float scaleBase = 0;
    public float alpha = 0;
    private float dis = 0;
    private void Update()
    {
        if (moveing)
        {

            dis = Vector2.Distance(mTrans.anchoredPosition, targetPos);
            if (dis < 960 && Vector2.Distance(mTrans.anchoredPosition, targetPos) > 2)
            {
                mTrans.anchoredPosition = Vector2.MoveTowards(mTrans.anchoredPosition, targetPos, moveSpeed);
                mTrans.localScale = Vector3.Lerp(mTrans.localScale, targetScale, Time.deltaTime * moveSpeed * 0.75f);
                cg.alpha = cg.alpha == alpha ? alpha : cg.alpha > alpha ? cg.alpha -= Time.deltaTime * moveSpeed * 0.25f : cg.alpha += Time.deltaTime * moveSpeed * 0.25f;
            }
            else
            {
                mTrans.anchoredPosition = targetPos;
                mTrans.localScale = targetScale;
                moveing = false;
                cg.alpha = alpha;
                if (mTrans.localScale.x == 1 && alpha == 1)
                {
                    grid.SetActive(true);
                    myCollider.enabled = true;
                }
                else if (grid.activeInHierarchy)
                {
                    grid.SetActive(false);
                    myCollider.enabled = false;
                }


            }
        }
    }




    public void SetSelfPos(Vector2 pos, float speed, float scaleBase = 1, float alphaBase = 1)
    {
        targetPos = pos;
        moveSpeed = speed;
        targetScale = Vector3.one * scaleBase;
        alpha = alphaBase;
        moveing = true;
    }

    public void SetSelfPos(Vector2 pos, float scaleBase = 1, float alphaBase = 1)
    {
        targetPos = pos;
        alpha = alphaBase;
        mTrans.anchoredPosition = targetPos;
        mTrans.localScale = Vector3.one * scaleBase;
        cg.alpha = alphaBase;
        if (scaleBase == 1 && alphaBase == 1)
        {
            grid.SetActive(true);
            myCollider.enabled = true;
        }
        else if (grid.activeInHierarchy)
        {
            grid.SetActive(false);
            myCollider.enabled = false;
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThursdayGame

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值