1+ //-------------------------------------------------------------------------------------
2+ // MementoExample2.cs
3+ //-------------------------------------------------------------------------------------
4+
5+ using UnityEngine ;
6+ using System . Collections . Generic ;
7+
8+ /*
9+ * provides a way to store previous states of an object easily
10+ *
11+ * memento: the basic object that is stored in different states
12+ *
13+ * originator: sets and gets values from the currently targeted memento. Creates new memenots and assigns current values to them
14+ *
15+ * caretaker: holds an list that contains all previous versions of the memento. it can store and retrieve stored mementos
16+ *
17+ *
18+ * */
19+
20+ namespace MementoExample2
21+ {
22+
23+
24+ public class MementoExample2 : MonoBehaviour
25+ {
26+
27+ Caretaker caretaker = new Caretaker ( ) ;
28+
29+ Originator originator = new Originator ( ) ;
30+
31+ int savedFiles = 0 , currentArticle = 0 ;
32+
33+ void Start ( )
34+ {
35+ // here we do some virtual typing and saving texts:
36+ Save ( "Tex1: Hello World, this is text example 1" ) ;
37+ Save ( "Text2: Ok here comes example number 2." ) ;
38+ Save ( "Text3: And example number 3. Just testing." ) ;
39+ Save ( "Text4: ...." ) ;
40+
41+ // Here we do some virtual button pressing
42+ Debug . Log ( "Pressing Undo" ) ;
43+ Undo ( ) ;
44+ Debug . Log ( "Pressing Undo" ) ;
45+ Undo ( ) ;
46+ Debug . Log ( "Pressing Undo" ) ;
47+ Undo ( ) ;
48+ Debug . Log ( "Pressing Redo" ) ;
49+ Redo ( ) ;
50+ }
51+
52+
53+ // these methods below might get called when someone is pressing a button
54+ // you could easily implement it with unitys new ui system :)
55+ public void Save ( string text )
56+ {
57+ originator . Set ( text ) ;
58+ caretaker . Add ( originator . StoreInMemento ( ) ) ;
59+ savedFiles = caretaker . GetCountOfSavedArticles ( ) ;
60+ currentArticle = savedFiles ;
61+ }
62+
63+ public string Undo ( )
64+ {
65+ if ( currentArticle > 0 )
66+ currentArticle -= 1 ;
67+
68+ Memento prev = caretaker . Get ( currentArticle ) ;
69+ string prevArticle = originator . RestoreFromMemento ( prev ) ;
70+ return prevArticle ;
71+ }
72+
73+ public string Redo ( )
74+ {
75+ if ( currentArticle < savedFiles )
76+ currentArticle += 1 ;
77+
78+ Memento next = caretaker . Get ( currentArticle ) ;
79+ string nextArticle = originator . RestoreFromMemento ( next ) ;
80+ return nextArticle ;
81+ }
82+
83+ }
84+
85+
86+
87+
88+ /// <summary>
89+ /// the basic object that is stored in different states
90+ /// </summary>
91+ public class Memento
92+ {
93+ public string article { get ; protected set ; }
94+
95+ // Base Memento class that in this case just stores article strings!:)
96+ public Memento ( string article )
97+ {
98+ this . article = article ;
99+ }
100+ }
101+
102+
103+ /// <summary>
104+ /// sets and gets values from the currently targeted memento. Creates new memenots and assigns current values to them.
105+ /// </summary>
106+ public class Originator
107+ {
108+ public string article { get ; protected set ; }
109+
110+ public void Set ( string article )
111+ {
112+ Debug . Log ( "From Originator: Current Version of article is: [\" " + article + "\" ]" ) ;
113+ this . article = article ;
114+ }
115+
116+ public Memento StoreInMemento ( )
117+ {
118+ Debug . Log ( "From Originator: Saving in Memento: [\" " + this . article + "\" ]" ) ;
119+ return new Memento ( this . article ) ;
120+ }
121+
122+ public string RestoreFromMemento ( Memento memento )
123+ {
124+ article = memento . article ;
125+ Debug . Log ( "From Originator: Previous Article saved in Memento: [\" " + article + "\" ]" ) ;
126+ return article ;
127+ }
128+ }
129+
130+
131+ /// <summary>
132+ /// holds an list that contains all previous versions of the memento. it can store and retrieve stored mementos
133+ /// </summary>
134+ public class Caretaker
135+ {
136+ List < Memento > savedArticles = new List < Memento > ( ) ;
137+
138+ public void Add ( Memento m )
139+ {
140+ savedArticles . Add ( m ) ;
141+ }
142+
143+ public Memento Get ( int i )
144+ {
145+ return savedArticles [ i ] ;
146+ }
147+
148+ public int GetCountOfSavedArticles ( )
149+ {
150+ return savedArticles . Count ;
151+ }
152+ }
153+
154+
155+ }
0 commit comments