zoukankan      html  css  js  c++  java
  • 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+"
    ";
                    }
                    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">

  • 相关阅读:
    filter函数和map函数
    生成器面试题
    装饰器激活生成器
    移动平均値
    send()方法的初识
    监听文件的输入
    迭代器抛出异常处理方法
    装饰器-wraps
    多个装饰器装饰一个函数
    WebView 安卓原生java与h5,js交互
  • 原文地址:https://www.cnblogs.com/YDoubleC/p/6203728.html
Copyright © 2011-2022 走看看