MainMenu

using UnityEngine;
using System.Collections;
using System.Xml;
/// <summary>
/// 主菜單場景
/// </summary>
public class MainMenu : MonoBehaviour {
	
	Transform Werewolf;//狼人對象
	Animation wolfAnimation;//動畫組件
	NavMeshAgent agent;//導航代理
	float stopDistance;//制動距離
	bool isNav;//是否開始導航
	BgMusic bgMusic;//背景音樂管理器
	string index;//讀取存檔索引
	
	#region 系統設置窗體組件
	Transform WinSetting;
	UIPopupList RunModeUI;
	UISlider SoundEffectsUI;
	UILabel SoundEffectsLabel;
	UISlider BgMusicUI;
	UILabel BgMusicLabel;
	UICheckbox JumpFontUI;
	UICheckbox TopInfoUI;
	UICheckbox WeatherSystemUI;
	#endregion
	
	#region 讀取遊戲窗體組件
	Transform WinLoad;
	Transform savePic;
	UILabel character;
	UILabel date;
	UILabel level;
	UILabel location;
	UILabel savenum;
	UILabel time;
	#endregion
	
	/// <summary>
	/// 初始化全部組件
	/// </summary>
	void Start () {
		//角色動畫組件
		Werewolf = GameObject.Find("Werewolf").transform;
		wolfAnimation = Werewolf.animation;
		wolfAnimation.wrapMode = WrapMode.Loop;
		//導航組件
		agent = Werewolf.GetComponent<NavMeshAgent>();
		stopDistance = agent.stoppingDistance;
		//背景音樂組件
		bgMusic = GameObject.Find("MainUI").GetComponent<BgMusic>();
		BgMusic.PlayMusic("MainMenu");
		BgMusic.SetVolume(1f);
		//初始化窗體組件
		initSetting();
		initLoadUI();
	}
	
	/// <summary>
	/// 初始化系統設置窗體組件
	/// </summary>
	void initSetting(){
		WinSetting = GameObject.Find("Win_Setting").transform;
		RunModeUI = WinSetting.Find("Options/RunMode/Popup List").GetComponent<UIPopupList>();
		SoundEffectsUI = WinSetting.Find("Options/SoundEffects/Slider").GetComponent<UISlider>();
		SoundEffectsLabel = WinSetting.Find("Options/SoundEffects/Slider/Label").GetComponent<UILabel>();
		BgMusicUI = WinSetting.Find("Options/BgMusic/Slider").GetComponent<UISlider>();
		BgMusicLabel = WinSetting.Find("Options/BgMusic/Slider/Label").GetComponent<UILabel>();
		JumpFontUI = WinSetting.Find("Options/JumpFont").GetComponent<UICheckbox>();
		TopInfoUI = WinSetting.Find("Options/TopInfo").GetComponent<UICheckbox>();
		WeatherSystemUI = WinSetting.Find("Options/WeatherSystem").GetComponent<UICheckbox>();
		
		BgMusicUI.sliderValue = 0.5f;
		SoundEffectsUI.sliderValue = 0.5f;
		BgMusicLabel.text = (int)(BgMusicUI.sliderValue * 100) + "%";
		SoundEffectsLabel.text = (int)(SoundEffectsUI.sliderValue * 100) + "%";
		WinSetting.gameObject.SetActive(false);
	}
	
	/// <summary>
	/// 初始化讀取遊戲窗體組件
	/// </summary>
	public void initLoadUI(){
		WinLoad = GameObject.Find("MainUI/Camera/Anchor/Win_Load").transform;
		savePic = WinLoad.Find("Panel2/SavePic");
		character = WinLoad.Find("Panel2/Labels/character").GetComponent<UILabel>();
		date = WinLoad.Find("Panel2/Labels/date").GetComponent<UILabel>();
		level = WinLoad.Find("Panel2/Labels/level").GetComponent<UILabel>();
		location = WinLoad.Find("Panel2/Labels/location").GetComponent<UILabel>();
		savenum = WinLoad.Find("Panel2/Labels/savenum").GetComponent<UILabel>();
		time = WinLoad.Find("Panel2/Labels/time").GetComponent<UILabel>();
		WinLoad.gameObject.SetActive(false);
	}
	
	/// <summary>
	/// 到達攝像機前,播放動畫後,開始遊戲
	/// </summary>
	void Update () {
		float distance = agent.remainingDistance;
		if(isNav && distance != 0 && distance <= stopDistance){
			isNav = false;
			agent.Stop();
			wolfAnimation.wrapMode = WrapMode.Once;
			wolfAnimation.Play("Attack");
			StartCoroutine(delayStart());
		}
	}
	
