今天主要讲下我在使用Unity3D过程中,读取文件数据时一些浅显的经验,肯定有不少问题。
游戏开发过程中,如果是单机游戏,一般不包含数据库,这样导致稍微复杂的游戏将会包含大量的数据信息,包括物品,剧情,技能,人物信息等,这些数据得存储一般都会以一个外部文件进行存储,可以是Json,Xml,Excel,Txt等,这样方便管理游戏中大量的数据。这些数据将在运行游戏的时候进行读取。如果只是读取数据,那么Txt文本是不错的选择,因为Unity源生对txt支持较好,可以在组件上直接拖入。而要像数据库一样,对这些数据进行增删改查的话Json和Xml是更好的选择。
这次主要讲下读取txt文本。
数据读取步骤
1.素材准备
先准备一个txt文件,我这用了一个Excel文档(来源:金庸群侠传X Mod编辑器)转成txt,因为个人感觉用excel管理数据,清晰明了,如果能直接读取当然更好,但是unity本身对其的支持不是很好,虽然也可以通过插件读取,但是或多或少还有些问题。excel表先另存为转成逗号分隔符的csv格式,然后直接强制转成txt。
附:excel文件地址 http://pan.baidu.com/s/1geZvqUJ
2.Unity中操作
1.Hierarchy面板新建一个空的游戏物体,起名GameManager。
2.Project面板新建四个文件夹备用,Scripts文件夹下,新建C#脚本InventoryInfoLoadManager和Inventory脚本,名称随意。
3.将脚本挂载到GameManager上。
3.读取代码
打开脚本Inventory,只读取一部分信息,编写如下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory
{
//属性定义
private string name;
private string des;
private string talent;
//封装
public string Name
{
get { return name; }
set { name = value; }
}
public string Des
{
get { return des; }
set { des = value; }
}
public string Talent
{
get { return talent; }
set { talent = value; }
}
}
打开脚本InventoryInfoLoadManager脚本,编写如下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryInfoLoadManager : MonoBehaviour
{
//单例模式
//public static InventoryInfoLoadManager _instance;
//也可以用Resources.Load读取,我这就直接通过Inspector面板拖入
public TextAsset inventoryInfo;
//List集合存储所有物品
public List<Inventory> list= new List<Inventory>();
//void Awake()
//{
// _instance = this;
//}
//Start方法中读取物品信息
void Start()
{
ReadInfo();
foreach (Inventory inventory in list)
{
print(inventory.Name);//输出到控制台测试
}
}
//信息读取方法
void ReadInfo()
{
string allInfo = inventoryInfo.text;
string[] lineInfoArray = allInfo.Split('\n');
foreach (string lineInfo in lineInfoArray)
{
string[] itemInfoArray = lineInfo.Split(',');
if (itemInfoArray[0] == "物品名")
{
}
else
{
string name = itemInfoArray[0];
string des = itemInfoArray[1];
string talent = itemInfoArray[3];
Inventory it= new Inventory();
it.Name = name;
it.Des = des;
it.Talent = talent;
list.Add(it);
}
}
}
}
写完代码把txt文件拖到脚本InventoryInfoLoadManager上,确保txt文本没有多余空格,且是utf-8格式,之后在控制台就能显示读取数据。到这就结束了,下面是我自己继续将信息显示到游戏物体之上,方法不好。
4.界面显示
首先将InventoryInfoLoadManager中注释的单例模式取消注释,再新建两个类,InventoryManager和InventoryItem,Hierarchy面板新建grid(grid上添加GridLayouGroup脚本)用来存放物品,和item物体(item物体添加InventoryItem脚本)用来存放物品信息,get按钮添加点击事件,InventoryManager拖到GameManager游戏物体上具体如下图:
InventoryItem脚本如下:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryItem : MonoBehaviour
{
//UI相关
public Text name;
public Text des;
public Text talent;
public void UpdateInfo(string name,string des,string talent)
{
this.name.text = name;
this.des.text = des;
this.talent.text = talent;
}
}
InventoryManager脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour
{
//网格游戏物体
public GameObject grid;
//prefab
public GameObject inventoryItemPrefab;
//获取list中所有物品
private List<Inventory> list;
public void OnClickLoadInventory()
{
list = InventoryInfoLoadManager._instance.list;
foreach (Inventory inventory in list)
{
inventoryItemPrefab.GetComponent<InventoryItem>().UpdateInfo(inventory.Name,inventory.Des,inventory.Talent);
GameObject iip = GameObject.Instantiate(inventoryItemPrefab) as GameObject;
iip.transform.parent = grid.transform;
}
}
}
Inspector面板如下图:
最终结果图:
写完了,好艰难。。