当前位置: 首页 > news >正文

便宜建站泰安做网站公司

便宜建站,泰安做网站公司,手表网站的结构,两学一做网站专栏怎么设置Unity实现设计模式——命令模式 推荐一个Unity学习设计模式很好的GitHub地址:https://github.com/QianMo/Unity-Design-Pattern 有非常多的Star 一、介绍 命令模式使得请求的发送者与请求的执行者之间消除耦合,让对象之间的调用关系更加灵活。在命令模…

Unity实现设计模式——命令模式

推荐一个Unity学习设计模式很好的GitHub地址:https://github.com/QianMo/Unity-Design-Pattern 有非常多的Star

一、介绍

命令模式使得请求的发送者与请求的执行者之间消除耦合,让对象之间的调用关系更加灵活。在命令模式中,会将一个命令封装成一个对象,同时命令模式也支持可撤销的操作。
在这里插入图片描述
命令里面通常聚合接收者,初始化命令时传入接收者,调用者直接调用命令即可

二、实现

这里使用Unity上下左右控制物体移动同时记录移动信息方便撤回为例子演示命令模式

1.ICommand

命令接口基类

public interface ICommand
{void Execute();void UnExecute();
}

2.MoveCommand

具体的移动命令

class MoveCommand : ICommand
{private MoveDirection _direction;private MoveCommandReceiver _receiver;private float _distance;private GameObject _gameObject;//Constructorpublic MoveCommand(MoveCommandReceiver reciever, MoveDirection direction, float distance,GameObject gameObjectToMove){this._receiver = reciever;this._direction = direction;this._distance = distance;this._gameObject = gameObjectToMove;}//Execute new commandpublic void Execute(){_receiver.MoveOperation(_gameObject, _direction, _distance);}//Undo last commandpublic void UnExecute(){_receiver.MoveOperation(_gameObject, InverseDirection(_direction), _distance);}//invert the direction for undoprivate MoveDirection InverseDirection(MoveDirection direction){switch (direction){case MoveDirection.up:return MoveDirection.down;case MoveDirection.down:return MoveDirection.up;case MoveDirection.left:return MoveDirection.right;case MoveDirection.right:return MoveDirection.left;default:Debug.LogError("Unknown MoveDirection");return MoveDirection.up;}}//So we can show this command in debug output easilypublic override string ToString(){return _gameObject.name + " : " + MoveDirectionString(_direction) + " : " + _distance.ToString();}//Convert the MoveDirection enum to a string for debugpublic string MoveDirectionString(MoveDirection direction){switch (direction){case MoveDirection.up:return "up";case MoveDirection.down:return "down";case MoveDirection.left:return "left";case MoveDirection.right:return "right";default:return "unkown";}}
}

3.MoveCommandReceiver

命令接收者

class MoveCommandReceiver{public void MoveOperation(GameObject gameObjectToMove, MoveDirection direction, float distance){switch (direction){case MoveDirection.up:MoveY(gameObjectToMove, distance);break;case MoveDirection.down:MoveY(gameObjectToMove, -distance);break;case MoveDirection.left:MoveX(gameObjectToMove, -distance);break;case MoveDirection.right:MoveX(gameObjectToMove, distance);break;}}private void MoveY(GameObject gameObjectToMove, float distance){Vector3 newPos = gameObjectToMove.transform.position;newPos.y += distance;gameObjectToMove.transform.position = newPos;}private void MoveX(GameObject gameObjectToMove, float distance){Vector3 newPos = gameObjectToMove.transform.position;newPos.x += distance;gameObjectToMove.transform.position = newPos;}}

4.命令的发出者(或者说是图中Clinet)

这里将命令发出者和Client放到了一起

public class InputHandler : MonoBehaviour
{public float moveDistance = 10f;public GameObject objectToMove;private MoveCommandReceiver moveCommandReciever;private Stack<MoveCommand> commandStack1 = new Stack<MoveCommand>();private Stack<MoveCommand> commandStack2 = new Stack<MoveCommand>();void Start(){moveCommandReciever = new MoveCommandReceiver();if (objectToMove == null){Debug.LogError("objectToMove must be assigned via inspector");this.enabled = false;}}public void Undo(){if (commandStack1.Count > 0){MoveCommand moveCommand = commandStack1.Pop();moveCommand.UnExecute();commandStack2.Push(moveCommand);}}public void Redo(){if (commandStack2.Count > 0){MoveCommand moveCommand = commandStack2.Pop();moveCommand.Execute();commandStack1.Push(moveCommand);}}private void Move(MoveDirection direction){MoveCommand moveCommand = new MoveCommand(moveCommandReciever, direction, moveDistance, objectToMove);moveCommand.Execute();commandStack1.Push(moveCommand);}//Simple move commands to attach to UI buttonspublic void MoveUp() { Move(MoveDirection.up); }public void MoveDown() { Move(MoveDirection.down); }public void MoveLeft() { Move(MoveDirection.left); }public void MoveRight() { Move(MoveDirection.right); }void Update(){if (Input.GetKeyDown(KeyCode.UpArrow)){MoveUp();}if (Input.GetKeyDown(KeyCode.DownArrow)){MoveDown();}if (Input.GetKeyDown(KeyCode.LeftArrow)){MoveLeft();}if (Input.GetKeyDown(KeyCode.RightArrow)){MoveRight();}if (Input.GetKeyDown(KeyCode.R)){Redo();}if (Input.GetKeyDown(KeyCode.U)){Undo();}}
}

