Unity3d打飞机(六)各种按钮的点击事件,文本框内容改变和最终分数的记录,背景音乐播放

分类栏目:unity3d教程

175

Unity3d打飞机(六)各种按钮的点击事件,文本框内容改变和最终分数的记录,背景音乐播放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonAndText : MonoBehaviour {
 
	public Image LogoImg;
	public Button StartBtn, ExitBtn, pauseButton, beginButton, bombButton, pause;
	public Text numText, bombText, maxText, maxNumText, newNum, newNumText;
 
 
	//背景音乐
	private AudioSource ads;
 
	//得分和最高分
	public static int num = 0;
	private int maxNum = 0;
 
	//炸弹数量
	public static int bombNum = 0;
 
	//是否有些结束
	public static bool isOver = false;
 
	// Use this for initialization
	void Start () {
		Time.timeScale = 0;
		ads = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
 
		numText.text = "当前得分:" + num;
		bombText.text = "x" + bombNum;
		//如果有些结束存取最高分
		if (isOver) {
			maxNum = PlayerPrefs.GetInt ("maxNum");
			if (maxNum < num) {
				maxNum = num;
				PlayerPrefs.SetInt ("maxNum", maxNum);
			}
			maxNumText.text = "" + maxNum;
			newNum.text = "" + num;
			maxText.gameObject.SetActive (true);
			maxNumText.gameObject.SetActive (true);
			newNum.gameObject.SetActive (true);
			newNumText.gameObject.SetActive (true);
			pause.gameObject.SetActive (true);
 
			pauseButton.gameObject.SetActive (false);
			bombButton.gameObject.SetActive (false);
			numText.gameObject.SetActive (false);
 
			Time.timeScale = 0;
 
		} else {
			maxText.gameObject.SetActive (false);
			maxNumText.gameObject.SetActive (false);
			newNum.gameObject.SetActive (false);
			newNumText.gameObject.SetActive (false);
			pause.gameObject.SetActive (false);
		}
	}
 
	//开始游戏  让帧数开始刷新 隐藏标题,开始和结束按钮
	public void OnClickStart(){
		Time.timeScale = 1;
		LogoImg.gameObject.SetActive (false);
		StartBtn.gameObject.SetActive (false);
		ExitBtn.gameObject.SetActive (false);
 
		pauseButton.gameObject.SetActive (true);
		bombButton.gameObject.SetActive (true);
		numText.gameObject.SetActive (true);
	}
	//暂停或者开始
	public void OnClickPause(){
 
		if (Time.timeScale == 1) {
			ads.Stop ();
			Time.timeScale = 0;
			beginButton.gameObject.SetActive (true);
			pauseButton.gameObject.SetActive (false);
		} else {
			ads.Play ();
			Time.timeScale = 1;
			beginButton.gameObject.SetActive (false);
			pauseButton.gameObject.SetActive (true);
		}
	}
 
	//结束游戏
	public void OnClickExit(){
		Application.Quit ();
	}
 
	//重新开始
	public void OnClickOver(){
		num = 0;
		isOver = false;
		EnemyAndSuperMove.num = 0;
		SceneManager.LoadScene ("Airplane");
		Time.timeScale = 1;
 
	}
}