添加链接描述





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;
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;
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;
}
}
}
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);
}
return item.transform;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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);
}
}
}
