课时41:在程序中读取文本,将物品信息读入内存
1. TextAsset类
文本类,变量bytes读取文本文件的字节,text读取文本文件的字符串
详解:https://docs.unity3d.com/ScriptReference/TextAsset.html
2.ObjectsInfo类
(1).创建TextAsset的公有变量 objectsInfoListText,将txt文件赋值给该共有变量,开始解析文本文件内容,按照行读取物品的信息
using System.Collections.Generic;//包含字典类 字典存储的是键值对
//读取物品信息函数
void ReadInfo() {
string text = objectsInfoListText.text;//取到文本文件所有字符
string[] strArray = text.Split('\n');//字符串数据,根据换行符拆分每一行
//遍历字符串数组,即遍历每一行
foreach (string str in strArray) {
//解析单个一行
string[] proArray = str.Split(',');//根据逗号拆分每行物品的属性
ObjectInfo info = new ObjectInfo();//创建ObjectInfo类的对象info
int id = int.Parse(proArray[0]);//物品ID,字符串转换成int类型
string name = proArray[1];//物品名称
string icon_name = proArray[2];//ICON名称
string str_type = proArray[3];//物品类型
ObjectType type = ObjectType.Drug;//根据物品的不同取出不同的类型
switch (str_type) {
case "Drug"://药品
type = ObjectType.Drug;
break;
case "Equip"://装备
type = ObjectType.Equip;
break;
case "Mat"://材料
type = ObjectType.Mat;
break;
}
info.id = id; info.name = name; info.icon_name = icon_name;
info.type = type;
//根据类型取出不同的属性
if (type == ObjectType.Drug) {//当前物品类型为药品
int hp = int.Parse(proArray[4]);//血量
int mp = int.Parse(proArray[5]);//魔法值
int price_sell = int.Parse(proArray[6]);//售价
int price_buy = int.Parse(proArray[7]);//购买价
info.hp = hp; info.mp = mp;
info.price_buy = price_buy; info.price_sell = price_sell;
}
}
}
(2)创建字典
//字典,存储的是键值对(int , objectinfo)
private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int, ObjectInfo>();
// ReadInfo()方法中foreach循环中添加
objectInfoDict.Add(id, info);//物品信息添加到字典中,id为key,可以很方便的根据id查找到这个物品信息。
(3)提供根据Id得到物品信息的方法,从字典中取出来
//根据ID得到物品信息
public ObjectInfo GetObjectInfoById(int id) {
ObjectInfoinfo=null;//定义 ObjectInfo对象
objectInfoDict.TryGetValue(id, out info);//得到物品信息,得到返回真
return info;
}
课时42:设计背包系统的UI界面
1.主界面背景:新建一个sprite,选择相应图片,命名为Inventory,背包系统
2.格子:在Inventory下创建sprite,选择响应的图片,点击Snap保持图片的大小-,命名Inventory-item-grid,并拖入Prefabs作为模板。
(1)Prefabs
当制作好了游戏组件(场景中的任意一个gameobject),我们希望将它制作成一个组件模版,用于批量的套用工作,例如说场景中本质上“重复”的东西,“敌人”,“士兵”,“子弹”这里说本质是因为默认生成的prefab其实和模版是一模一样的,就像是克隆体,但生成的位置和角度以及生成后的一些属性是允许发生变化的。
3.创建其他网格,依次命名
4.存放金币
创建sprite,选择金币的图片;命名Coin-label;
创建sprite,设置背景图片,命名Coin-bg;
并在下面创建一个label用来显示物品数量,与背景图片大小一致,设置边距为0,设置字体、字号和默认显示内容,命名为Coin-number;
5.给Inventory添加碰撞器,检测点击行为,使得点击背包页面的时候,游戏人物不会行走。
课时43:控制背包物品的管理
1. Inventory上创建脚本,管理Inventory,单例模式。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public staticInventory _instance;
void Awake() {
_instance = this;
}
}
2.添加动画,控制其显示和隐藏,tween->position.
(1)设置其起始位置
(2)设置播放时间
privateTweenPosition tween;//控制动画的播放时间
Awake()方法中:tween =this.GetComponent<TweenPosition>();
(3)对外提供显示和隐藏的方法
//显示方法
public void Show() {
tween.PlayForward();//播放动画
}
//隐藏方法
public void Hide() {
tween.PlayReverse();//动画反向播放
}
3.管理方格
(1)给每个方格添加脚本InventoryItemGrid
(2)Inventory脚本中
using System.Collections.Generic;
private int coinCount = 1000;//金币数量
//管理方格,InventoryItemGrid为方格上添加的脚本
public List<InventoryItemGrid>itemGridList = new List<InventoryItemGrid>();
public UILabel coinNumberLabel;//管理金币数量的显示
(3)给Inventory的itemGridList赋值;给金币数量赋值
课时44:创建背包系统里面的物品
1.选择第一个小方格,创建sprite,修改尺寸,命名为Inventory-item,并放在Prefabs中
2.给Inventory-item添加脚本InventoryItem,用于管理物品
3.给物品添加可以拖拽功能
(1)添加碰撞器
(2)InventoryItem继承UIDragDropItem
(3)删除InventoryItem的start()和Update()方法
4.监听拖拽事件
重写OnDragDropRelease()方法,判断当前物品是被拖拽到了空网格还是已有物品的网格。
5. Inventory item
private UISprite sprite;
private int id;
void Awake() {
sprite = this.GetComponent<UISprite>();
}
/// <summary>
/// 根据物品ID决定显示的物品
/// </summary>
/// <param name="id">物品ID</param>
public void SetId(int id) {
ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);//得到物品
sprite.spriteName = info.icon_name;//更新显示
}