com.puzzlescapegames.services 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +5 -1
  2. package/Runtime/Common/AudioMixer.mixer +146 -0
  3. package/Runtime/Common/AudioMixer.mixer.meta +8 -0
  4. package/Runtime/Common.meta +8 -0
  5. package/Runtime/Plugins/SlicedFilledImage.cs +730 -0
  6. package/Runtime/Plugins/SlicedFilledImage.cs.meta +11 -0
  7. package/Runtime/Plugins/UIGradient/Editor/UIGradientEditor.cs +218 -0
  8. package/Runtime/Plugins/UIGradient/Editor/UIGradientEditor.cs.meta +11 -0
  9. package/Runtime/Plugins/UIGradient/Editor/uigradient_inspector_icon.png +0 -0
  10. package/Runtime/Plugins/UIGradient/Editor/uigradient_inspector_icon.png.meta +110 -0
  11. package/Runtime/Plugins/UIGradient/Editor.meta +8 -0
  12. package/Runtime/Plugins/UIGradient/Scripts/UIGradient.cs +511 -0
  13. package/Runtime/Plugins/UIGradient/Scripts/UIGradient.cs.meta +11 -0
  14. package/Runtime/Plugins/UIGradient/Scripts/Vector2Extension.cs +24 -0
  15. package/Runtime/Plugins/UIGradient/Scripts/Vector2Extension.cs.meta +11 -0
  16. package/Runtime/Plugins/UIGradient/Scripts.meta +8 -0
  17. package/Runtime/Plugins/UIGradient.meta +8 -0
  18. package/Runtime/Plugins/Vibration/Vibration/Example/VibrationExample.cs +82 -0
  19. package/Runtime/Plugins/Vibration/Vibration/Example/VibrationExample.cs.meta +11 -0
  20. package/Runtime/Plugins/Vibration/Vibration/Example.meta +8 -0
  21. package/Runtime/Plugins/Vibration/Vibration/Vibration.cs +263 -0
  22. package/Runtime/Plugins/Vibration/Vibration/Vibration.cs.meta +12 -0
  23. package/Runtime/Plugins/Vibration/Vibration.asmdef +18 -0
  24. package/Runtime/Plugins/Vibration/Vibration.asmdef.meta +7 -0
  25. package/Runtime/Plugins/Vibration/Vibration.meta +8 -0
  26. package/Runtime/Plugins/Vibration/iOS/HapticFeedback.mm +64 -0
  27. package/Runtime/Plugins/Vibration/iOS/HapticFeedback.mm.meta +37 -0
  28. package/Runtime/Plugins/Vibration/iOS/Vibration.h +26 -0
  29. package/Runtime/Plugins/Vibration/iOS/Vibration.h.meta +27 -0
  30. package/Runtime/Plugins/Vibration/iOS/Vibration.mm +73 -0
  31. package/Runtime/Plugins/Vibration/iOS/Vibration.mm.meta +37 -0
  32. package/Runtime/Plugins/Vibration/iOS.meta +8 -0
  33. package/Runtime/Plugins/Vibration.meta +8 -0
  34. package/Runtime/Plugins.meta +8 -0
  35. package/Runtime/PuzzlescapeGames.Services.asmdef +19 -2
  36. package/Runtime/Services/AudioService/AudioService.cs +82 -0
  37. package/Runtime/Services/AudioService/AudioService.cs.meta +3 -0
  38. package/Runtime/Services/AudioService/AudioServiceCustomInstaller.cs +29 -0
  39. package/Runtime/Services/AudioService/AudioServiceCustomInstaller.cs.meta +3 -0
  40. package/Runtime/Services/AudioService/AudioSource.cs +87 -0
  41. package/Runtime/Services/AudioService/AudioSource.cs.meta +3 -0
  42. package/Runtime/Services/AudioService/AudioSourceFactory.cs +6 -0
  43. package/Runtime/Services/AudioService/AudioSourceFactory.cs.meta +3 -0
  44. package/Runtime/Services/AudioService/IAudioService.cs +16 -0
  45. package/Runtime/Services/AudioService/IAudioService.cs.meta +3 -0
  46. package/Runtime/Services/AudioService.meta +8 -0
  47. package/Runtime/Services/NTPService/INTPService.cs +13 -0
  48. package/Runtime/Services/NTPService/INTPService.cs.meta +3 -0
  49. package/Runtime/Services/NTPService/NTPService.cs +153 -0
  50. package/Runtime/Services/NTPService/NTPService.cs.meta +3 -0
  51. package/Runtime/Services/NTPService/NTPServiceInstaller.cs +12 -0
  52. package/Runtime/Services/NTPService/NTPServiceInstaller.cs.meta +3 -0
  53. package/Runtime/Services/NTPService.meta +8 -0
  54. package/Runtime/Services/TickableService/TickableService.cs +29 -0
  55. package/Runtime/Services/TickableService/TickableService.cs.meta +3 -0
  56. package/Runtime/Services/TickableService/TickableServiceInstaller.cs +12 -0
  57. package/Runtime/Services/TickableService/TickableServiceInstaller.cs.meta +3 -0
  58. package/Runtime/Services/TickableService.meta +8 -0
  59. package/Runtime/Services/UIBlockingService/UIBlocker.cs +15 -0
  60. package/Runtime/Services/UIBlockingService/UIBlocker.cs.meta +3 -0
  61. package/Runtime/Services/UIBlockingService/UIBlockingService.cs +36 -0
  62. package/Runtime/Services/UIBlockingService/UIBlockingService.cs.meta +3 -0
  63. package/Runtime/Services/UIBlockingService/UIBlockingServiceInstaller.cs +17 -0
  64. package/Runtime/Services/UIBlockingService/UIBlockingServiceInstaller.cs.meta +3 -0
  65. package/Runtime/Services/UIBlockingService.meta +8 -0
  66. package/Runtime/Services/VibrationService/IVibrationService.cs +7 -0
  67. package/Runtime/Services/VibrationService/IVibrationService.cs.meta +3 -0
  68. package/Runtime/Services/VibrationService/VibrationService.cs +19 -0
  69. package/Runtime/Services/VibrationService/VibrationService.cs.meta +3 -0
  70. package/Runtime/Services/VibrationService/VibrationServiceInstaller.cs +12 -0
  71. package/Runtime/Services/VibrationService/VibrationServiceInstaller.cs.meta +3 -0
  72. package/Runtime/Services/VibrationService.meta +8 -0
  73. package/Runtime/Services.meta +8 -0
  74. package/package.json +1 -1
