Unity UGUI背包系统 之 创建背包对应数据类
Unity UGUI背包系统 之 创建背包对应数据类。游戏中,背包的内容有武器、消耗品、装备等数据,他有有工程的一些属性,也有特殊的属性。本节介绍根据背包的特性,创建背包数据类的方法,具体如下
工具/原料
Unity
UGUI
UGUI背包系统 之 创建背包对应数据类
1、打开Unity,新建议一个空工程,具体如下图
2、在工程里面,新建四个脚本,分别命名为“Item”、“Weapon”、“Comsumable”以及“Armor”,双击“Item”或者右键“Open C# Project”打开脚本,具体如下图
3、在打开“Item”脚本上编写代码,首先“Item”作为基类,该类设置背包系统所有物品共有的一些属性变量,以及继承自雷的Item类型,然后在构造函数中赋值属性的值或内容,具体代码和代码说明如下图
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”,具体代码和代码说明如下图
6、“Weapon”脚本的具体粝简肯惧内容如下:using System.Collections;using System.Collections.Generic;using Unit烤恤鹇灭yEngine;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”,具体代码和代码说明如下图
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”,具体代码和代码说明如下图
10、“Armor”脚本的具体内间恒溪痞容如下:using System.Collections;using System.Col造婷用痃lections.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”文件夹下,具体如下图
12、到此,《Unity UGUI背包系统 之 创建背包对应数据类》讲解结束,谢谢