Skip to content

Commit 6a44dbc

Browse files
authored
Assets fix (playcanvas#2893)
* add check
1 parent 3c3dc59 commit 6a44dbc

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/asset/asset.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { findAvailableLocale } from '../i18n/utils.js';
77

88
import { ABSOLUTE_URL } from './constants.js';
99
import { AssetVariants } from './asset-variants.js';
10+
import { getApplication } from '../framework/globals.js';
1011

1112
// auto incrementing number for asset ids
1213
var assetIdCounter = -1;
@@ -192,29 +193,30 @@ class Asset extends EventHandler {
192193
return null;
193194

194195
if (this.type === 'texture' || this.type === 'textureatlas' || this.type === 'bundle') {
195-
var app = this.registry._loader._app;
196-
var device = app.graphicsDevice;
197-
198-
for (var i = 0, len = VARIANT_DEFAULT_PRIORITY.length; i < len; i++) {
199-
var variant = VARIANT_DEFAULT_PRIORITY[i];
200-
// if the device supports the variant
201-
if (! device[VARIANT_SUPPORT[variant]]) continue;
202-
203-
// if the variant exists in the asset then just return it
204-
if (this.file.variants[variant]) {
205-
return this.file.variants[variant];
206-
}
207-
208-
// if the variant does not exist but the asset is in a bundle
209-
// and the bundle contain assets with this variant then return the default
210-
// file for the asset
211-
if (app.enableBundles) {
212-
var bundles = app.bundles.listBundlesForAsset(this);
213-
if (! bundles) continue;
196+
var app = this.registry?._loader?._app || getApplication();
197+
var device = app?.graphicsDevice;
198+
if (device) {
199+
for (var i = 0, len = VARIANT_DEFAULT_PRIORITY.length; i < len; i++) {
200+
var variant = VARIANT_DEFAULT_PRIORITY[i];
201+
// if the device supports the variant
202+
if (! device[VARIANT_SUPPORT[variant]]) continue;
203+
204+
// if the variant exists in the asset then just return it
205+
if (this.file.variants[variant]) {
206+
return this.file.variants[variant];
207+
}
214208

215-
for (var j = 0, len2 = bundles.length; j < len2; j++) {
216-
if (bundles[j].file && bundles[j].file.variants && bundles[j].file.variants[variant]) {
217-
return this.file;
209+
// if the variant does not exist but the asset is in a bundle
210+
// and the bundle contain assets with this variant then return the default
211+
// file for the asset
212+
if (app.enableBundles) {
213+
var bundles = app.bundles.listBundlesForAsset(this);
214+
if (! bundles) continue;
215+
216+
for (var j = 0, len2 = bundles.length; j < len2; j++) {
217+
if (bundles[j].file && bundles[j].file.variants && bundles[j].file.variants[variant]) {
218+
return this.file;
219+
}
218220
}
219221
}
220222
}

0 commit comments

Comments
 (0)