@@ -14,8 +14,7 @@ public abstract class AbstractMemberBinding : MonoBehaviour, IMemberBinding
14
14
{
15
15
private bool _isInitCalled ;
16
16
17
- [ SerializeField ]
18
- private bool _isAutoConnection = true ;
17
+ [ SerializeField ] private bool _isAutoConnection = true ;
19
18
20
19
/// <summary>
21
20
/// Initialise this binding. Used when we first start the scene.
@@ -25,7 +24,7 @@ public virtual void Init()
25
24
{
26
25
_isInitCalled = true ;
27
26
28
- if ( ! gameObject . activeInHierarchy )
27
+ if ( ! gameObject . activeInHierarchy )
29
28
{
30
29
return ; //wait for enabling
31
30
}
@@ -40,47 +39,50 @@ public virtual void Init()
40
39
private object FindViewModel ( string viewModelName )
41
40
{
42
41
var trans = transform ;
43
- while ( trans != null )
42
+ while ( trans != null )
44
43
{
45
- var buffer = Buffer . Behaviours ;
46
- trans . GetComponents < MonoBehaviour > ( buffer ) ;
47
- var monoBehaviourViewModel = buffer
48
- . FirstOrDefault ( component => component . GetType ( ) . ToString ( ) == viewModelName ) ;
49
- if ( monoBehaviourViewModel != null )
44
+ using ( var cache = trans . gameObject . GetComponentsWithCache < MonoBehaviour > ( false ) )
50
45
{
51
- return monoBehaviourViewModel ;
52
- }
53
-
54
- var providedViewModel = buffer
55
- . Select ( component => component . GetViewModelData ( ) )
56
- . Where ( component => component != null )
57
- . FirstOrDefault (
58
- viewModelData => viewModelData . TypeName == viewModelName &&
46
+ var monoBehaviourViewModel = cache . Components
47
+ . FirstOrDefault ( component =>
48
+ component . GetType ( ) . ToString ( ) ==
49
+ viewModelName ) ;
50
+ if ( monoBehaviourViewModel != null )
51
+ {
52
+ return monoBehaviourViewModel ;
53
+ }
54
+
55
+ var providedViewModel = cache . Components
56
+ . Select ( component => component . GetViewModelData ( ) )
57
+ . Where ( component => component != null )
58
+ . FirstOrDefault (
59
+ viewModelData => viewModelData . TypeName == viewModelName &&
59
60
#pragma warning disable 252 , 253 // Warning says unintended reference comparison, but we do want to compare references
60
- ( object ) viewModelData . Model != this
61
+ ( object ) viewModelData . Model != this
61
62
#pragma warning restore 252 , 253
62
- ) ;
63
+ ) ;
63
64
64
- if ( providedViewModel != null )
65
- {
66
- return providedViewModel . Model ;
65
+ if ( providedViewModel != null )
66
+ {
67
+ return providedViewModel . Model ;
68
+ }
67
69
}
68
70
69
71
trans = trans . parent ;
70
72
}
71
73
72
- throw new ViewModelNotFoundException ( string . Format (
73
- "Tried to get view model {0 } but it could not be found on "
74
- + "object {1 }. Check that a ViewModelBinding for that view model exists further up in "
75
- + "the scene hierarchy. " , viewModelName , gameObject . name )
74
+ throw new ViewModelNotFoundException (
75
+ $ "Tried to get view model { viewModelName } but it could not be found on " +
76
+ $ "object { gameObject . name } . Check that a ViewModelBinding for that view model exists further up in " +
77
+ "the scene hierarchy. "
76
78
) ;
77
79
}
78
80
79
81
/// <summary>
80
82
/// Make a property end point for a property on the view model.
81
83
/// </summary>
82
84
protected PropertyEndPoint MakeViewModelEndPoint ( string viewModelPropertyName , string adapterId ,
83
- AdapterOptions adapterOptions )
85
+ AdapterOptions adapterOptions )
84
86
{
85
87
string propertyName ;
86
88
object viewModel ;
@@ -94,10 +96,10 @@ protected PropertyEndPoint MakeViewModelEndPoint(string viewModelPropertyName, s
94
96
/// Parse an end-point reference including a type name and member name separated by a period.
95
97
/// </summary>
96
98
protected static void ParseEndPointReference ( string endPointReference , out string memberName ,
97
- out string typeName )
99
+ out string typeName )
98
100
{
99
101
var lastPeriodIndex = endPointReference . LastIndexOf ( '.' ) ;
100
- if ( lastPeriodIndex == - 1 )
102
+ if ( lastPeriodIndex == - 1 )
101
103
{
102
104
throw new InvalidEndPointException (
103
105
"No period was found, expected end-point reference in the following format: <type-name>.<member-name>. " +
@@ -109,12 +111,12 @@ protected static void ParseEndPointReference(string endPointReference, out strin
109
111
memberName = endPointReference . Substring ( lastPeriodIndex + 1 ) ;
110
112
//Due to (undocumented) unity behaviour, some of their components do not work with the namespace when using GetComponent(""), and all of them work without the namespace
111
113
//So to be safe, we remove all namespaces from any component that starts with UnityEngine
112
- if ( typeName . StartsWith ( "UnityEngine." ) )
114
+ if ( typeName . StartsWith ( "UnityEngine." ) )
113
115
{
114
116
typeName = typeName . Substring ( typeName . LastIndexOf ( '.' ) + 1 ) ;
115
117
}
116
118
117
- if ( typeName . Length == 0 || memberName . Length == 0 )
119
+ if ( typeName . Length == 0 || memberName . Length == 0 )
118
120
{
119
121
throw new InvalidEndPointException (
120
122
"Bad format for end-point reference, expected the following format: <type-name>.<member-name>. " +
@@ -127,13 +129,13 @@ protected static void ParseEndPointReference(string endPointReference, out strin
127
129
/// Parse an end-point reference and search up the hierarchy for the named view-model.
128
130
/// </summary>
129
131
protected void ParseViewModelEndPointReference ( string endPointReference , out string memberName ,
130
- out object viewModel )
132
+ out object viewModel )
131
133
{
132
134
string viewModelName ;
133
135
ParseEndPointReference ( endPointReference , out memberName , out viewModelName ) ;
134
136
135
137
viewModel = FindViewModel ( viewModelName ) ;
136
- if ( viewModel == null )
138
+ if ( viewModel == null )
137
139
{
138
140
throw new ViewModelNotFoundException ( "Failed to find view-model in hierarchy: " + viewModelName ) ;
139
141
}
@@ -148,7 +150,7 @@ protected void ParseViewEndPointReference(string endPointReference, out string m
148
150
ParseEndPointReference ( endPointReference , out memberName , out boundComponentType ) ;
149
151
150
152
view = GetComponent ( boundComponentType ) ;
151
- if ( view == null )
153
+ if ( view == null )
152
154
{
153
155
throw new ComponentNotFoundException ( "Failed to find component on current game object: " +
154
156
boundComponentType ) ;
@@ -171,7 +173,7 @@ protected void ParseViewEndPointReference(string endPointReference, out string m
171
173
/// </summary>
172
174
protected void OnEnable ( )
173
175
{
174
- if ( ! _isAutoConnection && ! _isInitCalled )
176
+ if ( ! _isAutoConnection && ! _isInitCalled )
175
177
{
176
178
return ;
177
179
}
0 commit comments