总结Unity知识归纳

移动端外部配置文件读写

2017-04-11  本文已影响8人  IongX
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
public class DoFileTest : MonoBehaviour {

    public Text PathText;
    public Text ResultText;
    public InputField TestField;

    public string filepath;
    // Use this for initialization
    void Start ()
    {
        filepath = Application.persistentDataPath + "/file.txt";
        PathText.text = filepath;
    }
    
    public void ReadFile()
    {
        try
        {
            StreamReader sr = new StreamReader(filepath);
            string ss = sr.ReadToEnd();
            Debug.Log(ss);
            ResultText.text = ss;
        }catch(Exception e)
        {
            ResultText.text = e.Message;
        }

    }


    public void OnBtnClick()
    {
        Debug.Log(TestField.text + "       shengc");
        try
        {
            FileStream fs = new FileStream(filepath, FileMode.Create);
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(TestField.text);
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
            fs.Close();         
        }
        catch(Exception e)
        {
            Debug.Log("保存失败!!!!");
        }

    }
}

unity3d读取外部文件详细 :

http://www.cnblogs.com/murongxiaopifu/p/4199541.html

上一篇 下一篇

猜你喜欢

热点阅读