• 您的位(wèi)置:首頁 > 新聞動態 > Unity3D

    UNITY3D使用C#腳本的幾種讀寫TXT文(wén)本文件的方法

    2019/11/12      點擊:
    //注意:要讀(dú)取的文本文件TXT的編碼類型要為utf-8,不然會出現中文亂碼或者直接不顯(xiǎn)示,如果是其它編碼方(fāng)式可以把文(wén)件
    //另保存(cún)為UTF-8的格式
    using UnityEngine;
    using System.Collections;
    using System.IO;//用法三的時候需要定義這個
    using System.Text;//法(fǎ)二的(de)時候(hòu)需要使用(yòng)定義(yì)這個
    public class GUTexture : MonoBehaviour {
        GUIText m_GUIText;
        public TextAsset m_TextAsset;             //法一(yī) ,這個需要在unity編輯器進行賦值,把.txt文本文件保存在Asset下(xià)的     Resources文件夾下,然後把.txt文件(jiàn)拖拽過來進行賦值
        TextAsset m_TextAsset1;
        string m_Str;
        string m_FileName;                             //法二要用
        string[] strs;                                        //法二要用
    void Start () {
        m_GUIText = gameObject.GetComponent();               //找到該遊戲物體的GUIText組件,用來顯示(shì)讀取到 的文(wén)本
        m_FileName = "Z800虛擬頭盔說明書鏈接UTF-8.txt";                       //法二要用,要讀(dú)取(qǔ)的文(wén)件名,這個是相對路徑
    }
    //鼠標進入該遊(yóu)戲物(wù)體執行(háng) 
    void OnMouseEnter() {
       m_GUIText.text = Resources.Load("Z800虛擬頭盔說明書鏈接").text;//法一,需要把文本文件保存在Asset文   件夾下的Resources文件夾內
       // ReadFile(m_FileName);//法二
       //m_GUIText.text = m_Str;//把讀取到的內容放到GUIText組件中顯示 // Read();//法三
         //m_GUIText.text =m_Str;//把讀取到的內容放到(dào)GUIText組件中(zhōng)顯示
     }
    //方法二:通過ReadFile(名字自己定義)方法來讀取,傳入的是文件路徑
    void ReadFile(string FileName) {
       strs = File.ReadAllLines(FileName);//讀取文件的所有行,並將數據讀取到定義好的字符數組strs中,一(yī)行存(cún)一(yī)個單元
       for (int i = 0; i < strs.Length; i++)
       {
        m_Str += strs[i];//讀取每一行,並(bìng)連起(qǐ)來
        m_Str += "\n";//每一行末尾換行
        }
    }
    //方法三: 下麵這個是通過文件流來讀取txt文件(jiàn)的方法
    public void Read()
        {
            try
            {
                string pathSource = m_FileName;
                using (FileStream fsSource = new FileStream(pathSource,
                            FileMode.Open, FileAccess.Read))
                {
                    // Read the source file into a byte array.  
                    byte[] bytes = new byte[fsSource.Length];
                    int numBytesToRead = (int)fsSource.Length;
                    int numBytesRead = 0;
                    while (numBytesToRead > 0)
                    {
                        int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
                        if (n == 0)
                            break;
                        numBytesRead += n;
                        numBytesToRead -= n;
                    }
                    numBytesToRead = bytes.Length;
                    //text = Encoding.Default.GetString(bytes);  
                    m_Str= UTF8Encoding.UTF8.GetString(bytes);
                }
            }
            catch
            {
                //ioEx.Message  
            }
        }  //法(fǎ)四
    using System.IO;
    using System.Text;
    Debug.Log(File.ReadAllText("C:\\Users\\zhang\\Desktop\\wiseglove數據手套錄製數據.txt", Encoding.Default));                                      // ReadAllText方法第一個參數是要讀取txt文件的路徑,第二個參數是編碼方式,這裏采用默認
     
    //一種以追加的方式寫入txt方法
    using System.IO;
    using System.Text;
    File.AppendAllText("C:\\Users\\zxy\\Desktop\\wiseglove數據手(shǒu)套錄製(zhì)數據.txt", "我被寫進來了",Encoding.Default);
    //第一個參數是要寫入的文件路徑,第二(èr)個(gè)參數是要寫入的文本,第三個參數是(shì)編碼(mǎ)方式
    }
    

    网站地图 正能量网-传播正能量弘扬主旋律-免费正能量网站www正能量-免费正能量网站www正能量下载在线观看官网