Unity 读取、写入自定义路径文件,调用System.Windows.Forms
调用System.Windows.Forms DLL
首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll
然后代码中添加引用
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO; public class FileMenu : MonoBehaviour
{
#region Public
public UIButton BtnNew;
public UIButton BtnOpen;
public UIButton BtnSave;
public UIButton BtnSaveAs;
public UIButton BtnClose;
#endregion #region private SaveFileDialog saveLog;
StreamReader sr;
StreamWriter sw;
string strSendTxt;
UIPaneAuto uiPanelAuto;//一个显示文本和输入文本的文本框所在的类
#endregion void Awake()
{
uiPanelAuto = transform.GetComponent<UIPaneAuto>();
BtnNew = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnNew").GetComponent<UIButton>();
BtnOpen = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnOpen").GetComponent<UIButton>();
BtnSave = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnSave").GetComponent<UIButton>();
BtnSaveAs = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnSaveAs").GetComponent<UIButton>();
BtnClose = GameObject.Find("UI Root/Camera/PanelTop/LstBtn/BtnClose").GetComponent<UIButton>();
EventDelegate.Add(BtnNew.onClick, fnNew);
EventDelegate.Add(BtnOpen.onClick, fnOpen);
EventDelegate.Add(BtnSave.onClick, fnSave);
EventDelegate.Add(BtnSaveAs.onClick, fnSave);
EventDelegate.Add(BtnClose.onClick, fnClose);
} void fnNew()
{
Debug.Log("New");
uiPanelAuto.fnSetInputTxt("");
} void fnOpen()
{
Debug.Log("Open");
try
{
OpenFileDialog opLog = new OpenFileDialog();
opLog.InitialDirectory = UnityEngine.Application.dataPath;
opLog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
DialogResult result = opLog.ShowDialog();
if (result == DialogResult.OK)
{
string path = opLog.FileName;
Debug.Log(path);
sr = File.OpenText(path); string line;
BetterList<string> lst = new BetterList<string>();
strSendTxt = "";
while ((line = sr.ReadLine()) != null)
{
lst.Add(line);
}
foreach (string s in lst)
{
strSendTxt += s+"\n";
}
uiPanelAuto.fnSetInputTxt(strSendTxt);
sr.Close();
sr.Dispose();
}
}
catch(Exception e)
{
Debug.Log("打开错误:"+e.Message);
return;
}
} void fnSave()
{
Debug.Log("Save");
SaveFileDialog saveLog = new SaveFileDialog();
saveLog.InitialDirectory = UnityEngine.Application.dataPath;
saveLog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
DialogResult result = saveLog.ShowDialog();
if (result == DialogResult.OK)
{
Debug.Log(saveLog.FileName);
FileInfo f = new FileInfo(saveLog.FileName);
if (f.Exists)
{
f.Delete();//如果存在同名文本,就删除它重新建一个
f = new FileInfo(saveLog.FileName);
sw = f.AppendText();
//将LstIptChect中的数据添加到Txt中
foreach (string s in uiPanelAuto.lstIptCheck)
{
Debug.Log(s);
sw.WriteLine(s);
}
sw.Close();
sw.Dispose();
}
else
{
sw = f.AppendText();
foreach (string s in uiPanelAuto.lstIptCheck)
{
Debug.Log(s);
if(s!="")
sw.WriteLine(s);
}
sw.Close();
sw.Dispose();
}
}
}
} void fnClose()
{
Application.Quit();
}
}
</pre><pre class="csharp" name="code"></pre><pre class="csharp" name="code">
Unity 读取、写入自定义路径文件,调用System.Windows.Forms的更多相关文章
- Unity引用System.Windows.Forms遇到的一些坑
这两天在做一个unity打开文件选择框的功能.网上找到两种方法, 第一种是调用win32打开对话框,这个挺好,但是有个致命的问题,没办法多选!!!多选的话返回的是根目录的路径,文件名返回不了,找了半天 ...
- System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
- System.Windows.Forms.ListView : Control
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
- WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常
WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...
- C#在父窗口中调用子窗口的过程(无法访问已释放的对象)异常,不存在从对象类型System.Windows.Forms.DateTimePicker到已知的托管提供程序本机类型的映射。
一:C#在父窗口中调用子窗口的过程(无法访问已释放的对象)异常 其实,这个问题与C#的垃圾回收有关.垃圾回收器管 理所有的托管对象,所有需要托管数据的.NET语言(包括 C#)都受运行库的 垃圾回收器 ...
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...
- .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll
这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...
- System.Windows.Forms.Help
在开发过程中,基本都需要实现帮助功能,而一般帮助功能就是打开一个帮助文档,System.Windows.Forms提供了Help类用于打开帮助文档,挺方便的. Help类提供的方法如下: Name ...
- System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
随机推荐
- Introduction to object
1 Declarations VS definitions (Page 81) declarations: This function or variable exists somew ...
- Windows phone 之Image控件
wp目前支持的图片格式为png和jpeg ,我们可以通过设置Source属性设置图片源. 还有两个属性是:Stretch,Opacity Stretch属性 image的拉伸行为有此属性决定,此属性是 ...
- WF学习笔记(四)
1.使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity: public class ExceptionActivi ...
- BOM 之 screen history
/* avaiHeight // 屏幕的像素高度减去系统部件高度之后的值 var ah = screen.availHeight; alert(ah); */ /* avai ...
- Python核心编程2第三章课后练习
1. 标识符.为什么Python 中不需要变量名和变量类型声明? Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程.每个变量在内存中创建,都包括变量的标识,名称和数据这些信息.每 ...
- JSP 结构
网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面. 容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP的正 ...
- Linux内核监控模块-0-开篇简介
最近做了一个关于Linux内核编程的项目,主要实现的功能是对文件系统的监控. 我采用的方式是将监控的功能做成一个可加载模块(LKM)的形式,动态加载到Linux内核中.在这个内核层的“监控模块”中,我 ...
- http server v0.1_http_parse.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "mime.h&q ...
- MVC自学系列之二(MVC控制器-Controllers)
Controllers的职责 1.MVC模式中的Controllers的职责是对用户的输入做出响应,对用户的输入在实体上做一些变化.它关心的是应用的流动,处理传入的数据,并给相关的View提供数据 ...
- LINUX TOP,不是这样玩地!!!
老同志遇到新问题了. TOP显示完全不是我要的,CPU,内存都是0.每个CPU还分别显示. 网上搜下,原来是A(显示风格)R(反向排序)P,M(CPU,内存排序)之类引起的. 记下了.