惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - mcwind

Unity开发IOS游戏的优化建议 Unity3d 如何使用程序集(续) Unity 几个Mesh特效的例子 Unity动态载入可执行代码的方法 Unity几个有用的游戏运动特效 Unity——Export/Import Package功能和项目管理的研究 三维游戏引擎——Unity 角色模型优化要点续:换肤和导入 三维游戏引擎——Unity 角色模型优化要点 3D游戏引擎——Unity 如何绘制反面 3D游戏引擎——Unity中网格合并示例研究 资源:三维CAD二次开发网站 [收藏]你想成为一名游戏策划吗? [收藏]学习:游戏策划入门 如何让你开发的游戏赚钱 如何更改MapleTr登录页面和标题 Some Useful Articles Of TDD (Test Driven Development) On MSDN 第一次试用Live Writer手记 收藏:航空航天院所 倒腾sql2005数据表内容的方法 - mcwind - 博客园
Unity动态载入文本数据的方法归类
mcwind · 2011-03-25 · via 博客园 - mcwind

概述

在游戏中经常需要动态地载入一些数据。例如让玩家定制角色外貌时,需要从数据文件读取各种身体部位的信息;或者玩家访问NPC购买装备时,需要从数据文件读取装备信息等等。为了减少游戏程序的大小,我们往往采用最简单的文本文件来保存这些信息。所以本文总结一下Unity常用的读取文本数据的方法。

因为我是边开发项目便记录心得,时间很仓促,只能先把关键点记录下来,留待以后有时间再补全吧。对于没有详细说的内容,一般google一下都能查到更详细的说明。

让Text支持中文

读取TextAsset数据

在开发阶段,将Txt文件放置到Unity项目Asset文件目录下,Unity就会识别该文件成为TextAsset。Unity脚本中专门有TextAsset类来操作文本中的数据。

有时候你可能会发现文本中的中文汉字无法显示,这是因为TextAsset只支持UTF-8的缘故。可以用写字板将该txt文件重新存为UTF-8格式即可解决。

TextAsset和其他类型Asset一样,可以拖动到组件面板中用于赋值,我称之为静态载入。如果要在游戏运行中实现动态载入TextAsset,就必须采用Resource.Load()方法,或者用AssetBundle来实现。

需要理解的是,TextAsset是随游戏项目一起编译的。在最终的游戏程序中,是看不到原先的txt文件的。如果想在游戏运行时动态读取一个独立的txt文件数据,就要用下面的“读取外部文本数据”方法。

读取外部文本数据

对于windows平台上的游戏而言,运行中读取外部文本文件的方法非常简单,就是传统的文件读写操作。例如可以用.net的StreamReader类来实现。

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                String line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}