问题
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