最近也是跟着玩玩UGUI背包系统,简单的写了一下。希望对初学者有所帮助。
首先就是Unity界面的搭建(这里我用的是网上找到图片):
如上图所示:
随便找一个游戏背景图片(我的是BG),BG下面布局好Text,内容为“物品栏”,然后在创建一个Image,下面是物品栏里里面的装备图片,如下图:
然后创建Scroll View,下面的组件Content,设置如下(别忘记添加Grid layput Group):
Prefabwuqi 是用Button做的预制体:
接下俩就是代码(这段代码添加到人物物品栏,也就是上面的rw下面的图片都要添加这个脚本):
public class Beibaofangzi : MonoBehaviour, IDropHandler
{
public Transform myTR;
public Sprite myImage;
// Use this for initialization
void Start()
{
// MyBag();
}
// Update is called once per frame
void Update()
{
}
//void MyBag()
//{
// foreach (Transform child in myTR.GetComponentsInChildren<Transform>(true))
// {
// }
//}
public void OnDrop(PointerEventData data)
{
var originalObj = data.pointerDrag;
if (originalObj == null)
{
return;
}
var srcImage = originalObj.GetComponent<Image>();
if (srcImage == null)
{
return;
}
// Debug.LogError(data.pointerDrag.GetComponent<Image>().sprite.name);
// Debug.Log(myImage.name);
if (data.pointerDrag.GetComponent<Image>().sprite.name == myImage.name)
{
GetComponent<Image>().sprite = data.pointerDrag.GetComponent<Image>().sprite;
}
else
{
return;
}
// GetComponent<Image>().sprite = data.pointerDrag.GetComponent<Image>().sprite;
}
}
实例化代码(添加到Content上):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Instan : MonoBehaviour {
public GameObject Prefab;
public Transform ContTran;
public Dictionary<int, GameObject> dic = new Dictionary<int, GameObject>();
public Text MyText;
public static Instan instance;
public GameObject instate;
// Use this for initialization
void Awake()
{
instance = this;
}
void Start () {
Int();
}
// Update is called once per frame
void Update () {
}
void Int()
{
for (int i =0 ; i < 12; i++)
{
Sprite sp = Resources.Load<Sprite>(i.ToString());
instate = Instantiate(Prefab, ContTran) as GameObject;
instate.GetComponent<Image>().sprite = sp;
instate.gameObject.SetActive(true);
instate.name=sp.name;
// Debug.LogError(instate.name);
StoreKey(i,instate);
MyText.text = (i+1).ToString();
}
}
public void StoreKey(int a,GameObject value)
{
dic.Add(a, value);
foreach (KeyValuePair<int, GameObject> pair in dic)
{
Debug.LogError(pair.Key + " " + pair.Value);
}
}
}
物品代码(放到预制体上)别忘记在MonoBehaviour后添加引用 IBeginDragHandler, IDragHandler, IEndDragHandler:
public class wuPinBeiBao : MonoBehaviour
{ // 引用canvas 一会坐标要用
public Canvas cavs;
// 拖动物品时,在鼠标下面会有一个物品跟随
private GameObject m_DraggingIcon;
// 放下物品的位置
private RectTransform m_DraggingPlane;
// Use this for initialization
void Start()
{
// 重新生成物品角度方向和原来方向角度相同
m_DraggingPlane = cavs.transform as RectTransform;
}
// Update is called once per frame
void Update()
{
}
public void OnClick(GameObject name)
{
name.name=name.GetComponent<Text>().text;
// Debug.LogError(name.name);
}
public void OnBeginDrag(PointerEventData eventData)
{
// Debug.Log("OnBeginDrag: 拖动事件:" + eventData.position);
m_DraggingIcon = new GameObject("icon");
//public void SetParent(Transform parent, bool worldPositionStays);设置变换的父。设置生成的这个物体它的父亲是cavs.transform false坐标和父物体坐标一置
m_DraggingIcon.transform.SetParent(cavs.transform, false);
//将变换移到局部变换列表的结尾。生成的物体做为Cavas的最后一个子物体。
m_DraggingIcon.transform.SetAsLastSibling();
var image = m_DraggingIcon.AddComponent<Image>();
// Debug.LogError(image);
CanvasGroup group = m_DraggingIcon.AddComponent<CanvasGroup>();
group.blocksRaycasts = false;
image.sprite = eventData.pointerDrag.GetComponent<Image>().sprite;
/// 生成的物品不影响射线检测
// image.SetNativeSize();
if (m_DraggingIcon != null)
{
var rt = m_DraggingIcon.GetComponent<RectTransform>();
Vector3 globalMousePos;
if(RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane,eventData.position, eventData.pressEventCamera, out globalMousePos))
{
rt.position = globalMousePos;
rt.rotation = m_DraggingPlane.rotation;
}
}
}
internal string OnClick(string name)
{
throw new NotImplementedException();
}
public void OnDrag(PointerEventData eventDate)
{
// Debug.Log("OnDrag: 拖动中事件:" + eventDate.position);
if (m_DraggingIcon != null)
{
var rt = m_DraggingIcon.GetComponent<RectTransform>();
Vector3 globalMousePos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, eventDate.position, eventDate.pressEventCamera,out globalMousePos))
{
rt.position = globalMousePos;
rt.rotation = m_DraggingPlane.rotation;
}
}
}
}
好了 基本没有什么大问题,就是需要自己添加一个方法去Destroy掉物品就可以了。
效果图(装备栏和物品栏是对应的,比如武器只能放到武器栏,其他栏是放不进去的):
至于装备显示的文本信息大家可以根据自己需要填写