在Unity中使用滑动列表来看实现换装

该代码示例是Unity3D中用于创建ScrollRect滚动内容的C#脚本,根据方向(Horizontal或Vertical)和数量动态布局预制体,并在用户滑动时管理显示的内容。它使用了Dictionary和Queue数据结构来存储和回收游戏对象。
摘要由CSDN通过智能技术生成

完整代码如下:

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

public class CreateScro : MonoBehaviour
{
    public enum Direction
    {
        Horizontal,
        Vertical
    }
    public RectTransform prefab;
    public ScrollRect scroll;
    public RectOffset padding;
    public float spacing;
    public Direction dir = Direction.Horizontal;
    public int num;
    public RectTransform content;
    public RectTransform mask;

    public Dictionary<Rect, int> dic = new Dictionary<Rect, int>();
    public Dictionary<Rect, GameObject> showdic = new Dictionary<Rect, GameObject>();
    Queue<GameObject> que = new Queue<GameObject>();
    // Start is called before the first frame update
    void Start()
    {
        if (dir==Direction.Horizontal)
        {
            float w = padding.left + prefab.rect.width * num + spacing * (num - 1) + padding.right;
            float h = padding.top + prefab.rect.height + padding.bottom;
            content.sizeDelta = new Vector2(w, h);
            for (int i = 0; i < num; i++)
            {
                dic.Add(new Rect(new Vector2(padding.left+(prefab.rect.width+spacing)*i+prefab.rect.width/2-w/2,0),prefab.sizeDelta),i);
            }
        }
        else
        {
            float w = padding.left + prefab.rect.width + padding.right;
            float h = padding.top + prefab.rect.height * num + spacing * (num - 1) + padding.bottom;
            content.sizeDelta = new Vector2(w, h);
            for (int i = 0; i < num; i++)
            {
                dic.Add(new Rect(new Vector2(0,padding.top+(prefab.rect.height+spacing)*i+prefab.rect.height/2-h/2),prefab.sizeDelta),i);
            }
        }
        OnSlide();
    }

    public void OnSlide()
    {
        Rect rect = new Rect(-content.anchoredPosition, new Vector2(mask.rect.width, mask.rect.height));
        Debug.Log("mask:"+rect);
        //找到需要显示的
        Dictionary<Rect, int> show = new Dictionary<Rect, int>();
        foreach (var item in dic)
        {
            if (IsOverlap(rect,item.Key))
            {
                show.Add(item.Key,item.Value);
            }
        }
        //找创建好单不需要显示的
        List<Rect> rem = new List<Rect>();
        foreach (var item in showdic)
        {
            if (!IsOverlap(rect,item.Key))
            {
                que.Enqueue(item.Value);
                rem.Add(item.Key);
            }
        }

        foreach (var item in rem)
        {
            showdic.Remove(item);
        }
        //创建需要显示的
        foreach (var item in show)
        {
            if (!showdic.ContainsKey(item.Key))
            {
                GameObject obj;
                Debug.Log(que.Count);
                if (que.Count>0)
                {
                    obj = que.Dequeue();
                }
                else
                {
                    obj = Instantiate(prefab.gameObject, content);
                }

                obj.GetComponent<RectTransform>().anchoredPosition = item.Key.position;
                obj.transform.GetChild(0).GetComponent<Image>().sprite =
                    Resources.Load<Sprite>("Image/image_" + item.Value);
                showdic.Add(item.Key,obj);
            }
        }
    }

    private bool IsOverlap(Rect rect1, Rect rect2)
    {
        float r1lefe = rect1.x - rect1.width / 2;
        float r1right = rect1.x + rect1.width / 2;
        float r1down = rect1.y - rect1.height / 2;
        float r1up = rect1.y + rect1.height / 2;

        float r2lefe = rect2.x - rect2.width / 2;
        float r2right = rect2.x + rect2.width / 2;
        float r2down = rect2.y - rect2.height / 2;
        float r2up = rect2.y + rect2.height / 2;

        if (r1lefe<r2right&&r2lefe<r1right&&r1down<r2up&&r2down<r1up)
        {
            return true;
        }

        return false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值