美载小路 2016-09-18 14:32 采纳率: 50%
浏览 1566
已结题

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

我在网上找了一个打开用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 2016-09-18 15:55
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog