【連載1】PureMVC和Unity3D的UGUI制作一個錄音變聲小軟件

關于PureMVC的相關內容可以看我之前這個貼
http://www.lxweimin.com/p/904b36ad37e2

這次實現的軟件界面如下。


示意圖.png

我們把內容分為幾塊,這次專門負責寫View部分,我們一步一步的完成這個MVC項目。

Step 1

準備工作,把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;

    //點擊錄制的按鈕
    public void Btn_rec_OnClick(){
        Debug.Log ("點擊錄制的按鈕");
        if (isRecord == false) {
            StartRecoder ();
            isRecord = true;
        } else {
            StopRecoder ();
            isRecord = false;
        }
    }

    //點擊正常播放的按鈕
    public void Btn_normal_OnClick(){
        Debug.Log ("點擊正常播放的按鈕");
        PlayRecoderNomal ();
    }

    //點擊高音播放的按鈕
    public void Btn_hight_OnClick(){
        Debug.Log ("點擊高音播放的按鈕");
        PlayRecoderHight ();
    }

    //改變應用的標題
    public void ChangeTitle(string myTitle)
    {
        Debug.Log ("開始改變標題的文字");
        title.text = myTitle;
    }

    //改變中間按鈕的文字部分
    public void ChangeButtonText(string myText)
    {
        Btn_rec.GetComponentInChildren<UnityEngine.UI.Text> ().text = myText;
    }
}

其中,該腳本需要附在Unity3d的編輯器UI的局部位置之中,需要拉動相關的UI依賴的。如圖。

拉組件依賴.png

Step 2

然后,寫剛才UI對應的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; }
    }

    //構造函數
    public UserRecordMediator(UserRecord viewComponent):base(NAME, viewComponent)
    {
        Debug.Log("進入Mediator()的構造函數");

        // others
        View.StartRecoder += Listener_StartRecoder;
        View.StopRecoder += Listener_StopRecoder;
        View.PlayRecoderNomal += Listener_PlayRecoderNomal;
        View.PlayRecoderHight += Listener_PlayRecoderHight;
    }

    //接收按鈕的消息
    public void Listener_StartRecoder(){
        Debug.Log("進入Mediator()的StartRecoder");
    }

    public void Listener_StopRecoder(){
        Debug.Log("進入Mediator()的StopRecoder");
    }

    public void Listener_PlayRecoderNomal(){
        Debug.Log("進入Mediator()的PlayRecoderNomal");
    }

    public void Listener_PlayRecoderHight(){
        Debug.Log("進入Mediator()的PlayRecoderHight");
    }

    //接收廣播的監聽
    public override void HandleNotification(INotification note)
    {
        switch (note.Name)
        {
        case EventsEnum.STARTUP:
            View.ChangeTitle ("歡迎使用錄音變聲軟件 Make By @小小酥XX");
            break;
        }
    }
}

Step 3

接著,新建一個處理該UI的界面RecordUI,放在/Scripts/目錄下。附上如下的代碼。

using UnityEngine;
using System.Collections;

public class RecordUI : MonoBehaviour {

    public UserRecord myRecord;

    void Awake()
    {
        //啟動PureMVC程序
        ApplicationFacade facade = ApplicationFacade.Instance as ApplicationFacade;
        facade.Startup(this);
    }
}

之后,把這個啟動UI的代碼拉倒Unity3D總的UI控制器上,以便它一啟動就被調用,并且還要把剛剛局部UI控制的腳本拉到其依賴關系上。如下圖所示。

啟動RecordUI.png

Step 4

然后,我們需要修改PureMVC的啟動文件
Scripts/ApplicationFacade,以使得它能正確調用剛剛的RecodrdUI。

using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;

public class ApplicationFacade : Facade
{
    //單例啟動
    public new static IFacade Instance
    {
        get
        {
            if(m_instance == null)
            {
                lock(m_staticSyncRoot)
                {
                    if (m_instance == null)
                    {
                        Debug.Log("啟動PureMVC的入口函數ApplicationFacade");
                        m_instance = new ApplicationFacade();
                    }
                }
            }
            return m_instance;
        }
    }

    //開始執行
    public void Startup(RecordUI r)
    {
        Debug.Log("Startup()函數,發送消息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

最后,我們對Scipts/Controller/StartupCommand這個控制器進行簡單修改,以滿足其初始化的調用。

using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;

public class StartupCommand : SimpleCommand, ICommand
{
    public override void Execute(INotification notification)
    {
        Debug.Log("執行StartupCommand.Execute()的函數");
        RecordUI r = notification.Body as RecordUI;
        Facade.RegisterMediator(new UserRecordMediator(r.myRecord));
    }
}

最后,如果成功的話。應該可以看到如下的記錄。

DebugLog窗口.png

至此,我們已經完整的完成了一個PureMVC項目的View部分代碼的大致編寫。其中Controller和Model部分我們下次再繼續探討。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容