	/// <summary>
	/// 開始延遲,跳轉到角色選擇場景
	/// </summary>
	IEnumerator delayStart(){
		yield return new WaitForSeconds(2f);
		Application.LoadLevel("Character");
	}
	
	#region 主菜單按鈕事件
	/// <summary>
	/// 開始遊戲事件
	/// </summary>
	public void StartGame(){
		wolfAnimation.Play("Walk");
		Vector3 dest = GameObject.Find("Main Camera").transform.position;
		agent.SetDestination(dest);
		isNav = true;
	}
	
	/// <summary>
	/// 打開系統設置窗體
	/// </summary>
	public void OpenSetting(){
		if(!WinSetting.gameObject.activeSelf){
			WinLoad.gameObject.SetActive(false);
			WinSetting.gameObject.SetActive(true);
		}else{
			WinSetting.gameObject.SetActive(false);
		}
	}
	
	/// <summary>
	/// 打開讀取存檔窗體
	/// </summary>
	public void OpenLoading(){
		if(!WinLoad.gameObject.activeSelf){
			WinLoad.gameObject.SetActive(true);
			WinSetting.gameObject.SetActive(false);
			clearRecord();
		}else{
			WinLoad.gameObject.SetActive(false);
		}
	}
	
	/// <summary>
	/// 退出遊戲
	/// </summary>
	public void ExitGame(){
		Application.Quit();
	}
	#endregion
	
	#region 讀取記錄函數
	/// <summary>
	/// 讀取存檔記錄,並顯示到UI上
	/// </summary>
	void LoadRecord(string index){
		XmlNodeList recordList = Record.LoadRecord(index);
		if(recordList != null){
			this.index = index;
			string imageName = recordList.Item(1).InnerText;
			if(imageName != null && imageName.Length > 0){
				string path = "file://" + Application.dataPath + "/StreamingAssets/Xml/Persistence/Save/" + imageName + ".png";
				WWW www = new WWW(path);
				Texture image = www.texture;
				savePic.renderer.material.mainTexture = image;
				character.text = recordList.Item(2).InnerText;
				date.text = recordList.Item(3).InnerText;
				level.text = recordList.Item(4).InnerText;
				location.text = recordList.Item(5).InnerText;
				savenum.text = recordList.Item(6).InnerText;
				time.text = recordList.Item(7).InnerText;
			}else{
				clearRecord();
			}
		}
	}
	
	/// <summary>
	/// 選擇記錄,加載存檔記錄
	/// </summary>
	public void LoadGame(){
		if(index != null){
			Record.RecordName = index;
			PlayerPrefs.SetString("LevelName","LevelName");
			Application.LoadLevel("Load");
		}else{
			UIMessageBox.ShowMessage("請 選 擇 有 效 的 存 檔",3f);
		}
	}
	
	/// <summary>
	/// 清空UI存檔記錄
	/// </summary>
	public void clearRecord(){
		savePic.renderer.material.mainTexture = null;
		character.text = "";
		date.text = "";
		level.text = "";
		location.text = "";
		savenum.text = "";
		time.text = "";
		index = null;
	}
	
	public void LoadRecord1(){LoadRecord("01");}
	public void LoadRecord2(){LoadRecord("02");}
	public void LoadRecord3(){LoadRecord("03");}
	public void LoadRecord4(){LoadRecord("04");}
	public void LoadRecord5(){LoadRecord("05");}
	public void LoadRecord6(){LoadRecord("06");}
	public void LoadRecord7(){LoadRecord("07");}
	public void LoadRecord8(){LoadRecord("08");}
	#endregion
	
	#region 系統設置函數
	/// <summary>
	/// 保存系統設置
	/// </summary>
	public void SaveSetting(){
		Setting setting = new Setting();
		setting.TopInfoEff = TopInfoUI.isChecked;
		setting.JumpFontEff = JumpFontUI.isChecked;
		setting.WeatherEff = WeatherSystemUI.isChecked;
		setting.Window = (RunModeUI.selection == "Window");
		setting.BgMusicVolume = BgMusicUI.sliderValue;
		setting.SoundEffectsVolume = SoundEffectsUI.sliderValue;
		Persistence.Save("Setting",setting);
	}
	
	/// <summary>
	/// 設置背景音量
	/// </summary>
	public void SetVolumeBg(){
		if(BgMusicUI){
			float BgMusicVolume = BgMusicUI.sliderValue;
			BgMusicLabel.text = (int)(BgMusicVolume * 100) + "%";
			BgMusic.SetVolume(BgMusicVolume);
		}
	}
	
	/// <summary>
	/// 設置音效音量
	/// </summary>
	public void SetVolumeEff(){
		if(SoundEffectsUI)
			SoundEffectsLabel.text = (int)(SoundEffectsUI.sliderValue * 100) + "%";
	}
	#endregion
}
相關文章
相關標籤/搜索