UGui背包系统(实例化装备并且给角色换装,实现不同的装备放入相应的人物物品栏中)

最近也是跟着玩玩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掉物品就可以了。

效果图(装备栏和物品栏是对应的,比如武器只能放到武器栏,其他栏是放不进去的):

至于装备显示的文本信息大家可以根据自己需要填写

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity中使用UGUI实现装备合成树的步骤如下: 1. 设计装备合成树的结构:装备合成树是一个有层级关系的树形结构,每个节点代表一个装备,节点之间通过合成关系连接。可以使用脚本或者数据结构来定义装备节点和合成关系。 2. 创建UI界面:在Unity中创建一个Canvas对象,并添需要的UI元素,比如按钮、文本等,用来显示装备合成树。 3. 绘制节点:使用UGUI提供的Button或者Image等UI组件,绘制每个装备节点的标,并设置对应的合成信息和按钮事件等。 4. 布局节点:根据装备合成树的结构,将装备节点按照层级和位置进行布局,可以使用UGUI中的Layout组件来实现自动布局。 5. 添交互:为每个装备节点的按钮添事件监听,当点击某个装备节点时,根据节点的合成信息刷新树的显示内容,比如显示合成材料和合成结果等。 6. 更新合成树:当成功合成某个装备后,需要更新装备合成树的结构和显示内容,可以使用脚本来实现树的动态更新。 7. 状态管理:根据游戏逻辑和玩家行为,需要考虑装备合成树节点的状态管理,比如显示已拥有的装备节点和未解锁的装备节点等。 8. 界面优化:为了提升用户体验,可以考虑添特效、动画和过渡效果等,使装备合成树的界面更活跃和吸引人。 通过上述步骤,在Unity中使用UGUI可以实现一个功能完善的装备合成树界面,为玩家提供更好的游戏体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值