This repository was archived by the owner on Aug 10, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +62
-5
lines changed Expand file tree Collapse file tree 3 files changed +62
-5
lines changed Original file line number Diff line number Diff line change 1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Collections . ObjectModel ;
3
4
using GameEvents . Generic ;
4
5
using UnityEngine ;
@@ -39,7 +40,17 @@ public void RaiseGameEvent()
39
40
Debug . Log ( $ "Raise event: { name } , listener: { listener } ") ;
40
41
}
41
42
42
- listener . RaiseGameEvent ( ) ;
43
+ try
44
+ {
45
+ listener . RaiseGameEvent ( ) ;
46
+ }
47
+ catch ( Exception e )
48
+ {
49
+ if ( debug )
50
+ {
51
+ Debug . Log ( $ "Listener: { listener } of event: { name } has thrown an exception: { e . Message } ") ;
52
+ }
53
+ }
43
54
}
44
55
}
45
56
Original file line number Diff line number Diff line change 1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Collections . ObjectModel ;
3
4
using UnityEngine ;
4
5
@@ -38,7 +39,17 @@ public void RaiseGameEvent(TArgument argument)
38
39
Debug . Log ( $ "Raise event: { name } , listener: { listener } , argument: { argument } ") ;
39
40
}
40
41
41
- listener . RaiseGameEvent ( argument ) ;
42
+ try
43
+ {
44
+ listener . RaiseGameEvent ( argument ) ;
45
+ }
46
+ catch ( Exception e )
47
+ {
48
+ if ( debug )
49
+ {
50
+ Debug . Log ( $ "Listener: { listener } of event: { name } has thrown an exception: { e . Message } ") ;
51
+ }
52
+ }
42
53
}
43
54
}
44
55
Original file line number Diff line number Diff line change 1
- using System . Linq ;
1
+ using System ;
2
+ using System . Linq ;
2
3
using GameEvents . Bool ;
3
4
using GameEvents . Float ;
4
5
using GameEvents . Game ;
@@ -117,6 +118,40 @@ public void ShouldRaiseGameEventEvent()
117
118
Assert . AreEqual ( 0 , count [ 0 ] ) ;
118
119
}
119
120
121
+
122
+ [ Test ]
123
+ public void ShouldNotBreakChainWhenExceptionIsThrown ( )
124
+ {
125
+ // Given.
126
+ var gameObject = new UnityEngine . GameObject ( ) ;
127
+ gameObject . SetActive ( false ) ;
128
+
129
+ var listenerWithError = gameObject . AddComponent < GameEventListener > ( ) ;
130
+ var listener = gameObject . AddComponent < GameEventListener > ( ) ;
131
+
132
+ listenerWithError . OnGameEvent = new UnityEvent ( ) ;
133
+ listenerWithError . GameEvent = ScriptableObject . CreateInstance < GameEvent > ( ) ;
134
+
135
+ listener . OnGameEvent = new UnityEvent ( ) ;
136
+ listener . GameEvent = listenerWithError . GameEvent ;
137
+
138
+ var count = new int [ 1 ] ;
139
+ listenerWithError . OnGameEvent . AddListener ( ( ) => throw new NullReferenceException ( ) ) ;
140
+ listener . OnGameEvent . AddListener ( ( ) => count [ 0 ] ++ ) ;
141
+
142
+ // Then.
143
+ gameObject . SetActive ( true ) ;
144
+ listener . GameEvent . RaiseGameEvent ( ) ;
145
+
146
+ Assert . AreEqual ( 1 , count [ 0 ] ) ;
147
+ count [ 0 ] = 0 ;
148
+
149
+ gameObject . SetActive ( false ) ;
150
+ listener . GameEvent . RaiseGameEvent ( ) ;
151
+
152
+ Assert . AreEqual ( 0 , count [ 0 ] ) ;
153
+ }
154
+
120
155
[ Test ]
121
156
public void ShouldRegisterAndUnregisterGameObjectGameEventListener ( )
122
157
{
You can’t perform that action at this time.
0 commit comments