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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 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);
        }
    }
}