2016年12月28日 星期三

Unity 讀取文字檔內文

VariousVocabularyCreateText(C#):

using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace VariousVocabularyCreateText
{

    public class VocabularyData
    {
        // 檔案路徑
        private string folderPath = "D:/Vocabulary/";
        private string folderPathType
        {
            get { return folderPath; }
        }

        // 檔案名稱
        private string vocabularName;
        public string vocabularNameType
        {
            get { return vocabularName; }
            set { vocabularName = value; }
        }

        // 讀取文字檔內文
        private string readtxtMessage;
        public string readtxtMessageType
        {
            get { return readtxtMessage; }
        }

        // 附檔名
        private string attachment = ".txt";

        // 文字內容
        private List readTxtMessageList = new List();

        /// 
        /// 檢查資料夾
        /// 
        /// 
        private bool examinationFolderFunction()
        {

            bool fileExisit = (File.Exists(folderPath)) ? true : false;
            return fileExisit;
        }

        /// 
        /// 檢查文字檔
        /// 
        /// 
        private bool exmainationTxtFunction()
        {

            bool txtFileExisit = (File.Exists(folderPath + vocabularName + attachment)) ? true : false;
            return txtFileExisit;
        }

        /// 
        /// 建立文件檔
        /// 
        public void createTxtMeesage()
        {

            if (!examinationFolderFunction())
            {
                Directory.CreateDirectory(folderPath);

                FileStream fileStream = new FileStream(folderPath + vocabularName + attachment, FileMode.OpenOrCreate);
                StreamWriter stramWrite = new StreamWriter(fileStream);

                try
                {
                    stramWrite.WriteLine("安安");
                    stramWrite.WriteLine("狗狗");
                    stramWrite.Close();
                }
                catch (IOException ex)
                {
                    Debug.Log(ex.Message);
                    return;
                }
            }
        }

        /// 
        /// 讀取文建資料
        /// 
        /// 
        private List readFileTxtMessage()
        {
            StreamReader streamReader = new StreamReader(folderPath + vocabularName + attachment);
            while ((readtxtMessage = streamReader.ReadLine()) != null)
                readTxtMessageList.Add(readtxtMessage);

            streamReader.Close();
            return readTxtMessageList;
        }
    }
}
-----------------------------
 執行腳本(C#): 讀取記事本內文
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    VariousVocabularyCreateText.VocabularyData vocabularData = new VariousVocabularyCreateText.VocabularyData();

    void Start() {

        vocabularData.vocabularNameType = "bb";

        foreach (string message in vocabularData.readFileTxtMessage())
        {
            Debug.Log(message);
        }
    }
}
-----------------------------
 執行腳本(C#): 建立文建及換行
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    VariousVocabularyCreateText.VocabularyData vocabularData = new VariousVocabularyCreateText.VocabularyData();

    void Start() {
        vocabularData.vocabularNameType = "bb";
        vocabularData.createTxtMeesage();
    }
}