iOS / Android上的Unity Coroutine Freeze

时间:2016-11-11 17:13:31

标签: android ios unity3d coroutine

我一直在尝试制作适用于移动设备的应用程序(iOS和Android)以及此应用程序的功能非常简单:

  1. 从网络服务器下载txt文件

  2. "运行" txt,txt文件有一个命令,如" display"或者"玩"使用资源的URL(jpg或mp4)

  3. 将资源下载或流式传输到场景中的纹理(完成)

  4. 使用GoogleVR(LatestVersion)拍摄整件事

  5. 现在的问题是,当我启动协程下载内容时,游戏会在移动设备上冻结,但在编辑器中它并没有冻结。

    以下是代码:

    IEnumerator retrieveCommand(){
        print ("Retrieving command...");
        WWW rx = new WWW (host + "command.txt"); // host is the URL of server
        yield return rx; // wait for end WWW
        if (!string.IsNullOrEmpty (rx.error)) { // if there's an error i stop execution of coroutine
            print (rx.error);
            mex.text = rx.error;
            yield break;
        }
        print ("Command: " + rx.text);
        if (lastCommand != rx.text.Trim ()) { // if last command retrieved is NOT the same as the current
            lastCommand = rx.text.Trim ();
            command = rx.text.Trim ();
            string[] argv = command.Split ('|'); // command is in form of <cmd>|parameter
            mexdisplay = argv [0]; // argv[0] contains command, argv[1] contains the paramter
            switch (argv [0]) { // what's the command?
            case "PLAY":
                video.GetComponent<MediaPlayerCtrl> ().Play ();
                break;
            case "STOP":
                video.GetComponent<MediaPlayerCtrl> ().Stop ();
                break;
            case "PAUSE":
                video.GetComponent<MediaPlayerCtrl> ().Pause ();
                break;
            case "GRAB": // set video resource
                try {
                    video.GetComponent<MediaPlayerCtrl> ().m_strFileName = host + argv [1];
                } catch (Exception e) {
                    print ("Exception: " + e);
                }
                break;
            case "DISPLAY": // downloads and shows picture
                print ("Display: " + host + argv [1]);
                rx = new WWW (host + argv [1]);
                yield return rx;
                if (!string.IsNullOrEmpty (rx.error)) {
                    print (rx.error);
                    mex.text = rx.error;
                    yield break;
                }
                video.GetComponent<Renderer> ().material.mainTexture = rx.texture;
                break;
            }
        }
        asdf = true;
    }
    

    通过更新每隔10秒调用一个协程:

    void Update(){
        if(asdf){ // bool, if false -> running coroutine, if true -> not running->count 10 seconds
            curr += Time.deltaTime;
            if (curr >= time) { // time=10f, if counted to 10s start coroutine and reset timer
                asdf = false;
                curr = 0;
                StartCoroutine (retrieveCommand ());
            }
        }
    }
    

    编辑: 事实证明它就像相机停止渲染......任何帮助?我正在使用GoogleVR相机预制件...

0 个答案:

没有答案