UI Canvas 开始 - 退出游戏

UI Canvas Start - Quit Game

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

当 UI 图片 Play 被点击时

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

}

当 UI 图片 Exit 被点击时

Application.Quit ();

C# 如果可能的话。

将此脚本添加到您的播放图像:

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 blocking/overlapping 你的图片,否则他们将不会收到任何点击事件。

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