Skip to content

Commit fdd6bf0

Browse files
authored
Merge pull request #604 from CesiumGS/fix-credit-bug
Fix detection of credit image loading
2 parents dc038b6 + 24cfb74 commit fdd6bf0

5 files changed

Lines changed: 50 additions & 21 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Added option to ignore the `KHR_material_unlit` extension to force default lighting on tilesets.
88

9+
##### Fixes :wrench:
10+
11+
- Fixed a bug where `CesiumCreditSystem` did not accurately track the loading progress of images, which could result in missing credits.
12+
913
## v1.17.0 - 2025-08-01
1014

1115
##### Additions :tada:

Runtime/CesiumCreditSystem.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,23 @@ public static CesiumCreditSystem GetDefaultCreditSystem()
267267
return _defaultCreditSystem;
268268
}
269269

270-
internal bool HasLoadingImages()
270+
/// <summary>
271+
/// Gets the number of images on this credit system that are being loaded.
272+
/// </summary>
273+
/// <returns>The number of loading images.</returns>
274+
internal int GetNumberOfLoadingImages()
271275
{
272-
return this._numLoadingImages > 0;
276+
return this._numLoadingImages;
273277
}
274278

279+
/// <summary>
280+
/// A function invoked with StartCoroutine() to asynchronously load images from an HTML credit.
281+
/// </summary>
282+
/// <param name="url">A string containing either the base64-encoded image data or the URL of the image.</param>
275283
internal IEnumerator LoadImage(string url)
276284
{
277285
int index = this._images.Count;
286+
this._numLoadingImages++;
278287

279288
// Initialize a texture of arbitrary size as a placeholder,
280289
// so that when other images are loaded, their IDs align properly
@@ -287,23 +296,29 @@ internal IEnumerator LoadImage(string url)
287296
// Load an image from a string that contains the
288297
// "data:image/png;base64," prefix
289298
string byteString = url.Substring(base64Prefix.Length);
290-
byte[] bytes = Convert.FromBase64String(byteString);
291-
if (!texture.LoadImage(bytes))
299+
try
300+
{
301+
byte[] bytes = Convert.FromBase64String(byteString);
302+
if (!texture.LoadImage(bytes))
303+
{
304+
Debug.Log("Credit image could not be loaded into Texture2D.");
305+
}
306+
}
307+
catch (FormatException e)
292308
{
293-
Debug.Log("Could not parse image from base64 string.");
309+
Debug.Log("Could not parse credit image from base64 string.");
294310
}
295311
}
296312
else
297313
{
298314
// Load an image from a URL.
299315
UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
300-
this._numLoadingImages++;
301316
yield return request.SendWebRequest();
302317

303318
if (request.result == UnityWebRequest.Result.ConnectionError ||
304319
request.result == UnityWebRequest.Result.ProtocolError)
305320
{
306-
Debug.Log(request.error);
321+
Debug.LogError(request.error);
307322
}
308323
else
309324
{
@@ -314,10 +329,10 @@ internal IEnumerator LoadImage(string url)
314329
UnityLifetime.Destroy(placeholderTexture);
315330
}
316331

317-
this._numLoadingImages--;
318332
}
319333

320334
texture.wrapMode = TextureWrapMode.Clamp;
335+
this._numLoadingImages--;
321336
}
322337
}
323338
}

Runtime/ConfigureReinterop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public void ExposeToCPP()
495495
credits.Add(credit);
496496
credits.Clear();
497497

498-
if (!creditSystem.HasLoadingImages())
498+
if (creditSystem.GetNumberOfLoadingImages() == 0)
499499
{
500500
creditSystem.BroadcastCreditsUpdate();
501501
}

native~/Runtime/src/CesiumCreditSystemImpl.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ CesiumCreditSystemImpl::CesiumCreditSystemImpl(
3232
: _pCreditSystem(std::make_shared<CesiumUtility::CreditSystem>()),
3333
_htmlToUnityCredit(),
3434
_lastCreditsCount(0),
35-
_creditsUpdated(false) {}
35+
_lastLoadingImagesCount(0),
36+
_shouldBroadcastUpdate(false) {}
3637

3738
CesiumCreditSystemImpl::~CesiumCreditSystemImpl() {}
3839

@@ -43,24 +44,32 @@ void CesiumCreditSystemImpl::UpdateCredits(
4344
return;
4445
}
4546

46-
// If the credits were updated in a previous frame, the update will not get
47-
// broadcasted until all of the images are fully loaded. Broadcast the
48-
// previous update first before handling the next one.
49-
if (this->_creditsUpdated && !creditSystem.HasLoadingImages()) {
47+
// Images are handled asynchronously by Unity's coroutine system, so their
48+
// progress must be accounted for separately.
49+
size_t loadingImagesCount = creditSystem.GetNumberOfLoadingImages();
50+
if (loadingImagesCount != this->_lastLoadingImagesCount) {
51+
this->_shouldBroadcastUpdate = true;
52+
this->_lastLoadingImagesCount = loadingImagesCount;
53+
}
54+
55+
// If the credits were updated in a previous frame, do not broadcast the
56+
// update until all of the images are fully loaded. Then, broadcast the
57+
// previous update before handling the next one.
58+
if (this->_shouldBroadcastUpdate && loadingImagesCount == 0) {
5059
creditSystem.BroadcastCreditsUpdate();
51-
this->_creditsUpdated = false;
60+
this->_shouldBroadcastUpdate = false;
5261
}
5362

5463
const CesiumUtility::CreditsSnapshot& credits = _pCreditSystem->getSnapshot();
5564
const std::vector<CesiumUtility::Credit>& creditsToShowThisFrame =
5665
credits.currentCredits;
5766

5867
size_t creditsCount = creditsToShowThisFrame.size();
59-
this->_creditsUpdated = forceUpdate ||
60-
creditsToShowThisFrame.size() != _lastCreditsCount ||
61-
credits.removedCredits.size() > 0;
68+
bool creditsUpdated =
69+
forceUpdate || creditsToShowThisFrame.size() != this->_lastCreditsCount ||
70+
credits.removedCredits.size() > 0;
6271

63-
if (this->_creditsUpdated) {
72+
if (creditsUpdated) {
6473
List1<CesiumForUnity::CesiumCredit> popupCredits =
6574
creditSystem.popupCredits();
6675
List1<CesiumForUnity::CesiumCredit> onScreenCredits =
@@ -94,8 +103,8 @@ void CesiumCreditSystemImpl::UpdateCredits(
94103
}
95104
}
96105

97-
this->_creditsUpdated = true;
98106
this->_lastCreditsCount = creditsCount;
107+
this->_shouldBroadcastUpdate = true;
99108
}
100109
}
101110

native~/Runtime/src/CesiumCreditSystemImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class CesiumCreditSystemImpl : public CesiumImpl<CesiumCreditSystemImpl> {
4848
_htmlToUnityCredit;
4949

5050
size_t _lastCreditsCount;
51-
bool _creditsUpdated;
51+
size_t _lastLoadingImagesCount;
52+
bool _shouldBroadcastUpdate;
5253
};
5354

5455
} // namespace CesiumForUnityNative

0 commit comments

Comments
 (0)