Unity背包系统学习日志

Unity背包系统学习日志

Unity背包系统一直是我希望实现的一个功能,故查阅了很多资料,也看了很多文章,最终使用ScriptableObject来实现背包数据的存储。

人物移动脚本编写(PlayerController.cs)

此部分比较简单,只是编写一个基础的移动脚本供测试使用。
其中还包括了简单的开关背包的代码OpenMyBag()。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public Rigidbody rb;
    public GameObject myBag;

    public bool openBag;

    private void Awake()
    {
        myBag.SetActive(true);
        myBag.SetActive(false);
    }

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMove();
        OpenMyBag();
    }

    public void PlayerMove()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        rb.velocity = new Vector3(x * speed, rb.velocity.y, z * speed);
    }
    public void OpenMyBag()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            if (!openBag)
            {
                openBag = true;
                myBag.SetActive(true);
            }
            else
            {
                openBag = false;
                myBag.SetActive(false);
            }
        }
    }
}

创建物体

在此编写脚本实现快捷创建。
创建出来的item为一个物品,可以存储在背包中。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName ="Item",menuName ="Bag/New Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public int itemNumber;
    public Sprite itemImage;
    [TextArea]
    public string itemInfo;
}

创建背包

在此编写脚本实现快捷创建。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName ="BagData",menuName ="Bag/BagData")] //在Unity菜单创建背包
public class BagData : ScriptableObject
{
    public List<Item> itemList = new List<Item>(); //背包中的物品列表
}

制作物品预制体

创建一个预制体,其中包含一个图像以及一个文本。
实际上就是背包界面中显示的物体。
其中组件Grid只是为了方便图片与文本管理,代码附于下方。
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Grid : MonoBehaviour
{
    public Image gridImage;
    public Text girdNumber;
}

制作场景中可以被拾取的物品

在此制作一个名为Apple的物品。
首先创建一个Cube代替。
在这里插入图片描述
在Apple上创建一个子物体,挂载一个Box Collider组件作为触发器,用来检测Player的进入。
此处Player检测采用的触发器,也就是PickCheck,将其置于PickCheck的Layer,此Layer只能与Player产生碰撞。
在这里插入图片描述
在Apple上挂载PickUp组件,用来检测玩家拾取动作。
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUp : MonoBehaviour
{
    public Item item;
    public BagData bagData;

    public bool canPick = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Pickitem();
    }

    private void OnTriggerEnter(Collider other)
    {
        canPick = true;
    }

    private void OnTriggerExit(Collider other)
    {
        canPick = false;
    }

    public void Pickitem()
    {
        if (canPick && Input.GetKeyDown(KeyCode.E))
        {
            if (!bagData.itemList.Contains(item))
            {
                bagData.itemList.Add(item);
            }
            item.itemNumber += 1;
            BagDisplayUI.UpdateItemToUI();
            Destroy(this.gameObject);
        }
    }
}

背包UI界面显示

现在物体以及储存物体的数据都以及准备好,只需要在UI中显示出来即可。
创建一个MyBag的UI。
其中Grid Layout Group组件为自动排版的组件,方便背包界面的管理。
在这里插入图片描述
Bag Display UI代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BagDisplayUI : MonoBehaviour
{
    static BagDisplayUI bagDisplayUI;
    private void Awake()
    {
        if(bagDisplayUI != null)
        {
            Destroy(this);
        }
        bagDisplayUI = this;
    }

    public BagData bagData;
    public Grid gridPrefab;
    public GameObject myBag;

    private void OnEnable()
    {
        UpdateItemToUI();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public static void InsertItemToUI(Item item) //创建单个背包物品
    {
        Grid grid = Instantiate(bagDisplayUI.gridPrefab, bagDisplayUI.myBag.transform);
        grid.gridImage.sprite = item.itemImage;
        grid.girdNumber.text = item.itemNumber.ToString(); 
    }

    public static void UpdateItemToUI() //每次PickItem()执行,首先删除背包中所有物品,再添加背包列表中的所有物品
    {
        for(int i = 0; i < bagDisplayUI.myBag.transform.childCount; i++)
        {
            Destroy(bagDisplayUI.myBag.transform.GetChild(i).gameObject);
        }
        for (int i = 0; i <bagDisplayUI.bagData.itemList.Count; i++)
        {
            InsertItemToUI(bagDisplayUI.bagData.itemList[i]);
        }
    }
}

效果展示

在这里插入图片描述

参考文章

https://blog.csdn.net/xinzhilinger/article/details/113696369

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值