无限背包

添加链接描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

public class BagItem : MonoBehaviour
{
    public Text count;
    
    public  void InitItem(Item item)
    {
        count.text = item.Count.ToString();
    }
    
}

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

public class Item
{
    public int ID { get; set; }
    public int Count { get; set; }
}

public class BagMgr : BaseManager<BagMgr>
{
   public List<Item> items=new List<Item>();

    public  void InitItems(int count)
    {
        for (int i = 0; i < count; i++)
        {
            Item item=new Item(){ID = i,Count = i};
            items.Add(item);
        }
    }

}

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

public class BagPanel : MonoBehaviour
{
    public RectTransform content;
    public ScrollRect scrollRect;
    public int viewPortH; //可视范围的高
    public int gridHigh;
    public int intervalx;
    public int intervaly;
    public int xcount = 3; //横着多少格子
    private int oldMinIndex = -1;
    private int oldMaxIndex = -1;
    private Dictionary<int,Transform> nowShowItem=new Dictionary<int, Transform>();
    void Start()
    {
        content.sizeDelta=new Vector2(0, 10000/3*(gridHigh+intervaly));
        scrollRect.onValueChanged.AddListener((value) => { Change(value); });
    }

    public void Change(Vector2 value)
    {
        //起始位置
        int minIndex = (int)content.anchoredPosition.y / (gridHigh+ intervaly)*3;
        //结束位置
        int maxIndex = ((int)content.anchoredPosition.y+viewPortH) / (gridHigh + intervaly) * 3+2;

        //移除上一次不在视野范围内的物品
        for (int i = oldMinIndex; i < minIndex; i++)
        {
            if (nowShowItem.ContainsKey(i))
            {
                PoolMgr.GetInstance.Push(nowShowItem[i].transform.name, nowShowItem[i].transform);
                nowShowItem.Remove(i);
            }
        }

        for (int i = maxIndex; i <=oldMaxIndex; i++)
        {
            if (nowShowItem.ContainsKey(i))
            {
                PoolMgr.GetInstance.Push(nowShowItem[i].transform.name, nowShowItem[i].transform);
                nowShowItem.Remove(i);
            }
        }
        oldMinIndex = minIndex;
        oldMaxIndex= maxIndex;
        //创建可视范围内的item
        for (int i = minIndex; i <=maxIndex; i++)
        {
            if (nowShowItem.ContainsKey(i))
            {
                continue;
            }
            else
            {
                Transform item = PoolMgr.GetInstance.GetObjByInput();
                item.SetParent(content, false);
                item.GetComponent<BagItem>().InitItem(BagMgr.GetInstance.items[i]);
                //设置位置
                item.localPosition = new Vector3(i % xcount * (gridHigh + intervalx), -i / 3 * (gridHigh + intervaly), 0);
                nowShowItem.Add(i,item);
            }

        }
    }
}

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

/// <summary>
///不继承mono的单利基类
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseManager<T> where T : class, new()
{

    private static T _instance;

    public static T GetInstance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new T();
            }

            return _instance;
        }
    }

}




/// <summary>
/// 继承mono的单利基类
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseManagerMono<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;

    public static T GetInstance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.FindObjectOfType<T>();
            }

            return _instance;
        }
    }
}


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

public class GameManager : MonoBehaviour
{

    public BagPanel BagPanel;
    void Start()
    {
        PoolMgr.GetInstance.Init();
        BagMgr.GetInstance.InitItems(10000);
        PoolMgr.GetInstance.GetObj("BagItem");
        BagPanel.Change(Vector2.zero);
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            PoolMgr.GetInstance.GetObjByInput().SetParent(transform,false);
        }

    }
}

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

public class HideSelf : MonoBehaviour
{
   

}


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

public enum ObjType
{
    BagItem,
    Bullet,
    Fire,
    Hole,
}

public class Pool
{
    private Transform parent;
    private ObjType type;
    private Queue<Transform> queues;
    private Type[] types;
    public Pool(string poolName, Transform parent, ObjType type, params Type[] types)
    {
        GameObject game = new GameObject(poolName);
        game.transform.SetParent(parent);
        this.parent = game.transform;
        queues = new Queue<Transform>();
        this.type = type;
        this.types = types;
    }

    public Transform GetObj()
    {
        if (queues.Count > 0)
        {
            return queues.Dequeue();
        }
        else
        {
            return SpawnNew();
        }
    }

    public void Push(Transform obj)
    {
        obj.SetParent(parent);
        queues.Enqueue(obj);
    }
    private Transform SpawnNew()
    {
        GameObject item = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(type.ToString())).gameObject;
        item.name = parent.gameObject.name;
        foreach (Type type in types)
        {
            item.AddComponent(type);
        }


        //if (item.transform.parent!=null)
        //{
        //    item.transform.parent = null;

        //}

        return item.transform;
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 对象池管理类
/// </summary>
public class PoolMgr : BaseManager<PoolMgr>
{
    private Transform poolParent;
    private Dictionary<string, Pool> pools;
    public void Init()
    {
        GameObject game = new GameObject("Pools");
        game.SetActive(false);
        poolParent = game.transform;
        pools = new Dictionary<string, Pool>();
        string[] objNames = Enum.GetNames(typeof(ObjType));
        for (int i = 0; i < objNames.Length; i++)
        {
            pools.Add(objNames[i], new Pool(objNames[i], poolParent, (ObjType)Enum.Parse(typeof(ObjType), objNames[i]), typeof(HideSelf)));
        }
    }


    public Transform GetObjByInput()
    {
        string[] objNames = Enum.GetNames(typeof(ObjType));
        string name = objNames[0];
        return GetObj(name);
    }

    public Transform GetObj(string name)
    {
        if (pools.ContainsKey(name))
        {
            Transform obj = pools[name].GetObj();
            obj.parent = null;
            return obj;
        }
        else
        {
            Debug.Log("不包含这个key:" + name);
            return null;
        }
    }

    public void Push(string name, Transform obj)
    {
        if (pools.ContainsKey(name))
        {
            pools[name].Push(obj);
        }
        else
        {
            Debug.Log("不包含这个key:" + name);
        }
    }

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>