Giter VIP home page Giter VIP logo

puremvc-unity-demo's Introduction

PureMVC for Unity

幾個月前改用 Unity 開發遊戲,到目前的心得為:組件式開發真的是很便利,但是當組件數量多到一定程度時,結構上就有點可怕,常常在某 GameObject 上掛了組件後就忘了它的存在,雖然可以使用 Singleton design pattern 來製作主要的 Manager(本人對 Singleton 並不是很熱愛),程式還是會亂到一定程度,搜尋了一些 Unity with MVC 討論,一部分的人都對實作 MVC 不是很熱絡,也許是 Unity 特有的開發環境導致。

以前開發 Adobe Flex 專案最愛用的 MVC Framework 就是 PureMVC,即使後來有更方便的 MVC Framework 的也擋不住我對它的熱愛。Unity 是沒有所謂的全域 Root Scene,所有場景都是獨立,想要將 AS3 實作邏輯套用在 Unity 上將控制項都在 PureMVC 架構中實作是有點矯情多餘。如何保持 Unity 組件開發模式,導入 PureMVC 鬆綁主要邏輯,就是這次實驗的重點。

不清楚 PureMVC 的朋友們可以到這邊參觀一下:PureMVC 我也會

PureMVC C# Standard Framework on GitHub

ViewComponent 與 Mediator 整合是首要工作:

由於 Unity 沒有全域 Root Scene,如果將 new Mediator( viewComponent ) 寫在 PureMVC 架構下,即使透過 GameObject.Find 找那個對應的 GameObject 就轉了九彎十八拐,寫起來一點都不愉快,尤其考慮到場景的轉換,兩個場景中相關 Mediator 的註冊與移除處理,何況對 Unity 組件來說,能不能用被打包動態載入是件重要的事。綜合以上問題點,反向思考,改由 GameObject 掛載中介組件,在 OnEnable 與 OnDisable 通知 Facade 去註冊與移除其 Mediator,一來簡化為了實作 Meditaor 掛載 ViewComponent 而對 static class GameObject 的依賴,二來也不會對 Unity 組件開發模式有太大的影響。

// IMediatorPlug 用來處理設定 Meditaor 資料的 Unity 中介組件(當然也可以由程式指定)
public interface IMediatorPlug{
    void Connect();
    void Disconnect();
    string GetName();
    string GetClassRef();
    //傳送 ViewComponent
    UnityEngine.Object GetView();
}

//Mediator 中,需要實作兩個屬性的 Constructor
public CustomMediator(string mediatorName, object viewComponent ):base(mediatorName, viewComponent ) {}

還有一支處理 IMediatorPlug 串接的 UnityFacade ,主要兩個處理方法為:

public void ConnectMediator( IMediatorPlug item ){...}
public void DisconnectMediator( string mediatorName ){...}

修改 MediatorPlug 的執行順序

為確保 MediatorPlug 執行時間在場景起始後,需修改 Script Execution Order。打開 Edit / Project Settings / Script Execution Order,修改如下:

執行結果:

最後試驗結果挺令人滿意的,轉換場景後也沒有垃圾留下來,測試專案內寫了兩種 ViewComponent <-> Mediator 與 Proxy 更新範例。

puremvc-unity-demo's People

Contributors

erinlin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.