deferred load scene's raw asset

This commit is contained in:
Jare Guo
2016-07-06 23:17:11 +08:00
parent 3fce900878
commit ef436d9da4
17 changed files with 250 additions and 115 deletions
+1 -7
View File
@@ -809,7 +809,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
if (error) {
error = 'Failed to load scene: ' + error;
cc.error(error);
if (CC_EDITOR) {
if (CC_DEV) {
console.assert(false, error);
}
}
@@ -817,12 +817,6 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
if (sceneAsset instanceof cc.SceneAsset) {
scene = sceneAsset.scene;
scene._id = sceneAsset._uuid;
}
else {
// hack for preview
scene = sceneAsset;
}
if (scene instanceof cc.Scene) {
scene._name = sceneAsset._name;
self.runSceneImmediate(scene, onUnloaded, onLaunched);
}
+11 -1
View File
@@ -38,6 +38,16 @@ cc.Scene = cc.Class({
name: 'cc.Scene',
extends: require('./utils/base-node'),
//properties: {
// /**
// * !#en Indicates the raw assets of this scene can be load after scene launched.
// * !#zh 指示该场景依赖的资源可否在场景切换后再延迟加载。
// * @property {Boolean} _asyncLoadAssets
// * @default false
// */
// _asyncLoadAssets: false
//},
ctor: function () {
var sgNode = this._sgNode = new _ccsg.Scene();
if (CC_JSB) {
@@ -103,7 +113,7 @@ cc.Scene = cc.Class({
module.exports = cc.Scene;
if (CC_EDITOR) {
if (CC_DEV) {
var ERR = '"%s" is not defined in the Scene, it is only defined in child nodes.';
Object.defineProperties(cc.Scene.prototype, {
active: {
+11 -1
View File
@@ -124,7 +124,17 @@ cc.Asset = cc.Class({
*/
deserialize: function (data) {
return cc.deserialize(data);
}
},
/**
* !#en Indicates whether its dependent raw assets can support deferred load if the owner scene is marked as `asyncLoadAssets`.
* !#zh 当场景被标记为 `asyncLoadAssets`,禁止延迟加载该资源所依赖的其它 RawAsset。
*
* @property {Boolean} preventDeferredLoadDependents
* @default false
* @static
*/
preventDeferredLoadDependents: false
},
/**
+8 -1
View File
@@ -35,7 +35,14 @@ var Scene = cc.Class({
extends: cc.Asset,
properties: {
scene: null
scene: null,
/**
* !#en Indicates the raw assets of this scene can be load after scene launched.
* !#zh 指示该场景依赖的资源可否在场景切换后再延迟加载。
* @property {Boolean} asyncLoadAssets
* @default undefined
*/
asyncLoadAssets: undefined
},
});
+14
View File
@@ -327,6 +327,20 @@ var Button = cc.Class({
},
onEnable: function () {
// check sprite frames
if (this.normalSprite) {
this.normalSprite.ensureLoadTexture();
}
if (this.hoverSprite) {
this.hoverSprite.ensureLoadTexture();
}
if (this.pressedSprite) {
this.pressedSprite.ensureLoadTexture();
}
if (this.disabledSprite) {
this.disabledSprite.ensureLoadTexture();
}
//
if (!CC_EDITOR) {
this._registerEvent();
} else {
+20 -20
View File
@@ -308,24 +308,24 @@ var Component = cc.Class({
__scriptAsset: CC_EDITOR && {
get: function () {},
set: function (value) {
if (this.__scriptUuid !== value) {
if (value && Editor.UuidUtils.isUuid(value._uuid)) {
var classId = Editor.UuidUtils.compressUuid(value._uuid);
var NewComp = cc.js._getClassById(classId);
if (cc.isChildClassOf(NewComp, cc.Component)) {
cc.warn('Sorry, replacing component script is not yet implemented.');
//Editor.Ipc.sendToWins('reload:window-scripts', Editor._Sandbox.compiled);
}
else {
cc.error('Can not find a component in the script which uuid is "%s".', value._uuid);
}
}
else {
cc.error('Invalid Script');
}
}
},
//set: function (value) {
// if (this.__scriptUuid !== value) {
// if (value && Editor.UuidUtils.isUuid(value._uuid)) {
// var classId = Editor.UuidUtils.compressUuid(value._uuid);
// var NewComp = cc.js._getClassById(classId);
// if (cc.isChildClassOf(NewComp, cc.Component)) {
// cc.warn('Sorry, replacing component script is not yet implemented.');
// //Editor.Ipc.sendToWins('reload:window-scripts', Editor._Sandbox.compiled);
// }
// else {
// cc.error('Can not find a component in the script which uuid is "%s".', value._uuid);
// }
// }
// else {
// cc.error('Invalid Script');
// }
// }
//},
displayName: 'Script',
type: cc._Script,
tooltip: 'i18n:INSPECTOR.component.script'
@@ -445,8 +445,8 @@ var Component = cc.Class({
onLoad: null,
/**
* !#en Called before all scripts' update if the Component is enabled.
* !#zh 如果该组件启用,则在所有组件的 update 之前调用。
* !#en Called before all scripts' update if the Component is enabled the first time.
* !#zh 如果该组件第一次启用,则在所有组件的 update 之前调用。
* @method start
*/
start: null,
+1 -1
View File
@@ -407,7 +407,7 @@ var EditBox = cc.Class({
var bgSprite = new cc.Scale9Sprite();
bgSprite.setRenderingType(cc.Scale9Sprite.RenderingType.SLICED);
if (this.backgroundImage) {
this.backgroundImage.ensureLoadTexture();
bgSprite.setSpriteFrame(this.backgroundImage);
this._applyCapInset(bgSprite);
}
+6 -4
View File
@@ -563,12 +563,14 @@ var Sprite = cc.Class({
oldFrame.off('load', this._onSpriteFrameLoaded, this);
}
if (this._spriteFrame) {
if (this._spriteFrame.textureLoaded()) {
var spriteFrame = this._spriteFrame;
if (spriteFrame) {
if (spriteFrame.textureLoaded()) {
this._onSpriteFrameLoaded(null);
}
else {
this._spriteFrame.once('load', this._onSpriteFrameLoaded, this);
spriteFrame.once('load', this._onSpriteFrameLoaded, this);
spriteFrame.ensureLoadTexture();
}
}
else {
@@ -577,7 +579,7 @@ var Sprite = cc.Class({
if (CC_EDITOR) {
// Set atlas
this._applyAtlas(this._spriteFrame);
this._applyAtlas(spriteFrame);
}
},
+82 -23
View File
@@ -39,25 +39,55 @@ function isSceneObj (json) {
);
}
function loadDepends (pipeline, item, asset, tdInfo, callback) {
var uuid = item.id;
var uuidList = tdInfo.uuidList;
var ownerList = JS.array.copy(tdInfo.uuidObjList);
var propList = JS.array.copy(tdInfo.uuidPropList);
var depends = new Array(uuidList.length);
// load depends assets
for (var i = 0; i < uuidList.length; i++) {
var dependUuid = uuidList[i];
depends[i] = {
id: dependUuid,
type: 'uuid',
uuid: dependUuid
};
function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRuntime, callback) {
var uuid = item.id, uuidList = tdInfo.uuidList;
var objList, propList, depends;
var i, dependUuid;
if (deferredLoadRawAssetsInRuntime) {
objList = [];
propList = [];
depends = [];
// parse depends assets
for (i = 0; i < uuidList.length; i++) {
dependUuid = uuidList[i];
var obj = tdInfo.uuidObjList[i];
var prop = tdInfo.uuidPropList[i];
var info = cc.AssetLibrary._getAssetInfoInRuntime(dependUuid);
if (info.raw) {
// skip preloading raw assets
var url = info.url;
obj[prop] = url;
}
else {
objList.push(obj);
propList.push(prop);
// declare depends assets
depends.push({
id: dependUuid,
type: 'uuid',
uuid: dependUuid,
deferredLoadRaw: true,
});
}
}
}
// load raw
else {
objList = JS.array.copy(tdInfo.uuidObjList);
propList = JS.array.copy(tdInfo.uuidPropList);
depends = new Array(uuidList.length);
// declare depends assets
for (i = 0; i < uuidList.length; i++) {
dependUuid = uuidList[i];
depends[i] = {
id: dependUuid,
type: 'uuid',
uuid: dependUuid
};
}
}
// declare raw
if (tdInfo.rawProp) {
ownerList.push(asset);
objList.push(asset);
propList.push(tdInfo.rawProp);
depends.push(item.url);
}
@@ -77,13 +107,13 @@ function loadDepends (pipeline, item, asset, tdInfo, callback) {
}
for (var i = 0; i < depends.length; i++) {
var dependSrc = depends[i].uuid;
var obj = ownerList[i];
var dependObj = objList[i];
var dependProp = propList[i];
item = items[dependSrc];
if (item) {
if (item.complete) {
var value = item.isRawAsset ? item.url : item.content;
obj[dependProp] = value;
dependObj[dependProp] = value;
}
else {
// item was removed from cache, but ready in pipeline actually
@@ -92,7 +122,7 @@ function loadDepends (pipeline, item, asset, tdInfo, callback) {
this.obj[this.prop] = value;
};
var target = {
obj: obj,
obj: dependObj,
prop: dependProp
};
// Hack to get a better behavior
@@ -114,6 +144,32 @@ function loadDepends (pipeline, item, asset, tdInfo, callback) {
});
}
// can deferred load raw assets in runtime
function canDeferredLoad (asset, item, isScene) {
if (CC_EDITOR) {
return false;
}
var res = item.deferredLoadRaw;
if (res) {
// check if asset support deferred
if (asset instanceof cc.Asset && asset.constructor.preventDeferredLoadDependents) {
res = false;
}
}
else if (isScene) {
if (asset instanceof cc.SceneAsset) {
res = asset.asyncLoadAssets;
if (res) {
cc.log('deferred load raw assets for ' + item.id);
}
}
//else if (asset instanceof cc.Scene) {
// deferredLoadRawAssetsInRuntime = asset._asyncLoadAssets;
//}
}
return res;
}
function loadUuid (item, callback) {
var json;
if (typeof item.content === 'string') {
@@ -133,7 +189,8 @@ function loadUuid (item, callback) {
return;
}
var classFinder = isSceneObj(json) ? cc._MissingScript.safeFindClass : function (id) {
var isScene = isSceneObj(json);
var classFinder = isScene ? cc._MissingScript.safeFindClass : function (id) {
var cls = JS._getClassById(id);
if (cls) {
return cls;
@@ -148,7 +205,8 @@ function loadUuid (item, callback) {
try {
asset = cc.deserialize(json, tdInfo, {
classFinder: classFinder,
target: item.existingAsset
target: item.existingAsset,
customEnv: item
});
}
catch (e) {
@@ -156,7 +214,8 @@ function loadUuid (item, callback) {
return;
}
loadDepends(this.pipeline, item, asset, tdInfo, callback);
var deferredLoad = canDeferredLoad(asset, item, isScene);
loadDepends(this.pipeline, item, asset, tdInfo, deferredLoad, callback);
// tdInfo 是用来重用的临时对象,每次使用后都要重设,这样才对 GC 友好。
tdInfo.reset();
+1
View File
@@ -1031,6 +1031,7 @@ function parseAttributes (attrs, className, propName) {
parseSimpleAttr('rawType', 'string', Attr.RawType);
parseSimpleAttr('editorOnly', 'boolean', Attr.EditorOnly);
//parseSimpleAttr('preventDeferredLoad', 'boolean');
if (CC_DEV) {
parseSimpleAttr('displayName', 'string');
parseSimpleAttr('multiline', 'boolean', {multiline: true});
+4 -2
View File
@@ -161,7 +161,7 @@ var _Deserializer = (function () {
///**
// * @param {Boolean} isEditor - if false, "editorOnly" properties will be discarded
// */
function _Deserializer(jsonObj, result, target, classFinder) {
function _Deserializer(jsonObj, result, target, classFinder, customEnv) {
this._classFinder = classFinder;
if (ENABLE_TARGET) {
this._target = target;
@@ -170,6 +170,7 @@ var _Deserializer = (function () {
this._idObjList = [];
this._idPropList = [];
this.result = result || new Details();
this.customEnv = customEnv;
if (Array.isArray(jsonObj)) {
var jsonArray = jsonObj;
@@ -517,6 +518,7 @@ cc.deserialize = function (data, result, options) {
// 启用 createAssetRefs 后,如果有 url 属性则会被统一强制设置为 { uuid: 'xxx' },必须后面再特殊处理
var createAssetRefs = (options && options.createAssetRefs) || cc.sys.platform === cc.sys.EDITOR_CORE;
var target = ENABLE_TARGET && (options && options.target);
var customEnv = (options && options.customEnv);
if (CC_EDITOR && Buffer.isBuffer(data)) {
data = data.toString();
@@ -532,7 +534,7 @@ cc.deserialize = function (data, result, options) {
result = new Details();
}
cc.game._isCloning = true;
var deserializer = new _Deserializer(data, result, target, classFinder);
var deserializer = new _Deserializer(data, result, target, classFinder, customEnv);
cc.game._isCloning = false;
if (createAssetRefs) {
+47 -28
View File
@@ -81,9 +81,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
// just packing
return;
}
var texture = cc.textureCache.addImage(url);
this._refreshTexture(texture);
this._loadTexture();
}
}
}
@@ -373,7 +371,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
/*
* Initializes SpriteFrame with Texture, rect, rotated, offset and originalSize in pixels.<br/>
* Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.
* @method initWithTexture
* @method setTexture
* @param {String|Texture2D} texture
* @param {Rect} [rect=null]
* @param {Boolean} [rotated=false]
@@ -381,27 +379,25 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @param {Size} [originalSize=rect.size]
* @return {Boolean}
*/
initWithTexture: function (textureOrTextureFile, rect, rotated, offset, originalSize) {
this.setTexture(textureOrTextureFile, rect, rotated, offset, originalSize);
},
setTexture: function (textureOrTextureFile, rect, rotated, offset, originalSize) {
if (rect) {
this.setRect(rect);
} else {
}
else {
this._rect = null;
}
if (offset) {
this.setOffset(offset);
} else {
}
else {
this._offset = null;
}
if (originalSize) {
this.setOriginalSize(originalSize);
} else {
}
else {
this._originalSize = null;
}
@@ -411,17 +407,49 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
var texture = textureOrTextureFile;
if (cc.js.isString(texture)) {
this._textureFilename = texture;
texture = cc.textureCache.addImage(texture);
this._loadTexture();
}
if (texture instanceof cc.Texture2D) {
else if (texture instanceof cc.Texture2D) {
this._refreshTexture(texture);
} else {
}
else {
//todo log error
}
return true;
},
_loadTexture: function () {
if (this._textureFilename) {
var texture = cc.textureCache.addImage(this._textureFilename);
this._refreshTexture(texture);
}
},
/**
* !#en If a loading scene is marked as `asyncLoadAssets`, all the textures of the SpriteFrame which
* associated by user's custom Components in the scene, will not preload automatically.
* These textures will be load when Sprite component is going to render the SpriteFrames.
* You can call this method if you want to load the texture early.
* !#zh 当加载中的场景被标记为 `asyncLoadAssets` 时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。
* 只有当 Sprite 组件要渲染这些 SpriteFrame 时,才会检查贴图是否加载。如果你希望加载过程提前,你可以手工调用这个方法。
*
* @method ensureLoadTexture
* @example
* if (spriteFrame.textureLoaded()) {
* this._onSpriteFrameLoaded();
* }
* else {
* spriteFrame.once('load', this._onSpriteFrameLoaded, this);
* spriteFrame.ensureLoadTexture();
* }
*/
ensureLoadTexture: function () {
if (!this._texture) {
this._loadTexture();
}
},
_checkRect: function (texture) {
var rect = this._rect;
var maxX = rect.x, maxY = rect.y;
@@ -503,24 +531,15 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
// load texture via _textureFilenameSetter
var textureUuid = data.texture;
if (textureUuid) {
handle.result.push(this, '_textureFilenameSetter', textureUuid);
var dontLoadTexture = (handle.customEnv && handle.customEnv.deferredLoadRaw);
var receiver = dontLoadTexture ? '_textureFilename' : '_textureFilenameSetter';
handle.result.push(this, receiver, textureUuid);
}
}
});
var proto = cc.SpriteFrame.prototype;
/*
* Copy the sprite frame
* @method copyWithZone
* @return {SpriteFrame}
*/
proto.copyWithZone = proto.clone;
/*
* Copy the sprite frame
* @method copy
* @returns {SpriteFrame}
*/
proto.copy = proto.clone;
proto.initWithTexture = proto.setTexture;
+9 -9
View File
@@ -934,24 +934,24 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* !#en Sets the scale factor of the node. 1.0 is the default scale factor. This function can modify the X and Y scale at the same time.
* !#zh 设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。
* @method setScale
* @param {Number|Vec2} scale - scaleX or scale
* @param {Number|Vec2} scaleX - scaleX or scale
* @param {Number} [scaleY=scale]
* @example
* node.setScale(cc.v2(1, 1));
* node.setScale(1, 1);
*/
setScale: function (scale, scaleY) {
if (scale instanceof cc.Vec2) {
scaleY = scale.y;
scale = scale.x
setScale: function (scaleX, scaleY) {
if (typeof scaleX === 'object') {
scaleY = scaleX.y;
scaleX = scaleX.x
}
else {
scaleY = (scaleY || scaleY === 0) ? scaleY : scale;
scaleY = (scaleY || scaleY === 0) ? scaleY : scaleX;
}
if (this._scaleX !== scale || this._scaleY !== scaleY) {
this._scaleX = scale;
if (this._scaleX !== scaleX || this._scaleY !== scaleY) {
this._scaleX = scaleX;
this._scaleY = scaleY;
this._sgNode.setScale(scale, scaleY);
this._sgNode.setScale(scaleX, scaleY);
}
},
+4
View File
@@ -53,6 +53,10 @@ var TiledMapAsset = cc.Class({
}
},
statics: {
preventDeferredLoadDependents: true
},
createNode: CC_EDITOR && function (callback) {
var node = new cc.Node(this.name);
var tiledMap = node.addComponent(cc.TiledMap);
+4
View File
@@ -150,6 +150,10 @@ var SkeletonData = cc.Class({
scale: 1
},
statics: {
preventDeferredLoadDependents: true
},
// PUBLIC
createNode: function (callback) {
+5 -4
View File
@@ -331,15 +331,16 @@ cc.js.mixin(cc.director, {
if (error) {
error = 'Failed to load scene: ' + error;
cc.error(error);
if (CC_EDITOR) {
if (CC_DEV) {
console.assert(false, error);
}
}
else {
scene = sceneAsset.scene;
if (scene instanceof cc.Scene) {
if (sceneAsset instanceof cc.SceneAsset) {
scene = sceneAsset.scene;
scene._id = sceneAsset._uuid;
self.runScene(scene, onUnloaded, onLaunched);
scene._name = sceneAsset._name;
self.runSceneImmediate(scene, onUnloaded, onLaunched);
}
else {
error = 'The asset ' + uuid + ' is not a scene';
+22 -14
View File
@@ -79,21 +79,13 @@ prototype._ctor = function (filename, rect, rotated, offset, originalSize) {
}
};
prototype._initWithTexture = prototype.initWithTexture;
prototype.initWithTexture = function (texture, rect, rotated, offset, originalSize) {
this.setTexture(texture, rect, rotated, offset, originalSize);
};
prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, originalSize) {
if (rect) {
this.setRect(rect);
}
if (offset) {
this.setOffset(offset);
}
if (originalSize) {
this.setOriginalSize(originalSize);
}
@@ -103,10 +95,9 @@ prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, or
var texture = textureOrTextureFile;
if (cc.js.isString(textureOrTextureFile)) {
this._textureFilename = textureOrTextureFile;
texture = cc.textureCache.addImage(textureOrTextureFile);
this._loadTexture();
}
if (texture instanceof cc.Texture2D) {
else if (texture instanceof cc.Texture2D) {
this._refreshTexture(texture);
}
else {
@@ -116,6 +107,22 @@ prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, or
return true;
};
prototype._loadTexture = function () {
if (this._textureFilename) {
var texture = cc.textureCache.addImage(this._textureFilename);
this._refreshTexture(texture);
}
};
prototype.ensureLoadTexture = function () {
if (!this._texture) {
this._loadTexture();
}
};
prototype._initWithTexture = prototype.initWithTexture;
prototype.initWithTexture = prototype.setTexture;
prototype._refreshTexture = function (texture) {
if (this.getTexture() !== texture) {
@@ -178,7 +185,9 @@ prototype._deserialize = function (data, handle) {
// load texture via _textureFilenameSetter
var textureUuid = data.texture;
if (textureUuid) {
handle.result.push(this, '_textureFilenameSetter', textureUuid);
var dontLoadTexture = (handle.customEnv && handle.customEnv.deferredLoadRaw);
var receiver = dontLoadTexture ? '_textureFilename' : '_textureFilenameSetter';
handle.result.push(this, receiver, textureUuid);
}
};
prototype._checkRect = function (texture) {
@@ -210,8 +219,7 @@ prototype.getTexture = function () {
cc.js.set(prototype, '_textureFilenameSetter', function (url) {
this._textureFilename = url;
if (url) {
var texture = cc.textureCache.addImage(url);
this._refreshTexture(texture);
this._loadTexture();
}
});