本实例通过UGUI实现背包的简单功能,物品的拖拽,交换,拖放位置处理,还有针对背包多页面的滑动翻页的处理。
具体的代码如下。
首先是UIItem类,这个背包内各种物体的类:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIItem : MonoBehaviour {
public Text name;
public Image image;
public void Show(string name)
{
this.name.text = name;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragEvent : MonoBehaviour,IBeginDragHandler ,IDragHandler ,IEndDragHandler ,IPointerDownHandler ,IPointerUpHandler {
public RectTransform rectTransform;
public RectTransform rectTransformSlot;
public Transform draggedItemBox;
private Vector2 pointerOffset;
public CanvasGroup canvasGroup;
public Transform grid;
/// <summary>
/// 开始拖拽!
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
SetDraggedPosition(eventData);
}
/// <summary>
/// 设置拖拽开始和结束的位置
/// </summary>
/// <param name="eventData"></param>
private void SetDraggedPosition(PointerEventData eventData)
{
if (rectTransform == null)
return;
if(eventData .button ==PointerEventData .InputButton .Left )
{
//把世界坐标转换为局部坐标
rectTransform.SetAsLastSibling();
transform.SetParent(draggedItemBox);
Vector2 localPointerPosition;
//画布是否接受射线
canvasGroup.blocksRaycasts = false;
if(RectTransformUtility .ScreenPointToLocalPointInRectangle (rectTransformSlot ,Input .mousePosition ,
eventData .pressEventCamera ,out localPointerPosition ))
{
rectTransform.localPosition = localPointerPosition - pointerOffset;
}
}
}
public void OnDrag(PointerEventData eventData)
{
SetDraggedPosition(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
SetDraggedPosition(eventData);
canvasGroup.blocksRaycasts = true;
if(eventData .pointerCurrentRaycast .gameObject .name =="skill")
{
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.position;
}
else if(eventData .pointerCurrentRaycast .gameObject .name =="Text99")
{
this.transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent.parent);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.position;
eventData.pointerCurrentRaycast.gameObject.transform.parent.SetParent(grid);
}
else
{
transform.SetParent(grid);
}
}
public void OnPointerDown(PointerEventData eventData)
{
transform.localPosition = Vector3.one * 0.5f;
}
public void OnPointerUp(PointerEventData eventData)
{
transform.localPosition = Vector3.one;
}
}
最后的对画布和Scroll的类处理:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;
public class ScrollRectHelper : MonoBehaviour ,IBeginDragHandler ,IEndDragHandler {
/// <summary>
/// 滑动速度
/// </summary>
private float smooting = 2;
/// <summary>
/// 每页显示的数目
/// </summary>
private int pageCount = 16;
private ScrollRect sRect;
/// <summary>
/// 总页数
/// </summary>
private float pageIndex;
/// <summary>
/// 是否拖拽结束
/// </summary>
private bool isDrag = false;
/// <summary>
/// 总页数索引比例
/// </summary>
private List<float> listPageValue = new List<float> { 0 };
/// <summary>
/// 滑动目标位置
/// </summary>
float targetPos = 0;
/// <summary>
/// 当前索引位置
/// </summary>
float nowIndex = 0;
public GameObject UIItem;
private int count;
void Awake()
{
sRect = GetComponent<ScrollRect>();
ListPageValueInit();
}
/// <summary>
/// 初始化UIItem
/// </summary>
void Start()
{
for (int i = 0; i < count ; i++)
{
GameObject go = Instantiate(UIItem);
go.name += +i;
go.SetActive(true);
go.transform.localPosition = UIItem.transform.localPosition;
go.transform.parent = UIItem.transform.parent;
go.GetComponent<UIItem>().Show("物品" + i);
}
}
/// <summary>
/// 每页比例
/// </summary>
private void ListPageValueInit()
{
pageIndex = (count / pageCount) - 1;
if(count !=0)
{
for (int i = 1; i < pageIndex ; i++)
{
listPageValue.Add((i / pageIndex));
}
}
}
void Update()
{
if(!isDrag )
{
sRect.horizontalNormalizedPosition = Mathf.Lerp(sRect.horizontalNormalizedPosition, targetPos, Time.deltaTime);
}
}
/// <summary>
/// 开始拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = true;
}
/// <summary>
/// 拖拽结束
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
//获取拖动的值
float tempPos = sRect.horizontalNormalizedPosition;
var index = 0;
//拖动的绝对值
float offset = Mathf.Abs(listPageValue[index] - tempPos);
for (int i = 0; i < listPageValue .Count ; i++)
{
float temp = Mathf.Abs(tempPos - listPageValue[i]);
if(temp <offset )
{
index = i;
offset = temp;
}
}
targetPos = listPageValue[index];
nowIndex = index;
}
#region 如果需要分页,此处为左右分页代码,现有功能不需要,所以注释掉了
//public void BtnLeftGo()
//{
// nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);
// targetPos = listPageValue[Convert.ToInt32(nowindex)];
//}
//public void BtnRightGo()
//{
// nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);
// targetPos = listPageValue[Convert.ToInt32(nowindex)];
//}
#endregion
}
原文: 点击打开链接