• 转_读取外部数据


    概述
    在游戏中经常需要动态地载入一些数据。例如让玩家定制角色外貌时,需要从数据文件读取各种身体部位的信息;或者玩家访问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);
            }
        }
    }
    
    //////////////////////////////////

    using UnityEngine;

    using System.Collections;

    using System;

    using System.IO;

    public class Test : MonoBehaviour {

           void Start () {  

                                   try {            

                                             using (StreamReader sr = new StreamReader("c:\\log.txt"))            

                                             {

                                                     String line;

                                                     while ((line = sr.ReadLine()) != null)

                                                    {

                                                            print (line);

                                                    }

                                            }

                                      }

                                     catch (Exception e)

                                    {

                                               Console.WriteLine("The file could not be read:");

                                               Console.WriteLine(e.Message);

                                   }  

                     }

    }

      

  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/softimagewht/p/2638389.html
Copyright © 2020-2023  润新知