unity3D游戏开发中如何用UGUI制作背包

来源:互联网 发布:linux下关闭防火墙 编辑:程序博客网 时间:2024/05/19 19:14

在进行unity3D游戏开发的过程中,背包系统是必不可少的。背包系统在网上有很多的制作方法,下面我们就来讲一下最快速的:用UGUI制作背包系统。

  今天,给大家介绍一下如何在unity3D游戏开发中利用UGUI快速的去制作背包系统。

  首先,我们创建一个新场景,添加一个Image 做为背包的背景图片,在Image下添加一个空物体,在空物体下添加两个Button按钮,Text文字(物品栏)。

unity3D游戏开发中如何用UGUI制作背包

  在空物体容器上,添加组件Grid Layout Group用于自动排列Button 按钮。并把Button按钮下的Text 删除。

  把Button上的image图片更改成我们想要的物品图片。

  添加一个Image 图片,做为关闭这个背包框。在这个图片上添加组件 Toggle , 并把总的背包背景图片拉到 Target Graphic 位置上。

unity3D游戏开发中如何用UGUI制作背包

  其次,我们进行添加人物图片上的物品制作。添加一个Image , 并添加一个子image 做为物品的存放位置,放在人物身上的小方框内,并调整好大小位置

unity3D游戏开发中如何用UGUI制作背包

  最后,写代码,给每个物品栏上的Button 添加上脚本,脚本代码如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.EventSystems;public class wuPinBeiBao : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler{ // 引用canvas 一会坐标要用public Canvas cavs;// 拖动物品时,在鼠标下面会有一个物品跟随private GameObject m_DraggingIcon;// 放下物品的位置private RectTransform m_DraggingPlane;// Use this for initializationvoid Start () {// 重新生成物品角度方向和原来方向角度相同m_DraggingPlane = cavs.transform as RectTransform;}// Update is called once per framevoid Update (){}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();CanvasGroup group = m_DraggingIcon.AddComponent();group.blocksRaycasts = false;image.sprite = GetComponent().sprite;/// 生成的物品不影响射线检测image.SetNativeSize();if (m_DraggingIcon != null){var rt = m_DraggingIcon.GetComponent();Vector3 globalMousePos;if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, eventData.position, eventData.pressEventCamera, out globalMousePos)){rt.position = globalMousePos;rt.rotation = m_DraggingPlane.rotation;}}}public void OnDrag(PointerEventData eventDate){Debug.Log("OnDrag: 拖动中事件:"+ eventDate.position);if(m_DraggingIcon != null){var rt = m_DraggingIcon.GetComponent();Vector3 globalMousePos;if(RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane,eventDate.position,eventDate.pressEventCamera, out globalMousePos)){rt.position = globalMousePos;rt.rotation = m_DraggingPlane.rotation;}}}public void OnEndDrag(PointerEventData eventData){Debug.Log("OnEndDrag: 拖动结束事件" + eventData.position);if (m_DraggingIcon != null){Destroy(m_DraggingIcon);}}}

  把脚本放在Button上,并做好关联工作,如图所示:

unity3D游戏开发中如何用UGUI制作背包

  下面,我们要给每个人物身上的Image(做为存放物品的) ,添加脚本,脚本如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.EventSystems;public class Beibaofangzhi : MonoBehaviour,IDropHandler {public Sprite Rkkk;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}public void OnDrop(PointerEventData data){var originalObj = data.pointerDrag;if (originalObj == null){return;}var srcImage = originalObj.GetComponent();if (srcImage == null){return;}GetComponent().sprite = data.pointerDrag.GetComponent().sprite;}public void OnPointerExit(PointerEventData eventData){GetComponent().color = Color.red;}}

  完成之后,运行程序,拖动中,我们可以看到效果

unity3D游戏开发中如何用UGUI制作背包

  拖动结束,可以看到效果

unity3D游戏开发中如何用UGUI制作背包

  至此,背包系统功能完成。

0 0
原创粉丝点击