Unity UGUI背包系统 之 创建背包对应数据类

2025-11-20 01:13:11

1、打开Unity,新建议一个空工程,具体如下图

Unity UGUI背包系统 之 创建背包对应数据类

2、在工程里面,新建四个脚本,分别命名为“Item”、“Weapon”、“Comsumable”以及“Armor”,双击“Item”或者右键“Open C# Project”打开脚本,具体如下图

Unity UGUI背包系统 之 创建背包对应数据类

Unity UGUI背包系统 之 创建背包对应数据类

3、在打开“Item”脚本上编写代码,首先“Item”作为基类,该类设置背包系统所有物品共有的一些属性变量,以及继承自雷的Item类型,然后在构造函数中赋值属性的值或内容,具体代码和代码说明如下图

Unity UGUI背包系统 之 创建背包对应数据类

4、“Item”脚本的具体内容如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Item { 

    public int Id { get; private set; }

    public string Name { get; private set; }

    public string Description { get; private set; }

    public int BuyPrice { get; private set; }

    public int SellPrice { get; private set; }

    public string Icon { get; private set; }

    public string ItemType { get; protected set; }

    public Item(int id, string name, string description, int buyPrice,

        int sellPrice, string icon) {

        this.Id = id;

        this.Name = name;

        this.Description = description;

        this.BuyPrice = buyPrice;

        this.SellPrice = sellPrice;

        this.Icon = icon;

    }

}

5、在打开“Weapon”脚本上编写代码,首先继承“Item”类,然后设置武器类的特有属性攻击Damage,然后在构造函数中赋值特有属性值,以及ItemType为“Weapon”,具体代码和代码说明如下图

Unity UGUI背包系统 之 创建背包对应数据类

6、“Weapon”脚本的具体内容如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Weapon : Item {

    

    public int Damage { get; private set;}

    public Weapon(int id, string name, string description,

        int buyPrice, int sellPrice, string icon, int damage) :

        base(id, name, description, buyPrice, sellPrice, icon)

    {

        this.Damage = damage;

        base.ItemType = "Weapon";

    }

}

7、在打开“Consumable”脚本上编写代码,首先继承“Item”类,然后设置消耗品类的特有属性攻击BackHP和BackMp,然后在构造函数中赋值特有属性值,以及ItemType为“Consumable”,具体代码和代码说明如下图

Unity UGUI背包系统 之 创建背包对应数据类

8、“Consumable”脚本的具体内容如下

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Consumable : Item

{

    public int BackHp { get; private set; }

    public int BackMp { get; private set; }

    public Consumable(int id, string name, string description, int buyPrice,

        int sellPrice, string icon, int backHp, int backMp) :

        base(id, name, description, buyPrice, sellPrice, icon)

    {

        this.BackHp = backHp;

        this.BackMp = backMp;

        base.ItemType = "Consumable";

    }

}

9、在打开“Armor”脚本上编写代码,首先继承“Item”类,然后设置消耗品类的特有属性攻击Power、Defend和Agility,然后在构造函数中赋值特有属性值,以及ItemType为“Armor”,具体代码和代码说明如下图

Unity UGUI背包系统 之 创建背包对应数据类

10、“Armor”脚本的具体内容如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Armor : Item

{

    public int Power { get; private set; }

    public int Defend { get; private set; }

    public int Agility { get; private set; }

    public Armor(int id, string name, string description, int buyPrice,

        int sellPrice, string icon, int power, int defend, int agility) : 

        base(id, name, description, buyPrice, sellPrice, icon)

    {

        this.Power = power;

        this.Defend = defend;

        this.Agility = agility;

        base.ItemType = "Armor";

    }

}

11、脚本编译正确后,回到Unity界面,在工程中新建一个文件夹“Scripts”统一管理脚本,把四个脚本一道“Scripts”文件夹下,具体如下图

Unity UGUI背包系统 之 创建背包对应数据类

12、到此,《Unity UGUI背包系统 之 创建背包对应数据类》讲解结束,谢谢

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