Unity UGUI背包系统 之 创建背包对应数据类
Unity UGUI背包系统 之 创建背包对应数据类。游戏中,背包的内容有武器、消耗品、装备等数据,他有有工程的一些属性,也有特殊的属性。本节介绍根据背包的特性,创建背包数据类的方法,具体如下
工具/原料
Unity
UGUI
UGUI背包系统 之 创建背包对应数据类
1、打开Unity,新建议一个空工程,具体如下图


4、“Item”脚本的具体内容如下:using System.Collections;using System.Collections.G髫潋啜缅eneric;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”,具体代码和代码说明如下图

8、“Consumable”脚本的具体内容如下using System.Collections;using System.Co造婷用痃llections.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”,具体代码和代码说明如下图

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