UI Canvas Start - Quit Game

a 夏天 提交于 2019-12-12 03:25:20

问题


I have 2 Canvas UIs (Start and Exit) on the home screen of my game. I want to add 1 script that does the following:

When the UI Image Play is clicked

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

}

When the UI Image Exit is clicked

Application.Quit ();

C# if possible.


回答1:


Add this script to your Play image:

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
    }   
}

And add this script to your Exit image:

using UnityEngine;
using UnityEngine.EventSystems;

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

And finally make sure no other ui blocking/overlapping your images, otherwise they won't receive any click event.

Not to mention that a script file's name should match the name of it class :)



来源:https://stackoverflow.com/questions/36806531/ui-canvas-start-quit-game

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