给我的unity 5.x版本的脚本添加一个事件,“按下enter或者点击屏幕”开始游戏: using UnityEngine; using UnityEngine.UI;…
- User
给我的unity 5.x版本的脚本添加一个事件,“按下enter或者点击屏幕”开始游戏: using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.SceneManagement; using System.IO;
public class StarMenu : MonoBehaviour {
public GameObject DefaultInputs;
public MatteFade WhiteMatte; public MatteFade BlackMatte;
public float InitialWait = 1.0f; public float FadeInTime = 0.6f; public float FadeOutTime = 1.0f;
public AudioClip EnterLevel; public AudioClip LetsAGo; public AudioClip ClickButton;
public GameObject QuitButtonExe;
public string LevelName;
private InputManager inputManager;
public bool startable = false;
public GameObject titleText; public GameObject quit; public GameObject saveEditor; public GameObject exitEditor; public Button deleteSave;
private bool saveExists;
private string Path;
public void Awake() { titleText.SetActive (false); quit.SetActive (false); saveEditor.SetActive (false); exitEditor.SetActive (false); deleteSave.gameObject.SetActive (false);
inputManager = GameObject.FindObjectOfType();
startable = false;
if (!inputManager) { inputManager = ((GameObject)Instantiate(DefaultInputs, Vector3.zero, Quaternion.identity)).GetComponent(); }
if (Application.isEditor) { Path = Application.persistentDataPath + “/save.sm64hdrsave”; } else { Path = Application.streamingAssetsPath + “/save.sm64hdrsave”; } }
public void Start() { inputManager.UpdateKeyBindings();
StartCoroutine(FadeIntoMenu());
if (Application.isWebPlayer) { QuitButtonExe.SetActive(false); } }
void Update(){ if (Input.GetKeyDown (KeyCode.Return) && startable==true) { StartLevel (); startable = false; }
if (exitEditor.active) { if (File.Exists(Path)) { deleteSave.interactable = true; } else { deleteSave.interactable = false; } } }
IEnumerator FadeIntoMenu() { yield return new WaitForSeconds(InitialWait);
BlackMatte.FadeIn(FadeInTime);
yield return new WaitForSeconds(InitialWait * 2);
WhiteMatte.FadeIn(FadeInTime); WhiteMatte.FadeOut(FadeInTime); startable = true; titleText.SetActive (true); quit.SetActive (true); saveEditor.SetActive (true); }
void LoadLevel() { SceneManager.LoadScene(LevelName); }
public void OpenControlsMenu() { GetComponent().PlayOneShot(ClickButton); }
public void CloseControlsMenu() { GetComponent().PlayOneShot(ClickButton); }
public void ExitGame() { GetComponent().PlayOneShot(ClickButton);
Application.LoadLevel(0); }
public void DeleteSave() { //File.Delete(Path); GameObject.FindObjectOfType().erase(); }
public void EndGame() { GetComponent().PlayOneShot(ClickButton); Application.Quit(); }
public void StartLevel() { stopSound (); WhiteMatte.FadeIn(FadeOutTime); titleText.SetActive (false); quit.SetActive (false); saveEditor.SetActive (false);
GetComponent().PlayOneShot(EnterLevel); GetComponent().PlayOneShot(LetsAGo);
Invoke(“LoadLevel”, FadeOutTime); }
public void stopSound(){ bool[] wasPlaying; AudioSource[] allSources = GameObject.FindObjectsOfType();
wasPlaying = new bool[allSources.Length];
for (int i = 0; i < allSources.Length; i++) { var source = allSources[i];
if (source.isPlaying) { wasPlaying[i] = true; source.Pause(); } } } }
- Assistant
为了在 Unity 5.x 版本的脚本中添加一个事件,当按下 Enter 键或点击屏幕时开始游戏,我们需要在
Update方法中添加相应的输入检测。当前脚本已经处理了按下 Enter 键的情况,我们只需要添加对鼠标点击事件的检测即可。以下是更新后的脚本:
using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.SceneManagement; using System.IO; public class StarMenu : MonoBehaviour { public GameObject DefaultInputs; public MatteFade WhiteMatte; public MatteFade BlackMatte; public float InitialWait = 1.0f; public float FadeInTime = 0.6f; public float FadeOutTime = 1.0f; public AudioClip EnterLevel; public AudioClip LetsAGo; public AudioClip ClickButton; public GameObject QuitButtonExe; public string LevelName; private InputManager inputManager; public bool startable = false; public GameObject titleText; public GameObject quit; public GameObject saveEditor; public GameObject exitEditor; public Button deleteSave; private bool saveExists; private string Path; public void Awake() { titleText.SetActive(false); quit.SetActive(false); saveEditor.SetActive(false); exitEditor.SetActive(false); deleteSave.gameObject.SetActive(false); inputManager = GameObject.FindObjectOfType<InputManager>(); startable = false; if (!inputManager) { inputManager = ((GameObject)Instantiate(DefaultInputs, Vector3.zero, Quaternion.identity)).GetComponent<InputManager>(); } if (Application.isEditor) { Path = Application.persistentDataPath + "/save.sm64hdrsave"; } else { Path = Application.streamingAssetsPath + "/save.sm64hdrsave"; } } public void Start() { inputManager.UpdateKeyBindings(); StartCoroutine(FadeIntoMenu()); if (Application.isWebPlayer) { QuitButtonExe.SetActive(false); } } void Update() { if ((Input.GetKeyDown(KeyCode.Return) || Input.GetMouseButtonDown(0)) && startable == true) { StartLevel(); startable = false; } if (exitEditor.active) { if (File.Exists(Path)) { deleteSave.interactable = true; } else { deleteSave.interactable = false; } } } IEnumerator FadeIntoMenu() { yield return new WaitForSeconds(InitialWait); BlackMatte.FadeIn(FadeInTime); yield return new WaitForSeconds(InitialWait * 2); WhiteMatte.FadeIn(FadeInTime); WhiteMatte.FadeOut(FadeInTime); startable = true; titleText.SetActive(true); quit.SetActive(true); saveEditor.SetActive(true); } void LoadLevel() { SceneManager.LoadScene(LevelName); } public void OpenControlsMenu() { GetComponent<AudioSource>().PlayOneShot(ClickButton); } public void CloseControlsMenu() { GetComponent<AudioSource>().PlayOneShot(ClickButton); } public void ExitGame() { GetComponent<AudioSource>().PlayOneShot(ClickButton); Application.LoadLevel(0); } public void DeleteSave() { //File.Delete(Path); GameObject.FindObjectOfType<GameMaster>().erase(); } public void EndGame() { GetComponent<AudioSource>().PlayOneShot(ClickButton); Application.Quit(); } public void StartLevel() { stopSound(); WhiteMatte.FadeIn(FadeOutTime); titleText.SetActive(false); quit.SetActive(false); saveEditor.SetActive(false); GetComponent<AudioSource>().PlayOneShot(EnterLevel); GetComponent<AudioSource>().PlayOneShot(LetsAGo); Invoke("LoadLevel", FadeOutTime); } public void stopSound() { bool[] wasPlaying; AudioSource[] allSources = GameObject.FindObjectsOfType<AudioSource>(); wasPlaying = new bool[allSources.Length]; for (int i = 0; i < allSources.Length; i++) { var source = allSources[i]; if (source.isPlaying) { wasPlaying[i] = true; source.Pause(); } } } }在更新后的脚本中,我增加了对
Input.GetMouseButtonDown(0)的检测,从而实现了点击屏幕开始游戏的功能。现在,无论是按下 Enter 键还是点击屏幕都能开始游戏。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.