Unity 5.2.1 在 Application.Quit() 上构建崩溃;使用 Windows 触控 [英] Unity 5.2.1 Build crash on Application.Quit(); with Windows Touch

查看:45
本文介绍了Unity 5.2.1 在 Application.Quit() 上构建崩溃;使用 Windows 触控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的 Unity 4 项目升级到 Unity 5.2.1.我用过 Application.Quit();单击按钮关闭应用程序.这在早期(Unity 4)使用鼠标和触摸都可以正常工作,现在(Unity 5.2)也可以通过鼠标单击正常工作.但是,如果我使用 Touch(在 Windows 8 或 Windows 7 触摸屏上)单击按钮,应用程序就会崩溃.

I am upgrading my Unity 4 project to Unity 5.2.1. I have used Application.Quit(); to close the app on a button click. This worked fine earlier(Unity 4) with Mouse and Touch both and now (Unity 5.2) also works fine with mouse click. But if I click the button using Touch (on Windows 8 or Windows 7 Touch screens) the app crashes.

然后我通过创建一个新的 Unity 项目进行测试,并将带有以​​下代码的 cs 文件添加到主摄像头.当我用 Touch 单击此按钮时,它会崩溃.但不会因鼠标点击而崩溃.这是 Unity 5.2.1 中的错误吗?我该如何解决这个问题?

Then I tested by creating a new Unity project and added the cs file with the below code to the main camera. When I click this button with Touch it crashes. But doesn't crash with the mouse click. Is this a bug in Unity 5.2.1? How can I fix this issue?

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

public Texture btnTexture;

void OnGUI()
{
    if (!btnTexture)
    {
        Debug.LogError("Please assign a texture on the inspector");
        return;
    }
    if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
    {
        Debug.LogError("Clicked the button with an image");
        Application.Quit();
    }

    if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
        Debug.LogError("Clicked the button with text");

    }
}

谢谢

推荐答案

这解决了问题:

System.Diagnostics.Process.GetCurrentProcess().Kill();
// instead of
// Application.Quit();

您是否尝试从 OnGUI 方法的外部调用 Application.Quit?像这样:

Have you tried to call Application.Quit from outside of the OnGUI method? Like this:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 50, 50), "Exit"))
        {
            StartCoroutine(Quit());
        }
    }

    public static IEnumerator Quit()
    {
        yield return new WaitForEndOfFrame();
        Application.Quit();
    }
}

这篇关于Unity 5.2.1 在 Application.Quit() 上构建崩溃;使用 Windows 触控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