Unity3d MVCS遊戲框架Robotlegs

最近在一個新項目中需要使用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>();
	}
}


進行了這一系列配置後,可能你的項目需要從服務器中讀取數據
這個Demo在運行後將隨機生成一個[0,100]的整數,當點擊這個數後將持續增大

View

在View層內,有view與mediator兩個字分塊
view主要實現與客戶端界面的交互,mediator是View層與Controller層交互的中間介紐帶

MyTextView.cs
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));
	}
}


MyTextMediator.cs
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

在config上對事件進行是事件綁定後,將調用對應的Command
要向服務器發送數據請求,這裏我定義一個RequestCommand
RequestCommand.cs
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);
	}
}


config中對model與service進行注入綁定
		//注入綁定
		injector.Map<InterfaceScoreService>().ToSingleton<ScoreService>();
		injector.Map<ScoreModel>().ToSingleton<ScoreModel>();


Service

在service上定義一個派發器,在Command進行監聽,在Service派發數據
ScoreService.cs
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

從服務器上接收到數據後,在Command上進行對model進行數據處理(存儲、修改等操作)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreModel
{
	public int score { get; set; }

	public int Add()
	{
		return this.score++;
	}
}

這樣,運行程序後,向服務器發送數據請求,並呈現給客戶端

點擊事件的對Model層存儲了的數據進行處理了
這裏就不給大家介紹了,可以參考源碼學習!

這裏需要注意的是,兩種派發器的監聽
//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層上的派發器
後兩種四監聽自定義的派發器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章