Unity 读取、写入自定义路径文件,调用System.Windows.Forms

本文介绍如何在Unity中通过System.Windows.Forms库读取、写入自定义路径的文件。首先在Unity项目中添加Plugins文件夹并放入System.Windows.Forms.dll,然后通过C#代码实现文件操作。详细步骤包括创建OpenFileDialog和SaveFileDialog对话框,实现打开、保存、保存为和关闭文件的功能。
摘要由CSDN通过智能技术生成

Unity 读取、写入自定义路径文件,调用System.Windows.Forms

原创  2015年08月27日 17:00:55
  • 2726

调用System.Windows.Forms DLL

首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll

然后代码中添加引用

[csharp]  view plain  copy
 
  1. using System;  
  2. using UnityEngine;  
  3. using System.Collections;  
  4. using System.Collections.Generic;  
  5. using System.Windows.Forms;  
  6. using System.IO;  
  7.   
  8. public class FileMenu : MonoBehaviour  
  9. {  
  10.     #region Public  
  11.     public UIButton BtnNew;  
  12.     public UIButton BtnOpen;  
  13.     public UIButton BtnSave;  
  14.     public UIButton BtnSaveAs;  
  15.     public UIButton BtnClose;  
  16.     #endregion  
  17.  
  18.     #region private  
  19.       
  20.     SaveFileDialog saveLog;  
  21.     StreamReader sr;  
  22.     StreamWriter sw;  
  23.     string strSendTxt;  
  24.     UIPaneAuto uiPanelAuto;//一个显示文本和输入文本的文本框所在的类  
  25.     #endregion  
  26.   
  27.     void Awake()  
  28.     {  
  29.         uiPanelAuto = transform.GetComponent<UIPaneAuto>();  
  30.         BtnNew = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnNew").GetComponent<UIButton>();  
  31.         BtnOpen = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnOpen").GetComponent<UIButton>();  
  32.         BtnSave = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnSave").GetComponent<UIButton>();  
  33.         BtnSaveAs = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnSaveAs").GetComponent<UIButton>();  
  34.         BtnClose = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnClose").GetComponent<UIButton>();  
  35.         EventDelegate.Add(BtnNew.onClick, fnNew);  
  36.         EventDelegate.Add(BtnOpen.onClick, fnOpen);  
  37.         EventDelegate.Add(BtnSave.onClick, fnSave);  
  38.         EventDelegate.Add(BtnSaveAs.onClick, fnSave);  
  39.         EventDelegate.Add(BtnClose.onClick, fnClose);  
  40.     }  
  41.   
  42.       
  43.   
  44.     void fnNew()  
  45.     {  
  46.         Debug.Log("New");  
  47.         uiPanelAuto.fnSetInputTxt("");  
  48.     }  
  49.   
  50.     void fnOpen()  
  51.     {  
  52.         Debug.Log("Open");  
  53.         try  
  54.         {  
  55.             OpenFileDialog opLog = new OpenFileDialog();  
  56.             opLog.InitialDirectory = UnityEngine.Application.dataPath;  
  57.             opLog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";  
  58.             DialogResult result = opLog.ShowDialog();  
  59.             if (result == DialogResult.OK)  
  60.             {  
  61.                 string path = opLog.FileName;  
  62.                 Debug.Log(path);  
  63.                 sr = File.OpenText(path);  
  64.   
  65.                 string line;  
  66.                 BetterList<string> lst = new BetterList<string>();  
  67.                 strSendTxt = "";  
  68.                 while ((line = sr.ReadLine()) != null)  
  69.                 {  
  70.                     lst.Add(line);  
  71.                 }  
  72.                 foreach (string s in lst)  
  73.                 {  
  74.                     strSendTxt += s+"\n";  
  75.                 }  
  76.                 uiPanelAuto.fnSetInputTxt(strSendTxt);  
  77.                 sr.Close();  
  78.                 sr.Dispose();  
  79.             }  
  80.         }  
  81.         catch(Exception e)  
  82.         {  
  83.             Debug.Log("打开错误:"+e.Message);  
  84.             return;  
  85.         }  
  86.     }  
  87.   
  88.     void fnSave()  
  89.     {  
  90.             Debug.Log("Save");  
  91.             SaveFileDialog saveLog = new SaveFileDialog();  
  92.             saveLog.InitialDirectory = UnityEngine.Application.dataPath;  
  93.             saveLog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";  
  94.             DialogResult result = saveLog.ShowDialog();  
  95.             if (result == DialogResult.OK)  
  96.             {  
  97.                 Debug.Log(saveLog.FileName);  
  98.                 FileInfo f = new FileInfo(saveLog.FileName);  
  99.                 if (f.Exists)  
  100.                 {  
  101.                     f.Delete();//如果存在同名文本,就删除它重新建一个  
  102.                     f = new FileInfo(saveLog.FileName);  
  103.                     sw = f.AppendText();  
[csharp]  view plain  copy
 
  1.             //将LstIptChect中的数据添加到Txt中  
  2.                     foreach (string s in uiPanelAuto.lstIptCheck)  
  3.                     {  
  4.                         Debug.Log(s);  
  5.                         sw.WriteLine(s);  
  6.                     }  
  7.                     sw.Close();  
  8.                     sw.Dispose();  
  9.                 }  
  10.                 else  
  11.                 {  
  12.                     sw = f.AppendText();  
  13.                      foreach (string s in uiPanelAuto.lstIptCheck)  
  14.                     {  
  15.                         Debug.Log(s);  
  16.                         if(s!="")  
  17.                         sw.WriteLine(s);  
  18.                     }  
  19.                     sw.Close();  
  20.                     sw.Dispose();  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     void fnClose()  
  27.     {  
  28.         Application.Quit();  
  29.     }  
  30. }  
[csharp]  view plain  copy
 
  1.   
[csharp]  view plain  copy
 
  1. </pre><pre class="csharp" name="code"></pre><pre class="csharp" name="code">  

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值