Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add property 'url' to indecate the true url of native object #4536

Merged
merged 3 commits into from Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions cocos2d/core/assets/CCAsset.js
Expand Up @@ -102,6 +102,21 @@ cc.Asset = cc.Class({
visible: false
},

/**
* !#en
* Points to the true url of this asset's native object, only valid when asset is loaded and asyncLoadAsset is not enabled.
* Url equals nativeUrl on web(web-mobile, web-desktop) or native(iOS, Android etc) platform. The difference between
* nativeUrl and url is that url may points to temporary path or cached path on mini game platform which has cache mechanism (WeChat etc).
* If you want to make use of the native file on those platforms, you should use url instead of nativeUrl.
* !#zh
* 资源的原生文件的真实url,只在资源被加载后以及没有启用延迟加载时才有效。在web平台(web-mobile, web-desktop)或者原生平台(iOS,安卓等)上url与
* nativeUrl是相等的,nativeUrl与url的区别在于,某些带缓存机制的小游戏平台(微信等)上url可能会指向临时文件路径或者缓存路径,如果你需要在这些平台上使用资源的原生文件,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“原始文件”吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里概念不好描述,准确一点的说法应该是原生资源的文件。比如cc.Texture2d的原生资源的文件是图片,但是cc.Texutre2d本身也有个json文件,为了区别这个json和图片,所以描述为“原生”

* 请使用url,避免使用nativeUrl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赞,很清晰的注释!!!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

试试看哈。

* @property url
* @type {String}
*/
url: '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用户为什么会想要原始资源的 url ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有时候用户想要直接用平台的api处理资源文件,比如播放音频,会需要这个url


/**
* Serializable url for native asset.
* @property {String} _native
Expand Down
14 changes: 0 additions & 14 deletions cocos2d/core/assets/CCTexture2D.js
Expand Up @@ -317,18 +317,6 @@ var Texture2D = cc.Class({
ctor () {
// Id for generate hash in material
this._id = idGenerater.getNewId();

/**
* !#en
* The url of the texture, this could be empty if the texture wasn't created via a file.
* !#zh
* 贴图文件的 url,当贴图不是由文件创建时值可能为空
* @property url
* @type {String}
* @readonly
*/
// TODO - use nativeUrl directly
this.url = "";

/**
* !#en
Expand Down Expand Up @@ -864,8 +852,6 @@ var Texture2D = cc.Class({
let uuid = loadingItem && loadingItem.uuid;
if (uuid) {
this._uuid = uuid;
var url = this.nativeUrl;
this.url = url;
}
}
if (fields.length === 6) {
Expand Down
14 changes: 7 additions & 7 deletions cocos2d/core/load-pipeline/CCLoader.js
Expand Up @@ -816,17 +816,17 @@ proto.release = function (asset) {
if (item) {
var removed = this.removeItem(id);
asset = item.content;
if (asset instanceof cc.Asset) {
let nativeUrl = asset.nativeUrl;
if (nativeUrl) {
this.release(nativeUrl); // uncache loading item of native asset
}
asset.destroy();
}
if (CC_DEBUG && removed) {
this._releasedAssetChecker_DEBUG.setReleased(item, id);
}
}
if (asset instanceof cc.Asset) {
let nativeUrl = asset.nativeUrl;
if (nativeUrl) {
this.release(nativeUrl); // uncache loading item of native asset
}
asset.destroy();
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions cocos2d/core/load-pipeline/font-loader.js
Expand Up @@ -32,8 +32,8 @@ let _testString = "BES bswy:->@123\u4E01\u3041\u1101";
let _fontFaces = {};
let _intervalId = -1;
let _loadingFonts = [];
// 60 seconds timeout
let _timeout = 60000;
// 3 seconds timeout
let _timeout = 3000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果网络差一点,加载超过 3 秒咋办?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前的 60 太长了,这个只能取一个折衷值, @holycanvas 可以测一下一个中文的标准 ttf 文件 加载在 4G 网络环境下需要多久,取一个可以容纳的近似值

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实超过三秒也没关系,如果在字体还没加载完成的情况下就进行使用,系统会用默认字体来渲染,等真正字体加载完成后,会替换为正确的字体的,也就是说,在字体比较大的时候,可能会有一段的时间显示的是系统字体,然后突然变成正确的字体。有点类似延迟加载的感觉。关键是想要准确把握这个时间点很麻烦,标准中文字体一般15M以上,4g的网速也就500kb左右,这样至少也要30s的时间,提前进入游戏感觉还是好一点,即便字体不对

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没人会为 web 游戏带上一个 15M 的字体的,都是需要裁剪的ttf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个时间我没意见,60 确实太长。


// Refer to https://github.com/typekit/webfontloader/blob/master/src/core/fontwatcher.js
let useNativeCheck = (function () {
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/core/load-pipeline/loader.js
Expand Up @@ -62,11 +62,10 @@ function loadImage (item) {
}

// load cc.Texture2D
var rawUrl = item.rawUrl;
var tex = item.texture || new Texture2D();
tex._uuid = item.uuid;
tex.url = rawUrl;
tex._setRawAsset(rawUrl, false);
tex.url = item.url;
tex._setRawAsset(item.rawUrl, false);
tex._nativeAsset = image;
return tex;
}
Expand All @@ -83,6 +82,7 @@ function loadAudioAsAsset (item, callback) {
var audioClip = new cc.AudioClip();
audioClip._setRawAsset(item.rawUrl, false);
audioClip._nativeAsset = item.content;
audioClip.url = item.url;
return audioClip;
}

Expand Down
4 changes: 4 additions & 0 deletions cocos2d/core/load-pipeline/uuid-loader.js
Expand Up @@ -134,6 +134,9 @@ function loadDepends (pipeline, item, asset, depends, callback) {
if (this._stillUseUrl) {
value = (value && cc.RawAsset.wasRawAssetType(value.constructor)) ? value.nativeUrl : item.rawUrl;
}
if (this._ownerProp === '_nativeAsset') {
this._owner.url = item.url;
}
this._owner[this._ownerProp] = value;
if (item.uuid !== asset._uuid && dependKeys.indexOf(item.id) < 0) {
dependKeys.push(item.id);
Expand Down Expand Up @@ -275,6 +278,7 @@ function loadUuid (item, callback) {
}

asset._uuid = item.uuid;
asset.url = asset.nativeUrl;

if (CC_EDITOR && isScene && MissingClass.hasMissingClass) {
MissingClass.reportMissingClass(asset);
Expand Down