目录

CommandAttribute

ICommand

ACommand

PluralityCommand

CommandComponent


简介

高版本可以直接使用异步编程简单粗暴

本人工作需要的Unity版本是随机的,从4.x到2019都需要

为了向下兼容有了以下命令组件

场景:现在有10个预制体,10个图片,10个文本需要加载,加载完成后需要对这些资源进行一些操作

肯定不能简单的写一堆回调等待加载完成,再去进行操作

CommandAttribute

namespace ETModel
{public class CommandAttribute : BaseAttribute{public string Type { get; }public CommandAttribute(string type){this.Type = type;}}
}

ICommand

using System;namespace ETModel
{public static partial class CommandType{public const string Login="Login";public const string Test = "Test";}public interface ICommand{void Init();float Progress();void Execute();bool IsDone();void Undo();void ProgressChanged();void Complete();void Failure(Exception exception);}}

ACommand

using System;namespace ETModel
{public abstract class ACommand : ICommand{protected float progress=1;public abstract void Execute();public virtual void Complete(){}public virtual void Failure(Exception exception){}public virtual void Init(){}public virtual bool IsDone(){return true;}public  float Progress(){return progress;}public virtual void ProgressChanged(){}public virtual void Undo(){}}
}

PluralityCommand

using System;
using System.Collections.Generic;namespace ETModel
{public class PluralityCommand : ICommand{float progress;Action<float> onProgressChanged;Action<Exception> onErrorEvent;CommandComponent commandComponent;List<ICommand> list = new List<ICommand>();public PluralityCommand(CommandComponent commandComponent){this.commandComponent = commandComponent;}public void AddCommand(string command,params object[] args){if (!commandComponent.commands.ContainsKey(command)){Log.Error(new Exception(string.Format("No {0} command", command)));return;}try{var type = commandComponent.commands[command];var instance = Activator.CreateInstance(type, args) as ICommand;list.Add(instance);}catch(Exception ex){Log.Error(ex);}}public void OnError(Action<Exception> action){this.onErrorEvent = action;}public void OnProgressChanged(Action<float> action){this.onProgressChanged = action;}void ICommand.Complete(){for (int i = 0; i < list.Count; i++){list[i].Complete();}}void ICommand.Execute(){for (int i = 0; i < list.Count; i++){list[i].Execute();}}void ICommand.Failure(Exception exception){if (onErrorEvent != null){onErrorEvent.Invoke(exception);}}void ICommand.Init(){for (int i = 0; i < list.Count; i++){list[i].Init();}}float ICommand.Progress(){progress = 0;for (int i=0;i<list.Count;i++){progress+=list[i].Progress();}return progress / list.Count;}void ICommand.Undo(){for (int i = 0; i < list.Count; i++){list[i].Undo();}}bool ICommand.IsDone(){bool result = true;for (int i = 0; i < list.Count; i++){if(!list[i].IsDone() ){result = false;}}return result;}void ICommand.ProgressChanged(){if (onProgressChanged == null)return;var command = this as ICommand;onProgressChanged.Invoke(command.Progress());}}
}

CommandComponent

using System;
using System.Collections.Generic;namespace ETModel
{public class CommandComponent : Entity{public static int recordMax = 50;public ICommand command;PluralityCommand pluralityCommand;public readonly Dictionary<string, Type> commands = new Dictionary<string, Type>();public readonly Queue<ICommand> queues = new Queue<ICommand>();public readonly Stack<ICommand> stacks = new Stack<ICommand>(recordMax);public void Execute(string command,params object [] args){if (!commands.ContainsKey(command)){Log.Error(new Exception(string.Format("No {0} command",command)));return;}try{var type = commands[command];var instance = Activator.CreateInstance(type, args) as ICommand;queues.Enqueue(instance);}catch(Exception ex){Log.Error(ex);}}public void Undo(){if (stacks.Count < 0)return;var command = stacks.Pop();command.Undo();}public PluralityCommand Begin(){pluralityCommand = new PluralityCommand(this);return pluralityCommand;}public void End(){if (pluralityCommand == null)return;queues.Enqueue(pluralityCommand);pluralityCommand = null;}}[ObjectSystem]public class CommandComponentAwakeSystem : AwakeSystem<CommandComponent>{public override void Awake(CommandComponent self){self.commands.Clear();List<Type> types = Game.EventSystem.GetTypes(typeof(CommandAttribute));foreach (Type type in types){object[] attrs = type.GetCustomAttributes(typeof(CommandAttribute), false);if (attrs.Length == 0){continue;}var commandAttribute = attrs[0] as CommandAttribute;if (commandAttribute == null){continue;}self.commands.Add(commandAttribute.Type, type);}}}[ObjectSystem]public class CommandComponentUpdateSystem : UpdateSystem<CommandComponent>{bool isLoadComplete;public override void Update(CommandComponent self){if (self.command == null && self.queues.Count > 0){self.command = self.queues.Dequeue();self.command.Init();}if (self.command == null)return;try{isLoadComplete = self.command.IsDone();self.command.ProgressChanged();if (isLoadComplete){self.command.Execute();self.command.Complete();if (self.stacks.Count >= CommandComponent.recordMax)self.stacks.Pop();self.stacks.Push(self.command);self.command = null;}}catch (Exception ex){self.command.Failure(ex);self.command = null;}}}
}

