• 您(nín)的位置:首頁 > 新聞動(dòng)態 > Unity3D

    UNITY3D使用C#腳本的幾(jǐ)種讀寫TXT文本文件的方法

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

    自愉色色,亚洲色孩,jk无遮拦,日日夜夜7799天天综合,999夜夜,7799天天综合天天综合网精品视频,日日夜夜精品视频7799,天天综合7799精品影视