• 您的(de)位置(zhì):首頁 > 新聞動態 > Unity3D

    UNITY3D使用C#腳(jiǎo)本的幾種讀寫TXT文本文件的方法

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

    91网站入口_91视频导航_91短视频在线_91视频在线免费观看