關(guān)于PureMVC的相關(guān)內(nèi)容可以看我之前這個(gè)貼
http://www.lxweimin.com/p/904b36ad37e2
這次實(shí)現(xiàn)的軟件界面如下。
我們把內(nèi)容分為幾塊,這次專門負(fù)責(zé)寫View部分,我們一步一步的完成這個(gè)MVC項(xiàng)目。
Step 1
準(zhǔn)備工作,把PureMVC.DotNET.35.dll放到Plugins里面。
首先,寫UI的代碼。放到View模塊的/Scripts/View/Components/模塊里面
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
//View層
public class UserRecord : MonoBehaviour {
//UI
public UnityEngine.UI.Text title;
public UnityEngine.UI.Button Btn_rec;
public UnityEngine.UI.Button Btn_normal;
public UnityEngine.UI.Button Btn_hight;
// others
public System.Action StartRecoder;
public System.Action StopRecoder;
public System.Action PlayRecoderNomal;
public System.Action PlayRecoderHight;
//Add Listener
public void Start(){
Btn_rec.onClick.AddListener (Btn_rec_OnClick);
Btn_normal.onClick.AddListener (Btn_normal_OnClick);
Btn_hight.onClick.AddListener (Btn_hight_OnClick);
}
//判斷是否在錄音
public bool isRecord = false;
//點(diǎn)擊錄制的按鈕
public void Btn_rec_OnClick(){
Debug.Log ("點(diǎn)擊錄制的按鈕");
if (isRecord == false) {
StartRecoder ();
isRecord = true;
} else {
StopRecoder ();
isRecord = false;
}
}
//點(diǎn)擊正常播放的按鈕
public void Btn_normal_OnClick(){
Debug.Log ("點(diǎn)擊正常播放的按鈕");
PlayRecoderNomal ();
}
//點(diǎn)擊高音播放的按鈕
public void Btn_hight_OnClick(){
Debug.Log ("點(diǎn)擊高音播放的按鈕");
PlayRecoderHight ();
}
//改變應(yīng)用的標(biāo)題
public void ChangeTitle(string myTitle)
{
Debug.Log ("開始改變標(biāo)題的文字");
title.text = myTitle;
}
//改變中間按鈕的文字部分
public void ChangeButtonText(string myText)
{
Btn_rec.GetComponentInChildren<UnityEngine.UI.Text> ().text = myText;
}
}
其中,該腳本需要附在Unity3d的編輯器UI的局部位置之中,需要拉動(dòng)相關(guān)的UI依賴的。如圖。
Step 2
然后,寫剛才UI對(duì)應(yīng)的Mediator的代碼,放在/Scripts/View/模塊里面
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
using System.Collections.Generic;
public class UserRecordMediator : Mediator, IMediator {
//初始化
private UserRecord View
{
get { return (UserRecord)ViewComponent; }
}
//構(gòu)造函數(shù)
public UserRecordMediator(UserRecord viewComponent):base(NAME, viewComponent)
{
Debug.Log("進(jìn)入Mediator()的構(gòu)造函數(shù)");
// others
View.StartRecoder += Listener_StartRecoder;
View.StopRecoder += Listener_StopRecoder;
View.PlayRecoderNomal += Listener_PlayRecoderNomal;
View.PlayRecoderHight += Listener_PlayRecoderHight;
}
//接收按鈕的消息
public void Listener_StartRecoder(){
Debug.Log("進(jìn)入Mediator()的StartRecoder");
}
public void Listener_StopRecoder(){
Debug.Log("進(jìn)入Mediator()的StopRecoder");
}
public void Listener_PlayRecoderNomal(){
Debug.Log("進(jìn)入Mediator()的PlayRecoderNomal");
}
public void Listener_PlayRecoderHight(){
Debug.Log("進(jìn)入Mediator()的PlayRecoderHight");
}
//接收廣播的監(jiān)聽
public override void HandleNotification(INotification note)
{
switch (note.Name)
{
case EventsEnum.STARTUP:
View.ChangeTitle ("歡迎使用錄音變聲軟件 Make By @小小酥XX");
break;
}
}
}
Step 3
接著,新建一個(gè)處理該UI的界面RecordUI,放在/Scripts/目錄下。附上如下的代碼。
using UnityEngine;
using System.Collections;
public class RecordUI : MonoBehaviour {
public UserRecord myRecord;
void Awake()
{
//啟動(dòng)PureMVC程序
ApplicationFacade facade = ApplicationFacade.Instance as ApplicationFacade;
facade.Startup(this);
}
}
之后,把這個(gè)啟動(dòng)UI的代碼拉倒Unity3D總的UI控制器上,以便它一啟動(dòng)就被調(diào)用,并且還要把剛剛局部UI控制的腳本拉到其依賴關(guān)系上。如下圖所示。
Step 4
然后,我們需要修改PureMVC的啟動(dòng)文件
Scripts/ApplicationFacade,以使得它能正確調(diào)用剛剛的RecodrdUI。
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
public class ApplicationFacade : Facade
{
//單例啟動(dòng)
public new static IFacade Instance
{
get
{
if(m_instance == null)
{
lock(m_staticSyncRoot)
{
if (m_instance == null)
{
Debug.Log("啟動(dòng)PureMVC的入口函數(shù)ApplicationFacade");
m_instance = new ApplicationFacade();
}
}
}
return m_instance;
}
}
//開始執(zhí)行
public void Startup(RecordUI r)
{
Debug.Log("Startup()函數(shù),發(fā)送消息EventsEnum.STARTUP到RecordUI的UI總控制那里");
SendNotification(EventsEnum.STARTUP, r);
}
//初始化
protected override void InitializeController()
{
Debug.Log("初始化PureMVC框架");
base.InitializeController();
RegisterCommand(EventsEnum.STARTUP, typeof(StartupCommand));
RegisterCommand(EventsEnum.DELETE_USER, typeof(DeleteUserCommand));
}
}
Step 5
最后,我們對(duì)Scipts/Controller/StartupCommand這個(gè)控制器進(jìn)行簡(jiǎn)單修改,以滿足其初始化的調(diào)用。
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
public class StartupCommand : SimpleCommand, ICommand
{
public override void Execute(INotification notification)
{
Debug.Log("執(zhí)行StartupCommand.Execute()的函數(shù)");
RecordUI r = notification.Body as RecordUI;
Facade.RegisterMediator(new UserRecordMediator(r.myRecord));
}
}
最后,如果成功的話。應(yīng)該可以看到如下的記錄。
至此,我們已經(jīng)完整的完成了一個(gè)PureMVC項(xiàng)目的View部分代碼的大致編寫。其中Controller和Model部分我們下次再繼續(xù)探討。