NGUI之背包系统

背包系统的组件有:UIGrid,UIScrollview(循环)
NGUI原生的的UIsrollview 是没循环功能的,因此要想背包系统运行流畅,就必须将所有物品放在循环的scrollview中
不废话,直接上代码和效果图

效果图:

代码:

1 循环scrollview ( BackpackLoopScrollView )

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using cl_ls;
using cl_gs;
using cl_gs_ex;
using cl_gs_checkpoint;
using System.Collections.Generic;
using System;
using evt;
using fil;
using tools;
using System.IO;
using enumtype;
[RequireComponent(typeof(UIGrid))]
public class BackpackLoopScrollView : MonoBehaviour
{
	#region Members
	private List>  _dataList; 
	private List    _itemsList = new List(); // 存储物件列表
	private Vector4           _posParam;
	private Transform         _cachedTransform; 
	private int               _startIndex;       // 起始下标  
	private int               _maxCount;         // 最大长度               
	public  UIScrollView      _scrollView;       // 父ScrollView 
	private UIGrid            _grid;
	private bool              _canSlideToleft=false;   //是否可以向左互动
	private bool              _canSlideToRight=false;  //是否可以向右滑动
	#endregion
	void Awake()
	{
		_cachedTransform = this.transform;
		_scrollView = _cachedTransform.parent.GetComponent();
		// 设置Cull  
		_scrollView.GetComponent().cullWhileDragging = true;
		_grid = this.transform.GetComponent();
		float cellWidth = _grid.cellWidth;
		float cellHeight = _grid.cellHeight;
		_posParam = new Vector4(cellWidth,cellHeight,_grid.arrangement == UIGrid.Arrangement.Horizontal ? 1 : 0,_grid.arrangement == UIGrid.Arrangement.Vertical ? 1 : 0);

	}

