UI Canvas开始-退出游戏 [英] UI Canvas Start - Quit Game
本文介绍了UI Canvas开始-退出游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在游戏的主屏幕上有2个Canvas UI (Start and Exit)
.我想添加1个执行以下操作的脚本:
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:
当单击UI图像Play
时
public void NextLevel(int level)
{
Score.Inicializar();
Application.LoadLevel (1);
}
当单击UI图像Exit
时
Application.Quit ()
;
C#(如果可能).
推荐答案
将此脚本添加到您的Play图片:
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();
}
}
最后,请确保没有其他ui阻止/重叠您的图像,否则它们将不会收到任何点击事件.
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 :)
这篇关于UI Canvas开始-退出游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文