本文共 7170 字,大约阅读时间需要 23 分钟。
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{ public List - items=new List
- (); 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 DictionarynowShowItem=new Dictionary (); 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 ().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;//////不继承mono的单利基类/// ///public class BaseManager where T : class, new(){ private static T _instance; public static T GetInstance { get { if (_instance == null) { _instance = new T(); } return _instance; } }}/// /// 继承mono的单利基类/// ///public class BaseManagerMono : MonoBehaviour where T : MonoBehaviour{ private static T _instance; public static T GetInstance { get { if (_instance == null) { _instance = GameObject.FindObjectOfType (); } 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 Queuequeues; 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 (); 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 (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;////// 对象池管理类/// public class PoolMgr : BaseManager{ private Transform poolParent; private Dictionary pools; public void Init() { GameObject game = new GameObject("Pools"); game.SetActive(false); poolParent = game.transform; pools = new Dictionary (); 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); } }}
转载地址:http://mfrxo.baihongyu.com/