UI画布开始 - 退出游戏

时间:2016-04-23 04:08:36

标签: c# unity3d unityscript unity5

我在游戏主屏幕上有2个Canvas UI (Start and Exit)。我想添加一个执行以下操作的脚本:

单击UI图像Play

public void NextLevel(int level)
{
    Score.Inicializar(); 
    Application.LoadLevel (1);

}

单击UI图像Exit

Application.Quit ();

如果可能的话#。

1 个答案:

答案 0 :(得分:0)

将此脚本添加到您的Play图片:

using UnityEngine;
using UnityEngine.EventSystems;
//using UnityEngine.SceneManagement; // uncomment this line in case you wanna use SceneManager

public class HandleClickOnPlayImage : MonoBehaviour, IPointerClickHandler {
    int level = 1; // I'm assuming you're setting this value somehow in your application

    public void OnPointerClick (PointerEventData eventData)
    {
        Score.Inicializar(); 
        Application.LoadLevel (level);
        // SceneManager.LoadScene (level); // <-- use this method instead for loading scenes
    }   
}

将此脚本添加到退出图像:

using UnityEngine;
using UnityEngine.EventSystems;

public class HandleClickOnExitImage : MonoBehaviour, IPointerClickHandler {
    public void OnPointerClick (PointerEventData eventData)
    {
        Application.Quit();
    }   
}

最后确保没有其他ui阻止/重叠您的图片,否则他们将不会收到任何点击事件。

更不用说脚本文件的名称应该与它的名称相匹配:)