博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
无限背包
阅读量:6675 次
发布时间:2019-06-25

本文共 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 Dictionary
nowShowItem=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 Queue
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
(); 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/

你可能感兴趣的文章
1.1 DHCP服务的介绍和安装
查看>>
计算机安全篇1
查看>>
drawable 如何单个设置边界
查看>>
Nachos File System
查看>>
tomcat远程调试、普通java程序远程调试
查看>>
python day11
查看>>
office 2013-word选项配置参数
查看>>
Hadoop调试源代码
查看>>
Hanoi塔算法c语言实现
查看>>
ecshop新会员注册邮件提醒管理员
查看>>
mysqldump实现mysql备份小脚本
查看>>
JQuery--实用技巧总结
查看>>
语言,编程语言
查看>>
Redis事务处理
查看>>
C# []、List、Array、ArrayList 区别及应用
查看>>
继续说一下js对数组的处理---删除某个指定元素的方法
查看>>
data truncated for column at row 1原因
查看>>
java 关键字
查看>>
linux io过程自顶向下分析
查看>>
UNIX 技巧: UNIX 高手的另外10个习惯
查看>>