1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using UnityEngine ;
4
5
@@ -7,26 +8,39 @@ public class IsoSpriteSorting : MonoBehaviour
7
8
public bool isMovable ;
8
9
public bool renderBelowAll ;
9
10
11
+ [ NonSerialized ]
12
+ public bool registered = false ;
10
13
[ NonSerialized ]
11
14
public bool forceSort ;
12
15
13
16
[ NonSerialized ]
14
- public List < IsoSpriteSorting > staticDependencies = new List < IsoSpriteSorting > ( 16 ) ;
17
+ public readonly List < IsoSpriteSorting > staticDependencies = new List < IsoSpriteSorting > ( 16 ) ;
18
+ [ NonSerialized ]
19
+ public readonly List < IsoSpriteSorting > inverseStaticDependencies = new List < IsoSpriteSorting > ( 16 ) ;
15
20
[ NonSerialized ]
16
- public List < IsoSpriteSorting > inverseStaticDependencies = new List < IsoSpriteSorting > ( 16 ) ;
17
- public List < IsoSpriteSorting > movingDependencies = new List < IsoSpriteSorting > ( 8 ) ;
21
+ public readonly List < IsoSpriteSorting > movingDependencies = new List < IsoSpriteSorting > ( 8 ) ;
18
22
19
23
private readonly List < IsoSpriteSorting > visibleStaticDependencies = new List < IsoSpriteSorting > ( 16 ) ;
20
- private List < IsoSpriteSorting > activeDependencies = new List < IsoSpriteSorting > ( 16 ) ;
21
- public List < IsoSpriteSorting > ActiveDependencies
24
+
25
+ private int visibleStaticLastRefreshFrame = 0 ;
26
+ public List < IsoSpriteSorting > VisibleStaticDependencies
22
27
{
23
28
get
24
29
{
25
- activeDependencies . Clear ( ) ;
26
- IsoSpriteSortingManager . FilterListByVisibility ( staticDependencies , visibleStaticDependencies ) ;
27
- activeDependencies . AddRange ( visibleStaticDependencies ) ;
28
- activeDependencies . AddRange ( movingDependencies ) ;
29
- return activeDependencies ;
30
+ if ( visibleStaticLastRefreshFrame < Time . frameCount )
31
+ {
32
+ IsoSpriteSortingManager . FilterListByVisibility ( staticDependencies , visibleStaticDependencies ) ;
33
+ visibleStaticLastRefreshFrame = Time . frameCount ;
34
+ }
35
+ return visibleStaticDependencies ;
36
+ }
37
+ }
38
+
39
+ public List < IsoSpriteSorting > VisibleMovingDependencies
40
+ {
41
+ get
42
+ {
43
+ return movingDependencies ;
30
44
}
31
45
}
32
46
@@ -40,21 +54,67 @@ public enum SortType
40
54
41
55
public Vector3 SorterPositionOffset = new Vector3 ( ) ;
42
56
public Vector3 SorterPositionOffset2 = new Vector3 ( ) ;
57
+ public Renderer [ ] renderersToSort ;
43
58
44
59
private Transform t ;
45
60
61
+ public void SetupStaticCache ( )
62
+ {
63
+ RefreshBounds ( ) ;
64
+ RefreshPoint1 ( ) ;
65
+ RefreshPoint2 ( ) ;
66
+ }
67
+
68
+ private void RefreshBounds ( )
69
+ {
70
+ cachedBounds = new Bounds2D ( renderersToSort [ 0 ] . bounds ) ;
71
+ }
72
+
73
+ private void RefreshPoint1 ( )
74
+ {
75
+ cachedPoint1 = SorterPositionOffset + t . position ;
76
+ }
77
+
78
+ private void RefreshPoint2 ( )
79
+ {
80
+ cachedPoint2 = SorterPositionOffset2 + t . position ;
81
+ }
82
+
83
+ private int lastPoint1CalculatedFrame ;
84
+ private Vector2 cachedPoint1 ;
46
85
private Vector3 SortingPoint1
47
86
{
48
87
get
49
88
{
50
- return SorterPositionOffset + t . position ;
89
+ if ( isMovable )
90
+ {
91
+ int frameCount = Time . frameCount ;
92
+ if ( frameCount != lastPoint1CalculatedFrame )
93
+ {
94
+ lastPoint1CalculatedFrame = frameCount ;
95
+ RefreshPoint1 ( ) ;
96
+ }
97
+ }
98
+ return cachedPoint1 ;
51
99
}
52
100
}
101
+
102
+ private int lastPoint2CalculatedFrame ;
103
+ private Vector2 cachedPoint2 ;
53
104
private Vector3 SortingPoint2
54
105
{
55
106
get
56
107
{
57
- return SorterPositionOffset2 + t . position ;
108
+ if ( isMovable )
109
+ {
110
+ int frameCount = Time . frameCount ;
111
+ if ( frameCount != lastPoint2CalculatedFrame )
112
+ {
113
+ lastPoint2CalculatedFrame = frameCount ;
114
+ RefreshPoint2 ( ) ;
115
+ }
116
+ }
117
+ return cachedPoint2 ;
58
118
}
59
119
}
60
120
@@ -69,20 +129,6 @@ public Vector3 AsPoint
69
129
}
70
130
}
71
131
72
- public bool IsNear ( Vector3 point , float distance )
73
- {
74
- if ( sortType == SortType . Point )
75
- {
76
- return Vector2 . Distance ( point , SortingPoint1 ) <= distance ;
77
- }
78
- else
79
- {
80
- bool nearPoint1 = Vector2 . Distance ( point , SortingPoint1 ) <= distance ;
81
- bool nearPoint2 = Vector2 . Distance ( point , SortingPoint2 ) <= distance ;
82
- return nearPoint1 || nearPoint2 ;
83
- }
84
- }
85
-
86
132
private float SortingLineCenterHeight
87
133
{
88
134
get
@@ -99,8 +145,6 @@ private float SortingLineCenterHeight
99
145
}
100
146
}
101
147
102
- public Renderer [ ] renderersToSort ;
103
-
104
148
#if UNITY_EDITOR
105
149
public void SortScene ( )
106
150
{
@@ -118,27 +162,28 @@ public void SortScene()
118
162
}
119
163
#endif
120
164
121
- void Awake ( )
165
+ private void Awake ( )
166
+ {
167
+ t = transform ; //This needs to be here AND in the setup function
168
+ }
169
+
170
+ IEnumerator Start ( )
122
171
{
123
172
if ( Application . isPlaying )
124
173
{
125
174
IsoSpriteSortingManager temp = IsoSpriteSortingManager . Instance ; //bring the instance into existence so the Update function will run;
175
+ yield return null ;
126
176
Setup ( ) ;
127
177
}
128
178
}
129
179
130
180
private void Setup ( )
131
181
{
132
- t = transform ;
182
+ t = transform ; //This needs to be here AND in the Awake function
133
183
if ( renderersToSort == null || renderersToSort . Length == 0 )
134
184
{
135
- renderersToSort = new Renderer [ ] { GetComponent < Renderer > ( ) } ;
136
- }
137
- if ( ! isMovable )
138
- {
139
- cachedBounds = new Bounds2D ( renderersToSort [ 0 ] . bounds ) ;
185
+ renderersToSort = GetComponentsInChildren < Renderer > ( ) ;
140
186
}
141
- System . Array . Sort ( renderersToSort , ( a , b ) => a . sortingOrder . CompareTo ( b . sortingOrder ) ) ;
142
187
IsoSpriteSortingManager . RegisterSprite ( this ) ;
143
188
}
144
189
@@ -149,10 +194,12 @@ public static int CompairIsoSortersBasic(IsoSpriteSorting sprite1, IsoSpriteSort
149
194
return y2 . CompareTo ( y1 ) ;
150
195
}
151
196
197
+ //A result of -1 means sprite1 is above sprite2 in physical space
152
198
public static int CompareIsoSorters ( IsoSpriteSorting sprite1 , IsoSpriteSorting sprite2 )
153
199
{
154
200
if ( sprite1 . sortType == SortType . Point && sprite2 . sortType == SortType . Point )
155
201
{
202
+ //Debug.Log(sprite1.name + " - " + sprite1.SortingPoint1 + " sprite2: " + sprite2.name + " - " + sprite2.SortingPoint1);
156
203
return sprite2 . SortingPoint1 . y . CompareTo ( sprite1 . SortingPoint1 . y ) ;
157
204
}
158
205
else if ( sprite1 . sortType == SortType . Line && sprite2 . sortType == SortType . Line )
@@ -183,22 +230,22 @@ private static int CompareLineAndLine(IsoSpriteSorting line1, IsoSpriteSorting l
183
230
int comp1 = ComparePointAndLine ( line1Point1 , line2 ) ;
184
231
int comp2 = ComparePointAndLine ( line1Point2 , line2 ) ;
185
232
int oneVStwo = int . MinValue ;
186
- if ( comp1 == comp2 )
233
+ if ( comp1 == comp2 ) //Both points in line 1 are above or below line2
187
234
{
188
235
oneVStwo = comp1 ;
189
236
}
190
237
191
238
int comp3 = ComparePointAndLine ( line2Point1 , line1 ) ;
192
239
int comp4 = ComparePointAndLine ( line2Point2 , line1 ) ;
193
240
int twoVSone = int . MinValue ;
194
- if ( comp3 == comp4 )
241
+ if ( comp3 == comp4 ) //Both points in line 2 are above or below line1
195
242
{
196
243
twoVSone = - comp3 ;
197
244
}
198
245
199
246
if ( oneVStwo != int . MinValue && twoVSone != int . MinValue )
200
247
{
201
- if ( oneVStwo == twoVSone )
248
+ if ( oneVStwo == twoVSone ) //the two comparisons agree about the ordering
202
249
{
203
250
return oneVStwo ;
204
251
}
@@ -274,18 +321,21 @@ public int RendererSortingOrder
274
321
}
275
322
276
323
private Bounds2D cachedBounds ;
324
+ private int lastBoundsCalculatedFrame = 0 ;
277
325
public Bounds2D TheBounds
278
326
{
279
327
get
280
328
{
281
329
if ( isMovable )
282
330
{
283
- return new Bounds2D ( renderersToSort [ 0 ] . bounds ) ;
284
- }
285
- else
286
- {
287
- return cachedBounds ;
331
+ int frameCount = Time . frameCount ;
332
+ if ( frameCount != lastBoundsCalculatedFrame )
333
+ {
334
+ lastBoundsCalculatedFrame = frameCount ;
335
+ RefreshBounds ( ) ;
336
+ }
288
337
}
338
+ return cachedBounds ;
289
339
}
290
340
}
291
341
0 commit comments