	/**设置数据列表*/
	public List> DataList
	{
		set{_dataList=value;}
		get{return _dataList;}
	}
	/**创建item*/
	public List itemList
	{
		set{_itemsList=value;}
		get{return _itemsList;}
	}
	/**初始化表*/
	public void Init(List itemsList)
	{
		_itemsList=itemsList;
		for (int i = 0; i < itemsList.Count; ++i)
		{
			Transform itme = itemsList[i].transform;
			UIWidget widget = itme.GetComponent();
			widget.name = _itemsList.Count.ToString();
			if(i<_dataList.Count)
			{
				SetInfo(widget,_dataList[i]);
			}
		}
	}
	/**设置item的信息*/
	private void SetInfo(UIWidget widget,List info)
	{
		if(info.Count==0)
		{
			for(int i=1;i<=2;i++)
			{
				widget.transform.FindChild((i+1)+"/itemPanel").gameObject.SetActive(false);
			}
			return;
		}
		for(int i=0;i<2;i++)
		{
			if(info[i]==null)
			{
				widget.transform.FindChild((i+1)+"/itemPanel").gameObject.SetActive(false);
				return;
			}
			else
			{
				widget.transform.FindChild((i+1)+"/itemPanel").gameObject.SetActive(true);
				widget.transform.FindChild((i+1)+"/itemPanel/select").gameObject.SetActive(false);
			}
			Dictionary dic = ConfigManager.getInstance ().readDictionaryForId ("ItemRes", "id", info[i].rid.ToString());
			widget.transform.FindChild((i+1)+"/id").GetComponent().text=info[i].uid.ToString();                //id
			widget.transform.FindChild((i+1)+"/itemPanel/heroName").GetComponent().text=dic["name"];           //名称
			widget.transform.FindChild((i+1)+"/itemPanel/itemIcon").GetComponent().spriteName=dic["picture"]; //图标
			widget.transform.FindChild((i+1)+"/itemPanel/conttxt").GetComponent().text=info[i].num.ToString(); //数量
			widget.transform.FindChild((i+1)+"/itemPanel/Sprite").GetComponent().spriteName=EnumToString.GetInstance ().getGoodByQualityBack(int.Parse(dic["quality"])); //品质
		}
	}
	/**更新表单*/
	public void UpdateListItem(int count)
	{
		_canSlideToleft=true;
		_startIndex = 0;
		_maxCount = count;
		if(_grid==null)
		{
			_grid = this.transform.GetComponent();
		}
		_grid.repositionNow=true;
		List list=new List();
		for(int k=0;k<_itemsList.Count;k++)
		{
			list.Add(_grid.transform.GetChild(k).GetComponent());
		}
		_itemsList=list;
		for (int i = 0; i < _itemsList.Count; i++)
		{
			UIWidget item = _itemsList[i];
			item.name =i.ToString();
			if(i < count)
			{
				SetInfo(item,_dataList[i]);
				_canSlideToRight=false;
			}
			else
			{
				_canSlideToRight=true;
				item.transform.FindChild("1/itemPanel").gameObject.SetActive(false);
				item.transform.FindChild("2/itemPanel").gameObject.SetActive(false);
			}
		}
	}
	//滑动时刷新item的位置
	void LateUpdate()
	{
		if(!_scrollView.isDragging)
		{
			return;
		}
		if(_dataList.Count<=_itemsList.Count)
		{
			return;
		}
		int _sourceIndex = -1;
		int _targetIndex = -1;
		int _sign = 0;//滑动方向
		
		bool firstVislable = _itemsList[0].isVisible;                  //第一个item
		bool lastVisiable = _itemsList[_itemsList.Count - 1].isVisible;//最后一个item
		
		// 如果都显示,那么返回  
		if (firstVislable == lastVisiable)
		{
			return;
		}
		
		// 得到需要替换的源和目标  
		if (firstVislable)
		{
			if(_canSlideToleft)
			{
				return;
			}
			_sourceIndex = _itemsList.Count - 1;
			_targetIndex = 0;
			_sign = 1;
		}
		else if (lastVisiable)
		{
			if(_canSlideToRight)
			{
				return;
			}
			_sourceIndex = 0;
			_targetIndex = _itemsList.Count - 1;
			_sign = -1;
		}
		 
		int realSourceIndex = int.Parse(_itemsList[_sourceIndex].gameObject.name);
		int realTargetIndex = int.Parse(_itemsList[_targetIndex].gameObject.name);
		
		_scrollView.restrictWithinPanel = true;
		UIWidget movedWidget = _itemsList[_sourceIndex];
		Vector3 _offset = new Vector2(_sign * _posParam.x * _posParam.z*(-1),_sign * _posParam.y * _posParam.w);
		movedWidget.cachedTransform.localPosition = _itemsList[_targetIndex].cachedTransform.localPosition + _offset;
		_itemsList.RemoveAt(_sourceIndex);
		_itemsList.Insert(_targetIndex, movedWidget);
		
		int targetIndex = realSourceIndex > realTargetIndex ? (realTargetIndex - 1) : (realTargetIndex + 1);
		movedWidget.name = targetIndex.ToString();
		int dataIndex = 0;
		if (targetIndex > 0)
		{
			dataIndex = targetIndex % (_dataList.Count);
		}
		else if (targetIndex<0)
		{
			dataIndex = _dataList.Count - (Mathf.Abs(targetIndex) % (_dataList.Count) == 0 ? _dataList.Count: Mathf.Abs(targetIndex) % (_dataList.Count));
		}
		SetInfo(movedWidget,_dataList[dataIndex]);
		
		if(dataIndex==_maxCount-1)//判断是否已经滑到物品列表的底部
		{
			_canSlideToRight=true;
		}
		else
		{
			_canSlideToRight=false;
		}
		if(dataIndex==_startIndex) //判断是否已经滑到物品列表的顶部
		{
			_canSlideToleft=true;
		}
		else
		{
			_canSlideToleft=false;
		}
	}
}

2, 外部调用函数

	BackpackLoopScrollView  _loopsScrollView;
	UIScrollView            _scrollView;
	List>        allResources; 
	private void BuildItemList(List value)
	{
		int limitItemNum=10;     //物品栏个数
		int maxCount = value.Count;
		allResources.Clear();    //清楚临时物品数据列表
		for (int k = 0; k (){value[k],value[k+1]});
			}
			else if(k==maxCount-1)
			{
				allResources.Add(new List(){value[k],null});
			}
		}
		_loopsScrollView.DataList=allResources;
		List widgets=_loopsScrollView.itemList;
		if(widgets.Count==0)
		{
			widgets=new List();
			for(int i=0;i());
			}
			_grid.repositionNow=true;
			_loopsScrollView.Init(widgets);
		}
		_loopsScrollView.UpdateListItem(allResources.Count);
		_scrollView.localPosition=new Vector3(0f,0f,0f);
		_scrollView.GetComponent().clipOffset=new Vector2(0f,0f);
	}

3,效果图

NGUI之背包系统_第1张图片


左右滑动,





你可能感兴趣的:(unity3d)