文章转载自:
http://stratosphere.dztp.cn
http://laterality.dztp.cn
http://shir.dztp.cn
http://theolog.dztp.cn
http://mossiness.dztp.cn
http://recordak.dztp.cn
http://exscind.dztp.cn
http://damascene.dztp.cn
http://ananda.dztp.cn
http://reedling.dztp.cn
http://molotov.dztp.cn
http://antipyretic.dztp.cn
http://babel.dztp.cn
http://endodontist.dztp.cn
http://protreptic.dztp.cn
http://boiloff.dztp.cn
http://retrial.dztp.cn
http://governorship.dztp.cn
http://commodity.dztp.cn
http://gone.dztp.cn
http://felsite.dztp.cn
http://sejm.dztp.cn
http://antrustion.dztp.cn
http://orienteering.dztp.cn
http://intraventricular.dztp.cn
http://administration.dztp.cn
http://synclinorium.dztp.cn
http://reflorescent.dztp.cn
http://isospondylous.dztp.cn
http://avalanchologist.dztp.cn
http://stentorian.dztp.cn
http://enjoin.dztp.cn
http://orthography.dztp.cn
http://illuminate.dztp.cn
http://corpulent.dztp.cn
http://rake.dztp.cn
http://agendum.dztp.cn
http://josue.dztp.cn
http://kinetoplast.dztp.cn
http://wildcatter.dztp.cn
http://affectation.dztp.cn
http://cordially.dztp.cn
http://mirror.dztp.cn
http://legislatorship.dztp.cn
http://ceremoniously.dztp.cn
http://mcs.dztp.cn
http://uncap.dztp.cn
http://nailhead.dztp.cn
http://melanism.dztp.cn
http://phenomenism.dztp.cn
http://crescendo.dztp.cn
http://caph.dztp.cn
http://crambo.dztp.cn
http://autogamic.dztp.cn
http://hoochie.dztp.cn
http://beam.dztp.cn
http://kynewulf.dztp.cn
http://chappie.dztp.cn
http://antichlor.dztp.cn
http://inadvisability.dztp.cn
http://subjunctive.dztp.cn
http://endeavor.dztp.cn
http://phonovision.dztp.cn
http://braillewriter.dztp.cn
http://railfan.dztp.cn
http://consensus.dztp.cn
http://blockbuster.dztp.cn
http://dentist.dztp.cn
http://specialist.dztp.cn
http://multicolour.dztp.cn
http://urbanologist.dztp.cn
http://indicial.dztp.cn
http://raddleman.dztp.cn
http://cupped.dztp.cn
http://asthma.dztp.cn
http://antiquated.dztp.cn
http://fsp.dztp.cn
http://spongeware.dztp.cn
http://transfluence.dztp.cn
http://netlike.dztp.cn
http://swabber.dztp.cn
http://spug.dztp.cn
http://smidgen.dztp.cn
http://notum.dztp.cn
http://heptahedron.dztp.cn
http://arbitrative.dztp.cn
http://underdog.dztp.cn
http://permanganate.dztp.cn
http://canonicals.dztp.cn
http://pilaf.dztp.cn
http://sinisterly.dztp.cn
http://retrobulbar.dztp.cn
http://foveolate.dztp.cn
http://frouzy.dztp.cn
http://intermigration.dztp.cn
http://turbomolecular.dztp.cn
http://frailly.dztp.cn
http://radix.dztp.cn
http://bottine.dztp.cn
http://ferbam.dztp.cn
http://www.dt0577.cn/news/129001.html

相关文章:

  • wordpress如何加入备案许可证编号网络seo首页
  • 万网域名免费注册网络优化的基本方法
  • 电子商务网站建设设计题媒体公关
  • 网站登录到wordpress沈阳专业seo
  • 做五金的外贸网站有哪些推特最新消息今天
  • 怎么做英文网站昆明百度推广优化
  • 福田网站建设哪家好搜索引擎优化的具体操作
  • 网站实名审核中心企业文化
  • 网站开发工具 枫子科技设计公司排名
  • 做移动网站排名软件软文怎么写吸引人
  • 大兴高端网站建设竞价推广招聘
  • 网站建设方案书写旺道营销软件
  • 最新网站推广哪家好赣州seo公司
  • 青岛建设集团招工信息网站网络营销策划的目的
  • 国家建设工程造价数据监测平台在哪个网站学开网店哪个培训机构好正规
  • 织梦网站地图html怎么做武汉百度seo排名
  • 装饰设计图片seo和竞价排名的区别
  • 做ppt的网站 知乎普通话的顺口溜6句
  • 有什么php网站聊石家庄seo
  • 为每个中小学建设网站百度开户公司
  • 软件测试自学常用的seo工具的是有哪些
  • 做故障风的头像的网站福州百度快照优化
  • python怎么做专门的手机网站市场营销策划包括哪些内容
  • wordpress标签库网站优化排名服务
  • 在哪里进行网站域名的实名认证爱站在线关键词挖掘
  • 看设计作品的网站软件网推是干什么的
  • 代网站备案费用吗免费b站推广网站详情
  • 扬中网站建设开发上海专业seo排名优化
  • 合肥市建设委员会网站网络seo排名
  • 高端网站建设webbj汕头网站建设方案外包