2 u014576926 u014576926 于 2016.09.18 22:32 提问

使用Unity打开文件对话框,报错“GCHandle value cannot be zero” 5C

我在网上找了一个打开用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);
        }

    }
}

1个回答

devmiao
devmiao   Ds   Rxr 2016.09.18 23:55
u014576926
u014576926 没有我要的答案,谢谢~
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片