最近在一個新項目中需要使用Robotlegs這套框架來重構代碼
博主學習了下這套開源框架,還是很容易上手的
源碼下載地址點擊打開鏈接
博主學習了這套框架後也寫了一個Demo
本文內容是圍繞這個Demo來寫的,大家可以下載參考Demo來學習點擊打開鏈接
框架入口
using System.Collections;
using System.Collections.Generic;
using Robotlegs.Bender.Framework.API;
using Robotlegs.Bender.Framework.Impl;
using Robotlegs.Bender.Platforms.Unity.Bundles;
using Robotlegs.Bender.Platforms.Unity.Extensions.ContextViews.Impl;
using UnityEngine;
public class MyText : MonoBehaviour
{
IContext context;
public void Start()
{
context = new Context();
context.Install<UnitySingleContextBundle>().Configure<MyTextConfig>().Configure(new TransformContextView(this.transform));
}
}
Config
實例化框架後將安裝配置信息using System;
using System.Collections;
using System.Collections.Generic;
using Robotlegs.Bender.Extensions.EventCommand.API;
using Robotlegs.Bender.Extensions.Mediation.API;
using Robotlegs.Bender.Framework.API;
using UnityEngine;
public class MyTextConfig : IConfig
{
[Inject]
public IEventCommandMap eventCommandMap;
[Inject]
public IMediatorMap mediatorMap;
[Inject]
public IInjector injector;
[Inject]
public IContext context;
public void Configure()
{
//中間介綁定
mediatorMap.Map<MyTextView>().ToMediator<MyTextMediator>();
//事件綁定
eventCommandMap.Map(ViewEvent.Type.REQUEST).ToCommand<RequestCommand>();
eventCommandMap.Map(ViewEvent.Type.ADD).ToCommand<AddCommand>();
//注入綁定
injector.Map<InterfaceScoreService>().ToSingleton<ScoreService>();
injector.Map<ScoreModel>().ToSingleton<ScoreModel>();
//數據綁定後調用
context.AfterInitializing(StartApplication);
}
private void StartApplication()
{
GameObject gameobj = GameObject.FindGameObjectWithTag("Text");
gameobj.AddComponent<MyTextView>();
}
}
進行了這一系列配置後,可能你的項目需要從服務器中讀取數據
View
using Robotlegs.Bender.Platforms.Unity.Extensions.Mediation.Impl;
using UnityEngine.UI;
public class MyTextView : EventView
{
protected override void Start()
{
base.Start();
dispatcher.Dispatch(new ViewEvent(ViewEvent.Type.REQUEST));
this.GetComponent<Button>().onClick.AddListener(OnBtnClicked);
}
//數據更新
public void UpdateScore(int score)
{
this.GetComponent<Text>().text = score.ToString();
}
//點擊事件
private void OnBtnClicked()
{
//View派發器 將派發到mediator
dispatcher.Dispatch(new ViewEvent(ViewEvent.Type.ADD));
}
}
using System.Collections;
using System.Collections.Generic;
using Robotlegs.Bender.Bundles.MVCS;
using Robotlegs.Bender.Extensions.EventManagement.API;
using UnityEngine;
public class MyTextMediator : Mediator
{
//注入自身對象下的MyTextView組件
[Inject]
public MyTextView myTextView;
//當全部的屬性變量注入對象成功的時候,將調用該函數
public override void Initialize()
{
//view派發器的監聽
AddViewListener<ViewEvent>(ViewEvent.Type.REQUEST, Dispatch);
AddViewListener<ViewEvent>(ViewEvent.Type.ADD, Dispatch);
//context自定義派發器的監聽
AddContextListener<CommandEvent>(CommandEvent.Type.REQUEST, UpdateScore);
AddContextListener<CommandEvent>(CommandEvent.Type.ADD, UpdateScore);
}
private void UpdateScore(CommandEvent evt)
{
myTextView.UpdateScore(evt.score);
}
}
Controller
using System;
using System.Collections;
using System.Collections.Generic;
using Robotlegs.Bender.Extensions.CommandCenter.API;
using Robotlegs.Bender.Extensions.EventManagement.API;
using UnityEngine;
public class RequestCommand : ICommand
{
[Inject]
public InterfaceScoreService scoreService;
[Inject]
public ScoreModel scoreModel;
[Inject]
public IEventDispatcher dispatcher;
//當命定被使用後開始調用該函數
public void Execute()
{
Debug.Log("Command...");
scoreService.dispatcher.AddEventListener<ServiceEvent>(ServiceEvent.Type.REQUEST, GetRequestScore);
scoreService.OnRequestScore("127.0.0.1");
}
public void GetRequestScore(ServiceEvent evt)
{
scoreModel.score = evt.score;
dispatcher.Dispatch(new CommandEvent(CommandEvent.Type.REQUEST, scoreModel.score));
scoreService.dispatcher.RemoveEventListener<ServiceEvent>(ServiceEvent.Type.REQUEST, GetRequestScore);
}
}
//注入綁定
injector.Map<InterfaceScoreService>().ToSingleton<ScoreService>();
injector.Map<ScoreModel>().ToSingleton<ScoreModel>();
Service
using System;
using System.Collections;
using System.Collections.Generic;
using Robotlegs.Bender.Extensions.EventManagement.API;
using Robotlegs.Bender.Extensions.EventManagement.Impl;
public class ScoreService : InterfaceScoreService
{
[Inject]
public IEventDispatcher dispatcher { get; set; }
public void OnReceiveScore()
{
}
public void OnRequestScore(string url)
{
UnityEngine.Debug.Log("向服務器發送請求");
int score = UnityEngine.Random.Range(0, 100);
dispatcher.Dispatch(new ServiceEvent(ServiceEvent.Type.REQUEST,score));
}
public void UpdateScore(string url, int score)
{
}
}
Model
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScoreModel
{
public int score { get; set; }
public int Add()
{
return this.score++;
}
}
這樣,運行程序後,向服務器發送數據請求,並呈現給客戶端
//view派發器的監聽
AddViewListener<ViewEvent>(ViewEvent.Type.REQUEST, Dispatch);
AddViewListener<ViewEvent>(ViewEvent.Type.ADD, Dispatch);
//context自定義派發器的監聽
AddContextListener<CommandEvent>(CommandEvent.Type.REQUEST, UpdateScore);
AddContextListener<CommandEvent>(CommandEvent.Type.ADD, UpdateScore);
前兩種是監聽View層上的派發器