@@ -0,0 +1,730 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using UnityEngine;
4
+ using UnityEngine.Sprites;
5
+ using UnityEngine.UI;
6
+ #if UNITY_2017_4 || UNITY_2018_2_OR_NEWER
7
+ using UnityEngine.U2D;
8
+ #endif
9
+
10
+ #if UNITY_EDITOR
11
+ using UnityEditor;
12
+
13
+ // Custom Editor to order the variables in the Inspector similar to Image component
14
+ [ CustomEditor( typeof(SlicedFilledImage) ) ]
15
+ [ CanEditMultipleObjects ]
16
+ public sealed class SlicedFilledImageEditor : Editor
17
+ {
18
+ private SerializedProperty spriteProp, colorProp;
19
+ private GUIContent spriteLabel;
20
+
21
+ public override void OnInspectorGUI()
22
+ {
23
+ serializedObject.Update();
24
+
25
+ EditorGUILayout.PropertyField( spriteProp, spriteLabel );
26
+ EditorGUILayout.PropertyField( colorProp );
27
+ DrawPropertiesExcluding( serializedObject, "m_Script", "m_Sprite", "m_Color", "m_OnCullStateChanged" );
28
+
29
+ serializedObject.ApplyModifiedProperties();
30
+ }
31
+
32
+ private void OnEnable()
33
+ {
34
+ spriteProp = serializedObject.FindProperty( "m_Sprite" );
35
+ colorProp = serializedObject.FindProperty( "m_Color" );
36
+ spriteLabel = new GUIContent( "Source Image" );
37
+ }
38
+ }
39
+ #endif
40
+
41
+ // Credit: https://bitbucket.org/Unity-Technologies/ui/src/2018.4/UnityEngine.UI/UI/Core/Image.cs
42
+ [ RequireComponent( typeof(CanvasRenderer) ) ]
43
+ [ AddComponentMenu( "UI/Sliced Filled Image", 11 ) ]
44
+ public sealed class SlicedFilledImage : MaskableGraphic, ISerializationCallbackReceiver, ILayoutElement, ICanvasRaycastFilter
45
+ {
46
+ private static readonly Vector3[] s_Vertices = new Vector3[4];
47
+ private static readonly Vector2[] s_UVs = new Vector2[4];
48
+ private static readonly Vector2[] s_SlicedVertices = new Vector2[4];
49
+ private static readonly Vector2[] s_SlicedUVs = new Vector2[4];
50
+
51
+ // Whether this is being tracked for Atlas Binding
52
+ private bool m_Tracked;
53
+
54
+ int ILayoutElement.layoutPriority => 0;
55
+ float ILayoutElement.minWidth => 0;
56
+ float ILayoutElement.minHeight => 0;
57
+ float ILayoutElement.flexibleWidth => -1;
58
+ float ILayoutElement.flexibleHeight => -1;
59
+
60
+ float ILayoutElement.preferredWidth
61
+ {
62
+ get
63
+ {
64
+ if ( activeSprite == null )
65
+ return 0;
66
+
67
+ return DataUtility.GetMinSize( activeSprite ).x / pixelsPerUnit;
68
+ }
69
+ }
70
+
71
+ float ILayoutElement.preferredHeight
72
+ {
73
+ get
74
+ {
75
+ if ( activeSprite == null )
76
+ return 0;
77
+
78
+ return DataUtility.GetMinSize( activeSprite ).y / pixelsPerUnit;
79
+ }
80
+ }
81
+
82
+ protected SlicedFilledImage() => useLegacyMeshGeneration = false;
83
+
84
+ protected override void OnEnable()
85
+ {
86
+ base.OnEnable();
87
+ TrackImage();
88
+ }
89
+
90
+ protected override void OnDisable()
91
+ {
92
+ base.OnDisable();
93
+
94
+ if ( m_Tracked )
95
+ UnTrackImage();
96
+ }
97
+
98
+ #if UNITY_EDITOR
99
+ protected override void OnValidate()
100
+ {
101
+ base.OnValidate();
102
+ m_PixelsPerUnitMultiplier = Mathf.Max( 0.01f, m_PixelsPerUnitMultiplier );
103
+ }
104
+ #endif
105
+
106
+ protected override void OnPopulateMesh( VertexHelper vh )
107
+ {
108
+ if ( activeSprite == null )
109
+ {
110
+ base.OnPopulateMesh( vh );
111
+ return;
112
+ }
113
+
114
+ GenerateSlicedFilledSprite( vh );
115
+ }
116
+
117
+ /// <summary>
118
+ /// Update the renderer's material.
119
+ /// </summary>
120
+ protected override void UpdateMaterial()
121
+ {
122
+ base.UpdateMaterial();
123
+
124
+ // Check if this sprite has an associated alpha texture (generated when splitting RGBA = RGB + A as two textures without alpha)
125
+ if ( activeSprite == null )
126
+ {
127
+ canvasRenderer.SetAlphaTexture( null );
128
+ return;
129
+ }
130
+
131
+ Texture2D alphaTex = activeSprite.associatedAlphaSplitTexture;
132
+ if ( alphaTex != null )
133
+ canvasRenderer.SetAlphaTexture( alphaTex );
134
+ }
135
+
136
+ private void GenerateSlicedFilledSprite( VertexHelper vh )
137
+ {
138
+ vh.Clear();
139
+
140
+ if ( m_FillAmount < 0.001f )
141
+ return;
142
+
143
+ Rect rect = GetPixelAdjustedRect();
144
+ Vector4 outer = DataUtility.GetOuterUV( activeSprite );
145
+ Vector4 padding = DataUtility.GetPadding( activeSprite );
146
+
147
+ if ( !hasBorder )
148
+ {
149
+ Vector2 size = activeSprite.rect.size;
150
+
151
+ int spriteW = Mathf.RoundToInt( size.x );
152
+ int spriteH = Mathf.RoundToInt( size.y );
153
+
154
+ // Image's dimensions used for drawing. X = left, Y = bottom, Z = right, W = top.
155
+ var vertices = new Vector4( rect.x + rect.width * ( padding.x / spriteW ),
156
+ rect.y + rect.height * ( padding.y / spriteH ),
157
+ rect.x + rect.width * ( ( spriteW - padding.z ) / spriteW ),
158
+ rect.y + rect.height * ( ( spriteH - padding.w ) / spriteH ) );
159
+
160
+ GenerateFilledSprite( vh, vertices, outer, m_FillAmount );
161
+ return;
162
+ }
163
+
164
+ Vector4 inner = DataUtility.GetInnerUV( activeSprite );
165
+ Vector4 border = GetAdjustedBorders( activeSprite.border / pixelsPerUnit, rect );
166
+
167
+ padding = padding / pixelsPerUnit;
168
+
169
+ s_SlicedVertices[ 0 ] = new Vector2( padding.x, padding.y );
170
+ s_SlicedVertices[ 3 ] = new Vector2( rect.width - padding.z, rect.height - padding.w );
171
+
172
+ s_SlicedVertices[ 1 ].x = border.x;
173
+ s_SlicedVertices[ 1 ].y = border.y;
174
+
175
+ s_SlicedVertices[ 2 ].x = rect.width - border.z;
176
+ s_SlicedVertices[ 2 ].y = rect.height - border.w;
177
+
178
+ if ( m_PreserveBorder )
179
+ switch ( m_FillDirection )
180
+ {
181
+ case FillDirection.Right:
182
+ {
183
+ s_SlicedVertices[ 0 ].x = padding.x;
184
+ s_SlicedVertices[ 3 ].x = rect.width * m_FillAmount - padding.z;
185
+
186
+ float width = s_SlicedVertices[ 3 ].x - s_SlicedVertices[ 0 ].x;
187
+ if ( width < border.x + border.z )
188
+ {
189
+ s_SlicedVertices[ 1 ].x = border.x;
190
+ s_SlicedVertices[ 2 ].x = border.x;
191
+ }
192
+ else
193
+ {
194
+ s_SlicedVertices[ 1 ].x = border.x;
195
+ s_SlicedVertices[ 2 ].x = rect.width * m_FillAmount - border.z;
196
+ }
197
+ }
198
+ break;
199
+ case FillDirection.Left:
200
+ {
201
+ s_SlicedVertices[ 0 ].x = rect.width * ( 1f - m_FillAmount ) + padding.x;
202
+ s_SlicedVertices[ 3 ].x = rect.width - padding.z;
203
+
204
+ float width = s_SlicedVertices[ 3 ].x - s_SlicedVertices[ 0 ].x;
205
+ if ( width < border.x + border.z )
206
+ {
207
+ s_SlicedVertices[ 1 ].x = rect.width - border.z;
208
+ s_SlicedVertices[ 2 ].x = rect.width - border.z;
209
+ }
210
+ else
211
+ {
212
+ s_SlicedVertices[ 1 ].x = rect.width * ( 1f - m_FillAmount ) + border.x;
213
+ s_SlicedVertices[ 2 ].x = rect.width - border.z;
214
+ }
215
+ }
216
+ break;
217
+ case FillDirection.Up:
218
+ {
219
+ s_SlicedVertices[ 0 ].y = padding.y;
220
+ s_SlicedVertices[ 3 ].y = rect.height * m_FillAmount - padding.w;
221
+
222
+ float height = s_SlicedVertices[ 3 ].y - s_SlicedVertices[ 0 ].y;
223
+ if ( height < border.y + border.w )
224
+ {
225
+ s_SlicedVertices[ 1 ].y = border.y;
226
+ s_SlicedVertices[ 2 ].y = border.y;
227
+ }
228
+ else
229
+ {
230
+ s_SlicedVertices[ 1 ].y = border.y;
231
+ s_SlicedVertices[ 2 ].y = rect.height * m_FillAmount - border.w;
232
+ }
233
+ }
234
+ break;
235
+ case FillDirection.Down:
236
+ {
237
+ s_SlicedVertices[ 0 ].y = rect.height * ( 1f - m_FillAmount ) + padding.y;
238
+ s_SlicedVertices[ 3 ].y = rect.height - padding.w;
239
+
240
+ float height = s_SlicedVertices[ 3 ].y - s_SlicedVertices[ 0 ].y;
241
+ if ( height < border.y + border.w )
242
+ {
243
+ s_SlicedVertices[ 1 ].y = rect.height - border.w;
244
+ s_SlicedVertices[ 2 ].y = rect.height - border.w;
245
+ }
246
+ else
247
+ {
248
+ s_SlicedVertices[ 1 ].y = rect.height * ( 1f - m_FillAmount ) + border.y;
249
+ s_SlicedVertices[ 2 ].y = rect.height - border.w;
250
+ }
251
+ }
252
+ break;
253
+ }
254
+
255
+ for ( var i = 0; i < 4; ++i )
256
+ {
257
+ s_SlicedVertices[ i ].x += rect.x;
258
+ s_SlicedVertices[ i ].y += rect.y;
259
+ }
260
+
261
+ s_SlicedUVs[ 0 ] = new Vector2( outer.x, outer.y );
262
+ s_SlicedUVs[ 1 ] = new Vector2( inner.x, inner.y );
263
+ s_SlicedUVs[ 2 ] = new Vector2( inner.z, inner.w );
264
+ s_SlicedUVs[ 3 ] = new Vector2( outer.z, outer.w );
265
+
266
+ float rectStartPos;
267
+ float _1OverTotalSize;
268
+ if ( m_FillDirection == FillDirection.Left || m_FillDirection == FillDirection.Right )
269
+ {
270
+ rectStartPos = rect.x + padding.x;
271
+
272
+ float totalSize = rect.width - padding.x - padding.z;
273
+ _1OverTotalSize = totalSize > 0f ? 1f / totalSize : 1f;
274
+ }
275
+ else
276
+ {
277
+ rectStartPos = rect.y + padding.y;
278
+
279
+ float totalSize = rect.height - padding.y - padding.w;
280
+ _1OverTotalSize = totalSize > 0f ? 1f / totalSize : 1f;
281
+ }
282
+
283
+ for ( var x = 0; x < 3; x++ )
284
+ {
285
+ int x2 = x + 1;
286
+
287
+ for ( var y = 0; y < 3; y++ )
288
+ {
289
+ if ( !m_FillCenter && x == 1 && y == 1 )
290
+ continue;
291
+
292
+ int y2 = y + 1;
293
+
294
+ float sliceStart, sliceEnd;
295
+ switch ( m_FillDirection )
296
+ {
297
+ case FillDirection.Right:
298
+ sliceStart = ( s_SlicedVertices[ x ].x - rectStartPos ) * _1OverTotalSize;
299
+ sliceEnd = ( s_SlicedVertices[ x2 ].x - rectStartPos ) * _1OverTotalSize;
300
+ break;
301
+ case FillDirection.Up:
302
+ sliceStart = ( s_SlicedVertices[ y ].y - rectStartPos ) * _1OverTotalSize;
303
+ sliceEnd = ( s_SlicedVertices[ y2 ].y - rectStartPos ) * _1OverTotalSize;
304
+ break;
305
+ case FillDirection.Left:
306
+ sliceStart = 1f - ( s_SlicedVertices[ x2 ].x - rectStartPos ) * _1OverTotalSize;
307
+ sliceEnd = 1f - ( s_SlicedVertices[ x ].x - rectStartPos ) * _1OverTotalSize;
308
+ break;
309
+ case FillDirection.Down:
310
+ sliceStart = 1f - ( s_SlicedVertices[ y2 ].y - rectStartPos ) * _1OverTotalSize;
311
+ sliceEnd = 1f - ( s_SlicedVertices[ y ].y - rectStartPos ) * _1OverTotalSize;
312
+ break;
313
+ default: // Just there to get rid of the "Use of unassigned local variable" compiler error
314
+ sliceStart = sliceEnd = 0f;
315
+ break;
316
+ }
317
+
318
+ if ( sliceStart >= m_FillAmount )
319
+ continue;
320
+
321
+ var vertices = new Vector4( s_SlicedVertices[ x ].x, s_SlicedVertices[ y ].y, s_SlicedVertices[ x2 ].x,
322
+ s_SlicedVertices[ y2 ].y );
323
+ var uvs = new Vector4( s_SlicedUVs[ x ].x, s_SlicedUVs[ y ].y, s_SlicedUVs[ x2 ].x,
324
+ s_SlicedUVs[ y2 ].y );
325
+ float fillAmount = ( m_FillAmount - sliceStart ) / ( sliceEnd - sliceStart );
326
+
327
+ GenerateFilledSprite( vh, vertices, uvs, fillAmount );
328
+ }
329
+ }
330
+ }
331
+
332
+ private Vector4 GetAdjustedBorders( Vector4 border, Rect adjustedRect )
333
+ {
334
+ Rect originalRect = rectTransform.rect;
335
+
336
+ for ( var axis = 0; axis <= 1; axis++ )
337
+ {
338
+ float borderScaleRatio;
339
+
340
+ // The adjusted rect (adjusted for pixel correctness) may be slightly larger than the original rect.
341
+ // Adjust the border to match the adjustedRect to avoid small gaps between borders (case 833201).
342
+ if ( originalRect.size[ axis ] != 0 )
343
+ {
344
+ borderScaleRatio = adjustedRect.size[ axis ] / originalRect.size[ axis ];
345
+ border[ axis ] *= borderScaleRatio;
346
+ border[ axis + 2 ] *= borderScaleRatio;
347
+ }
348
+
349
+ // If the rect is smaller than the combined borders, then there's not room for the borders at their normal size.
350
+ // In order to avoid artefacts with overlapping borders, we scale the borders down to fit.
351
+ float combinedBorders = border[ axis ] + border[ axis + 2 ];
352
+ if ( adjustedRect.size[ axis ] < combinedBorders && combinedBorders != 0 )
353
+ {
354
+ borderScaleRatio = adjustedRect.size[ axis ] / combinedBorders;
355
+ border[ axis ] *= borderScaleRatio;
356
+ border[ axis + 2 ] *= borderScaleRatio;
357
+ }
358
+ }
359
+
360
+ return border;
361
+ }
362
+
363
+ private void GenerateFilledSprite( VertexHelper vh, Vector4 vertices, Vector4 uvs, float fillAmount )
364
+ {
365
+ if ( m_FillAmount < 0.001f )
366
+ return;
367
+
368
+ float uvLeft = uvs.x;
369
+ float uvBottom = uvs.y;
370
+ float uvRight = uvs.z;
371
+ float uvTop = uvs.w;
372
+
373
+ if ( fillAmount < 1f )
374
+ {
375
+ if ( m_FillDirection == FillDirection.Left || m_FillDirection == FillDirection.Right )
376
+ {
377
+ if ( m_FillDirection == FillDirection.Left )
378
+ {
379
+ vertices.x = vertices.z - ( vertices.z - vertices.x ) * fillAmount;
380
+ uvLeft = uvRight - ( uvRight - uvLeft ) * fillAmount;
381
+ }
382
+ else
383
+ {
384
+ vertices.z = vertices.x + ( vertices.z - vertices.x ) * fillAmount;
385
+ uvRight = uvLeft + ( uvRight - uvLeft ) * fillAmount;
386
+ }
387
+ }
388
+ else
389
+ {
390
+ if ( m_FillDirection == FillDirection.Down )
391
+ {
392
+ vertices.y = vertices.w - ( vertices.w - vertices.y ) * fillAmount;
393
+ uvBottom = uvTop - ( uvTop - uvBottom ) * fillAmount;
394
+ }
395
+ else
396
+ {
397
+ vertices.w = vertices.y + ( vertices.w - vertices.y ) * fillAmount;
398
+ uvTop = uvBottom + ( uvTop - uvBottom ) * fillAmount;
399
+ }
400
+ }
401
+ }
402
+
403
+ s_Vertices[ 0 ] = new Vector3( vertices.x, vertices.y );
404
+ s_Vertices[ 1 ] = new Vector3( vertices.x, vertices.w );
405
+ s_Vertices[ 2 ] = new Vector3( vertices.z, vertices.w );
406
+ s_Vertices[ 3 ] = new Vector3( vertices.z, vertices.y );
407
+
408
+ s_UVs[ 0 ] = new Vector2( uvLeft, uvBottom );
409
+ s_UVs[ 1 ] = new Vector2( uvLeft, uvTop );
410
+ s_UVs[ 2 ] = new Vector2( uvRight, uvTop );
411
+ s_UVs[ 3 ] = new Vector2( uvRight, uvBottom );
412
+
413
+ int startIndex = vh.currentVertCount;
414
+
415
+ for ( var i = 0; i < 4; i++ )
416
+ vh.AddVert( s_Vertices[ i ], color, s_UVs[ i ] );
417
+
418
+ vh.AddTriangle( startIndex, startIndex + 1, startIndex + 2 );
419
+ vh.AddTriangle( startIndex + 2, startIndex + 3, startIndex );
420
+ }
421
+
422
+ void ILayoutElement.CalculateLayoutInputHorizontal() {}
423
+ void ILayoutElement.CalculateLayoutInputVertical() {}
424
+
425
+ bool ICanvasRaycastFilter.IsRaycastLocationValid( Vector2 screenPoint, Camera eventCamera )
426
+ {
427
+ if ( alphaHitTestMinimumThreshold <= 0 )
428
+ return true;
429
+
430
+ if ( alphaHitTestMinimumThreshold > 1 )
431
+ return false;
432
+
433
+ if ( activeSprite == null )
434
+ return true;
435
+
436
+ Vector2 local;
437
+ if ( !RectTransformUtility.ScreenPointToLocalPointInRectangle( rectTransform, screenPoint, eventCamera,
438
+ out local ) )
439
+ return false;
440
+
441
+ Rect rect = GetPixelAdjustedRect();
442
+
443
+ // Convert to have lower left corner as reference point.
444
+ local.x += rectTransform.pivot.x * rect.width;
445
+ local.y += rectTransform.pivot.y * rect.height;
446
+
447
+ Rect spriteRect = activeSprite.rect;
448
+ Vector4 border = activeSprite.border;
449
+ Vector4 adjustedBorder = GetAdjustedBorders( border / pixelsPerUnit, rect );
450
+
451
+ for ( var i = 0; i < 2; i++ )
452
+ {
453
+ if ( local[ i ] <= adjustedBorder[ i ] )
454
+ continue;
455
+
456
+ if ( rect.size[ i ] - local[ i ] <= adjustedBorder[ i + 2 ] )
457
+ {
458
+ local[ i ] -= rect.size[ i ] - spriteRect.size[ i ];
459
+ continue;
460
+ }
461
+
462
+ float lerp = Mathf.InverseLerp( adjustedBorder[ i ], rect.size[ i ] - adjustedBorder[ i + 2 ], local[ i ] );
463
+ local[ i ] = Mathf.Lerp( border[ i ], spriteRect.size[ i ] - border[ i + 2 ], lerp );
464
+ }
465
+
466
+ // Normalize local coordinates.
467
+ Rect textureRect = activeSprite.textureRect;
468
+ var normalized = new Vector2( local.x / textureRect.width, local.y / textureRect.height );
469
+
470
+ // Convert to texture space.
471
+ float x = Mathf.Lerp( textureRect.x, textureRect.xMax, normalized.x ) / activeSprite.texture.width;
472
+ float y = Mathf.Lerp( textureRect.y, textureRect.yMax, normalized.y ) / activeSprite.texture.height;
473
+
474
+ switch ( m_FillDirection )
475
+ {
476
+ case FillDirection.Right:
477
+ if ( x > m_FillAmount )
478
+ return false;
479
+ break;
480
+ case FillDirection.Left:
481
+ if ( 1f - x > m_FillAmount )
482
+ return false;
483
+ break;
484
+ case FillDirection.Up:
485
+ if ( y > m_FillAmount )
486
+ return false;
487
+ break;
488
+ case FillDirection.Down:
489
+ if ( 1f - y > m_FillAmount )
490
+ return false;
491
+ break;
492
+ }
493
+
494
+ try
495
+ {
496
+ return activeSprite.texture.GetPixelBilinear( x, y ).a >= alphaHitTestMinimumThreshold;
497
+ }
498
+ catch ( UnityException e )
499
+ {
500
+ Debug.LogError(
501
+ "Using alphaHitTestMinimumThreshold greater than 0 on Image whose sprite texture cannot be read. " +
502
+ e.Message + " Also make sure to disable sprite packing for this sprite.", this );
503
+ return true;
504
+ }
505
+ }
506
+
507
+ void ISerializationCallbackReceiver.OnBeforeSerialize() {}
508
+
509
+ void ISerializationCallbackReceiver.OnAfterDeserialize() => m_FillAmount = Mathf.Clamp01( m_FillAmount );
510
+
511
+ private void TrackImage()
512
+ {
513
+ if ( activeSprite != null && activeSprite.texture == null )
514
+ {
515
+ #if UNITY_2017_4 || UNITY_2018_2_OR_NEWER
516
+ if ( !s_Initialized )
517
+ {
518
+ SpriteAtlasManager.atlasRegistered += RebuildImage;
519
+ s_Initialized = true;
520
+ }
521
+
522
+ m_TrackedTexturelessImages.Add( this );
523
+ #endif
524
+ m_Tracked = true;
525
+ }
526
+ }
527
+
528
+ private void UnTrackImage()
529
+ {
530
+ #if UNITY_2017_4 || UNITY_2018_2_OR_NEWER
531
+ m_TrackedTexturelessImages.Remove( this );
532
+ #endif
533
+ m_Tracked = false;
534
+ }
535
+
536
+ #if UNITY_2017_4 || UNITY_2018_2_OR_NEWER
537
+ private static void RebuildImage( SpriteAtlas spriteAtlas )
538
+ {
539
+ for ( int i = m_TrackedTexturelessImages.Count - 1; i >= 0; i-- )
540
+ {
541
+ SlicedFilledImage image = m_TrackedTexturelessImages[ i ];
542
+ if ( spriteAtlas.CanBindTo( image.activeSprite ) )
543
+ {
544
+ image.SetAllDirty();
545
+ m_TrackedTexturelessImages.RemoveAt( i );
546
+ }
547
+ }
548
+ }
549
+ #endif
550
+ private static class SetPropertyUtility
551
+ {
552
+ public static bool SetStruct< T >( ref T currentValue, T newValue )
553
+ where T : struct
554
+ {
555
+ if ( EqualityComparer< T >.Default.Equals( currentValue, newValue ) )
556
+ return false;
557
+
558
+ currentValue = newValue;
559
+ return true;
560
+ }
561
+
562
+ public static bool SetClass< T >( ref T currentValue, T newValue )
563
+ where T : class
564
+ {
565
+ if ( ( currentValue == null && newValue == null ) ||
566
+ ( currentValue != null && currentValue.Equals( newValue ) ) )
567
+ return false;
568
+
569
+ currentValue = newValue;
570
+ return true;
571
+ }
572
+ }
573
+
574
+ public enum FillDirection { Right = 0, Left = 1, Up = 2, Down = 3 }
575
+
576
+ #pragma warning disable 1692
577
+ #pragma warning disable IDE1006 // Suppress 'Naming rule violation' warnings
578
+ #pragma warning disable 0649
579
+ [ SerializeField ] private Sprite m_Sprite;
580
+ public Sprite sprite
581
+ {
582
+ get => m_Sprite;
583
+ set
584
+ {
585
+ if ( SetPropertyUtility.SetClass( ref m_Sprite, value ) )
586
+ {
587
+ SetAllDirty();
588
+ TrackImage();
589
+ }
590
+ }
591
+ }
592
+
593
+ [ SerializeField ] private FillDirection m_FillDirection;
594
+ public FillDirection fillDirection
595
+ {
596
+ get => m_FillDirection;
597
+ set
598
+ {
599
+ if ( SetPropertyUtility.SetStruct( ref m_FillDirection, value ) )
600
+ SetVerticesDirty();
601
+ }
602
+ }
603
+
604
+ [ Range( 0, 1 ) ][ SerializeField ] private float m_FillAmount = 1f;
605
+ public float fillAmount
606
+ {
607
+ get => m_FillAmount;
608
+ set
609
+ {
610
+ if ( SetPropertyUtility.SetStruct( ref m_FillAmount, Mathf.Clamp01( value ) ) )
611
+ SetVerticesDirty();
612
+ }
613
+ }
614
+
615
+ [ SerializeField ] private bool m_FillCenter = true;
616
+ public bool fillCenter
617
+ {
618
+ get => m_FillCenter;
619
+ set
620
+ {
621
+ if ( SetPropertyUtility.SetStruct( ref m_FillCenter, value ) )
622
+ SetVerticesDirty();
623
+ }
624
+ }
625
+
626
+ [ SerializeField ] private bool m_PreserveBorder;
627
+ public bool preserveBorder
628
+ {
629
+ get => m_PreserveBorder;
630
+ set
631
+ {
632
+ if ( SetPropertyUtility.SetStruct( ref m_PreserveBorder, value ) )
633
+ SetVerticesDirty();
634
+ }
635
+ }
636
+
637
+ [ SerializeField ] private float m_PixelsPerUnitMultiplier = 1f;
638
+ public float pixelsPerUnitMultiplier
639
+ {
640
+ get => m_PixelsPerUnitMultiplier;
641
+ set => m_PixelsPerUnitMultiplier = Mathf.Max( 0.01f, value );
642
+ }
643
+
644
+ public float pixelsPerUnit
645
+ {
646
+ get
647
+ {
648
+ float spritePixelsPerUnit = 100;
649
+ if ( activeSprite )
650
+ spritePixelsPerUnit = activeSprite.pixelsPerUnit;
651
+
652
+ float referencePixelsPerUnit = 100;
653
+ if ( canvas )
654
+ referencePixelsPerUnit = canvas.referencePixelsPerUnit;
655
+
656
+ return m_PixelsPerUnitMultiplier * spritePixelsPerUnit / referencePixelsPerUnit;
657
+ }
658
+ }
659
+ #pragma warning restore 0649
660
+
661
+ [ NonSerialized ] private Sprite m_OverrideSprite;
662
+ public Sprite overrideSprite
663
+ {
664
+ get => activeSprite;
665
+ set
666
+ {
667
+ if ( SetPropertyUtility.SetClass( ref m_OverrideSprite, value ) )
668
+ {
669
+ SetAllDirty();
670
+ TrackImage();
671
+ }
672
+ }
673
+ }
674
+
675
+ private Sprite activeSprite => m_OverrideSprite != null ? m_OverrideSprite : m_Sprite;
676
+
677
+ public override Texture mainTexture
678
+ {
679
+ get
680
+ {
681
+ if ( activeSprite != null )
682
+ return activeSprite.texture;
683
+
684
+ return material != null && material.mainTexture != null ? material.mainTexture : s_WhiteTexture;
685
+ }
686
+ }
687
+
688
+ public bool hasBorder
689
+ {
690
+ get
691
+ {
692
+ if ( activeSprite != null )
693
+ {
694
+ Vector4 v = activeSprite.border;
695
+ return v.sqrMagnitude > 0f;
696
+ }
697
+
698
+ return false;
699
+ }
700
+ }
701
+
702
+ public override Material material
703
+ {
704
+ get
705
+ {
706
+ if ( m_Material != null )
707
+ return m_Material;
708
+
709
+ if ( activeSprite && activeSprite.associatedAlphaSplitTexture != null )
710
+ {
711
+ #if UNITY_EDITOR
712
+ if ( Application.isPlaying )
713
+ #endif
714
+ return Image.defaultETC1GraphicMaterial;
715
+ }
716
+
717
+ return defaultMaterial;
718
+ }
719
+ set { base.material = value; }
720
+ }
721
+
722
+ public float alphaHitTestMinimumThreshold { get; set; }
723
+ #pragma warning restore IDE1006
724
+ #pragma warning restore 1692
725
+
726
+ #if UNITY_2017_4 || UNITY_2018_2_OR_NEWER
727
+ private static readonly List< SlicedFilledImage > m_TrackedTexturelessImages = new();
728
+ private static bool s_Initialized;
729
+ #endif
730
+ }