Unity 5.2.1 Build crash on Application.Quit(); with Windows Touch

旧城冷巷雨未停 提交于 2021-01-28 22:19:49

问题


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.

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");

    }
}

Thanks


回答1:


[EDIT:] This solved the problem:

System.Diagnostics.Process.GetCurrentProcess().Kill();
// instead of
// 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();
    }
}



回答2:


if (Input.GetKey("escape")) Application.Quit();



来源:https://stackoverflow.com/questions/34068406/unity-5-2-1-build-crash-on-application-quit-with-windows-touch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!