图片描述
特别说明:UI框架的作用只是为了方便管理,和使得整个结构清晰明了。窗体封装了打开与关闭的公有方法,并无涉及到窗体具体的特有功能,所以这里不讲框架并不会造成做不出背包的问题。
UI_PacksackForm.cs(脚本说明)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyUI;
using System;
using UnityEngine.EventSystems;
public class UI_PacksackForm : BaseUIForm,UnityEngine.EventSystems.IBeginDragHandler,
UnityEngine.EventSystems.IDragHandler, UnityEngine.EventSystems.IEndDragHandler{
/*属性字段*/
public List<UI_PackCell> DrugCells; //这个用来存储药品类所有小格子的列表
public List<UI_PackCell> EquipCells;
public List<UI_PackCell> SkillCells;
public List<UI_PackCell> TaskCells;
private Vector3 _offsetPointerToPack; //这个鼠标指针点击到背包窗体的中心点的偏移
private UnityEngine.UI.Button _Btn_ClearUp; //这个是整理按钮
private UnityEngine.UI.Button _Btn_Close; //关闭按钮
private UnityEngine.UI.ScrollRect _ScrollView; //这个是ScrollView
private UnityEngine.UI.Dropdown _DpDn_BackSelect;//背包类型的下拉选择菜单
public RectTransform DrugContent; //这是4个不同种类的背包
public RectTransform EquipContent;
public RectTransform SkillContent;
public RectTransform TaskContent;
private ItemsType _itemsType = ItemsType.Drug; //背包当前选择的物品种类
#region 拖拽
///开始拖拽
void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
{
Debug.Log("开始拖拽背包");
Vector3 worldPosition;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(),
eventData.position, eventData.pressEventCamera, out worldPosition))
{
_offsetPointerToPack = worldPosition - transform.GetComponent<RectTransform>().position;
}
}
///拖拽中
void IDragHandler.OnDrag(PointerEventData eventData)
{
Vector3 worldPosition;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(),
eventData.position, eventData.pressEventCamera, out worldPosition))
{
transform.GetComponent<RectTransform>().position = worldPosition - _offsetPointerToPack;
}
}
///拖拽结束
void IEndDragHandler.OnEndDrag(PointerEventData eventData)
{
Debug.Log("结束背包拖拽");
}
#endregion
/*公有方法*/
/// <summary>
/// 用以注册下拉选项的事件
/// </summary>
/// <param name="num"></param>
public void onValueChange(Int32 num)
{
switch(num)
{
case 0:
{
DrugContent.gameObject.SetActive(true);
EquipContent.gameObject.SetActive(false);
SkillContent.gameObject.SetActive(false);
TaskContent.gameObject.SetActive(false);
_ScrollView.content = DrugContent;
_itemsType = ItemsType.Drug;
}
break;
case 1:
{
DrugContent.gameObject.SetActive(false);
EquipContent.gameObject.SetActive(true);
SkillContent.gameObject.SetActive(false);
TaskContent.gameObject.SetActive(false);
_ScrollView.content = EquipContent;
_itemsType = ItemsType.Equip;
}
break;
case 2:
{
DrugContent.gameObject.SetActive(false);
EquipContent.gameObject.SetActive(false);
SkillContent.gameObject.SetActive(true);
TaskContent.gameObject.SetActive(false);
_ScrollView.content = SkillContent;
_itemsType = ItemsType.Skil;
}
break;
case 3:
{
DrugContent.gameObject.SetActive(false);
EquipContent.gameObject.SetActive(false);
SkillContent.gameObject.SetActive(false);
TaskContent.gameObject.SetActive(true);
_ScrollView.content = TaskContent;
_itemsType = ItemsType.Other;
}
break;
}
}
/// <summary>
/// 用以注册整理按钮的事件
/// </summary>
public void CleanUp()
{
List<UI_PackCell> temp;
switch (_itemsType)
{
case ItemsType.Drug:
{
temp = DrugCells;
}
break;
case ItemsType.Equip:
{
temp = EquipCells;
}
break;
case ItemsType.Skil:
{
temp = SkillCells;
}
break;
case ItemsType.Other:
{
temp = TaskCells;
}
break;
default:
temp = new List<UI_PackCell>();
break;
}
if (temp.Count > 0)
for (int i = 0; i < temp.Count; ++i)
{
for(int j = i+1; j < temp.Count; ++j)
{
if(temp[i].getGoodsId()<temp[j].getGoodsId()&& temp[j].getGoodsId()>0)
{
temp[i].Exchange(temp[j]);
}
}
}
}
// Use this for initialization
void Awake () {
_DpDn_BackSelect = transform.FindChild("DpDn_PackSelect").GetComponent<UnityEngine.UI.Dropdown>();
_ScrollView = transform.FindChild("ScrollView").GetComponent<UnityEngine.UI.ScrollRect>(