我在网上找了一个打开用Unity打开文件对话框的示例,运行的时候报错“GCHandle value cannot be zero”,请问有哪位大牛知道怎么解决吗?另外使用的是System.Windows.Forms库
以下是代码
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
public class FileMenu : MonoBehaviour {
//SaveFileDialog saveLog;
StreamReader sr;
//StreamWriter sw;
string strSendTxt;
// 显示文本和输入文本框所在的类
public GameObject text ;
public 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 = "";
List<string> lst = new List<string>();
strSendTxt = "";
while( (line == sr.ReadLine()) != null)
{
lst.Add(line);
}
foreach(string s in lst)
{
strSendTxt += s+"\n";
}
//文本框的文本内容发生改变
text.GetComponent<Text>().text = strSendTxt;
sr.Close();
sr.Dispose();
}
}
catch(Exception e) {
Debug.Log ("打开错误!" + e.Message);
//Application.Quit(0);
}
}
}