unity 命令模式相关推荐

  1. Unity命令模式, 实现撤销/反撤销

    可以实现记录移动的位置, 撤销和反撤销 先上图: 记录1右 记录2上 记录3上 丢弃3上 记录3左 说思路: 每一步操作都是一个"命令" 用列表记录"命令" & ...

  2. 在Unity实现游戏命令模式

    本文由开发者Najmm Shora介绍在Unity中通过使用命令模式实现回放功能,撤销功能和重做功能.我们可以使用该方法来强化自己的策略类游戏. 你是否想知道<超级食肉男孩>(Super ...

  3. 游戏设计模式:命令模式(以Unity开发坦克大战为例)

    命令模式 命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式.请求以命令的形式包裹在对象中,并传给调用对象.调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应 ...

  4. 游戏设计模式 - 命令模式在Unity中的应用(C#)

    游戏设计模式 - 命令模式在Unity中的应用(C#) 实测:这是一篇简单易懂的文章-> https://zhuanlan.zhihu.com/p/46937284

  5. [命令模式]在游戏开发中的应用

    设计模式中的每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心.这样,你就能一次又一次地使用该方案而不必做重复劳动. 一个设计模式,它的服务对象是高层模块,在设计模式中称为客 ...

  6. 游戏编程模式 - 命令模式

    edit date: 2019-11-26 16:59:39 0x00 命令模式 新坑<游戏编程模式>(作者 Robert Nystrom). 在 Unity 下,用 C# 做了简单的实现 ...

  7. 游戏设计模式---命令模式

    命令模式 游戏设计模式Design Patterns Revisited 命令模式是我最喜欢的模式之一. 大多数我写的游戏或者别的什么之类的大型程序,都会在某处用到它. 当在正确的地方使用时,它可以将 ...

  8. 【游戏设计模式】之二 论撤消重做、回放系统的实现:命令模式

    本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接: http://blog.csdn.net/poem_qianmo/article/details/52663057 作者:毛星云(浅 ...

  9. 【游戏设计模式】之二 论撤消重做 回放系统的实现 命令模式

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴!  本系列 ...

最新文章

  1. Android Cursor自动更新的实现和原理
  2. SMO算法原理转载+自己补充
  3. 【牛客 - 188D 】愤怒(01滚动数组优化dp,括号匹配方案个数,tricks)
  4. AI杂谈(2)请教支持向量机用于图像分类
  5. html里面textfield属性,StyleableTextField的CSS属性htmlText
  6. 值得借鉴的新年海报设计|PSD分层模板,图层素材随心用
  7. UI设计干货|可临摹数据可视化页面才王道
  8. 摩拜回应裁员传闻:属正常业务调整 部分岗位仍在招聘
  9. WebService的相关概念
  10. Android 反编译Apk修改资源文件并重新打包
  11. 滨江机器人餐厅_滨江机器人的视觉效果好吗?
  12. 东野圭吾《沉默的巡游》简单书评(含剧透)
  13. ASO优化:教你学会如何选词,技巧有哪些?
  14. 修改Pycharm for Mac背景色为黑灰配色
  15. python里str什么意思_python中str是什么意思
  16. optionally enclosed by
  17. Tableau画桑基图
  18. Android-开发之从掉洞到填坑之路,走进Android架构
  19. 小程序---仿百思不得姐
  20. delphi xe mysql_delphi xe 之路(27)XE7 Datasnap使用dbExpress连接MySQL-阿里云开发者社区...

热门文章

  1. Sid Meier的文明dlc+汉化教程
  2. c语言管道通信实例,C语言辅导:VC命名管道通信的实现
  3. 使用Transient noise和ac noise仿真晶体管噪声
  4. 生物、人工智能、经济、哲学和系统科学的新流派
  5. 中华会计网吉安培训中心
  6. matlab为什么生成obj文件,为文件输入和输出创建新的 System object
  7. Windows XP Professional SP2 原版
  8. mySql获取表的字段名,字段注释
  9. php拨号接口,拨号网络对应的协议是什么
  10. SQL触发器的使用及语法