Unity游戏开发设计模式—备忘录模式
概要
备忘录模式:在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可以在以后将对象恢复到原想保存的状态
需求
策划来了个需求,屏幕里有个苹果。
需求是:
策划可以随便修改它的大小,旋转,位置,按回车键保存它的信息。
可以按数字键可以实现撤销N步,回到刚才的某一步。
实现
在Apple.cs里添加方法Restore和CreateMemento方法
// 创建备忘录
// create memento
internal AppleMemento CreateMemento()
{
return new AppleMemento(this);
}
// 从备忘录中恢复
// restore from memento
internal void Restore(AppleMemento memento)
{
transform.position = memento.GetStartPos();
transform.localScale = memento.GetScale();
transform.rotation = memento.GetRotation();
}
添加AppleMemento类来实现状态存储