Unity3d MVCS游戏框架Robotlegs
来源:互联网 发布:手机上怎样改淘宝评价 编辑:程序博客网 时间:2024/06/09 14:04
最近在一个新项目中需要使用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派发器 将派发到mediatordispatcher.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层上的派发器
后两种四监听自定义的派发器
阅读全文
1 0
- Unity3d MVCS游戏框架Robotlegs
- StrangeIoc MVCS游戏框架
- Robotlegs框架
- ROBOTLEGS轻量级AS3框架
- RobotLegs轻量级AS3框架
- ROBOTLEGS轻量级AS3框架
- RobotLegs轻量级AS3框架
- ROBOTLEGS轻量级AS3框架
- RobotLegs轻量级AS3框架
- ROBOTLEGS轻量级AS3框架
- Unity框架_MVC与MVCS
- 让Robotlegs支持Starling框架
- StrangeIOC MVCS框架介绍及进阶
- StrangeIoc MVCS框架第一课:初步理解
- 【StrangeIOC框架】 通过一个实例来理解MVCS结构
- Unity3D技术之游戏框架设计
- Unity3D游戏开发框架-消息机制
- Unity3D游戏开发框架-资源管理类ResourceManage
- 深入理解javascript原型和闭包(12)——简介【作用域】
- word2vec
- 使用Java读取Excel文件内容
- 深入理解javascript原型和闭包(13)-【作用域】和【上下文环境】
- 软件开发流程回顾
- Unity3d MVCS游戏框架Robotlegs
- Docker——常用命令(四)
- java集合系列01--ArrayList
- Android APP测试的日志文件抓取
- CJOJ 1070 【Uva】嵌套矩形
- leetcode 122. Best Time to Buy and Sell Stock II
- scala基础系列一
- 递归与分治策略之大整数的乘法
- 深入理解javascript原型和闭包(14)——从【自由变量】到【作用域链】