Merge pull request #2207 from jareguo/master

add prefab option to optimize instantiation
This commit is contained in:
pandamicro
2017-12-26 10:53:40 +08:00
committed by GitHub
2 changed files with 92 additions and 12 deletions
+90 -12
View File
@@ -23,6 +23,51 @@
THE SOFTWARE.
****************************************************************************/
/**
* !#zh
* Prefab 创建实例所用的优化策略,配合 {{#crossLink "Prefab.optimizationPolicy"}}cc.Prefab#optimizationPolicy{{/crossLink}} 使用。
* !#en
* An enumeration used with the {{#crossLink "Prefab.optimizationPolicy"}}cc.Prefab#optimizationPolicy{{/crossLink}}
* to specify how to optimize the instantiate operation.
*
* @enum Prefab.OptimizationPolicy
* @since 1.8.0
*/
var OptimizationPolicy = cc.Enum({
/**
* !#zh
* 根据创建次数自动调整优化策略。初次创建实例时,行为等同 SINGLE_INSTANCE,多次创建后将自动采用 MULTI_INSTANCE。
* !#en
* The optimization policy is automatically chosen based on the number of instantiations.
* When you first create an instance, the behavior is the same as SINGLE_INSTANCE. MULTI_INSTANCE will be automatically used after multiple creation.
* @property {Number} AUTO
*/
AUTO: 0,
/**
* !#zh
* 优化单次创建性能。<br>
* 该选项会跳过针对这个 prefab 的代码生成优化操作。当该 prefab 加载后,一般只会创建一个实例时,请选择此项。
* !#en
* Optimize for single instance creation.<br>
* This option skips code generation for this prefab.
* When this prefab will usually create only one instances, please select this option.
* @property {Number} SINGLE_INSTANCE
*/
SINGLE_INSTANCE: 1,
/**
* !#zh
* 优化多次创建性能。<br>
* 该选项会启用针对这个 prefab 的代码生成优化操作。当该 prefab 加载后,一般会创建多个实例时,请选择此项。如果该 prefab 在场景中的节点启用了自动关联,并且在场景中有多份实例,也建议选择此项。
* !#en
* Optimize for creating instances multiple times.<br>
* This option enables code generation for this prefab.
* When this prefab will usually create multiple instances, please select this option.
* It is also recommended to select this option if the prefab instance in the scene has Auto Sync enabled and there are multiple instances in the scene.
* @property {Number} MULTI_INSTANCE
*/
MULTI_INSTANCE: 2,
});
/**
* !#en Class for prefab handling.
* !#zh 预制资源类。
@@ -32,6 +77,16 @@
var Prefab = cc.Class({
name: 'cc.Prefab',
extends: cc.Asset,
ctor () {
/**
* Cache function to optimize instance creaton.
* @property {Function} _createFunction
* @private
*/
this._createFunction = null;
this._instantiatedTimes = 0;
},
properties: {
/**
@@ -39,23 +94,33 @@ var Prefab = cc.Class({
*/
data: null,
/**
* !#zh
* 设置实例化这个 prefab 时所用的优化策略。根据使用情况设置为合适的值,能优化该 prefab 实例化所用的时间。
* !#en
* Indicates the optimization policy for instantiating this prefab.
* Set to a suitable value based on usage, can optimize the time it takes to instantiate this prefab.
*
* @property {Prefab.OptimizationPolicy} optimizationPolicy
* @default Prefab.OptimizationPolicy.AUTO
* @since 1.8.0
* @example
* prefab.optimizationPolicy = cc.Prefab.OptimizationPolicy.MULTI_INSTANCE;
*/
optimizationPolicy: OptimizationPolicy.AUTO,
/**
* !#en Indicates the raw assets of this prefab can be load after prefab loaded.
* !#zh 指示该 Prefab 依赖的资源可否在 Prefab 加载后再延迟加载。
* @property {Boolean} asyncLoadAssets
* @default false
*/
asyncLoadAssets: undefined,
asyncLoadAssets: false,
},
/**
* Cache function for fast instantiation
* @property {Function} _createFunction
* @private
*/
_createFunction: {
default: null,
serializable: false
}
statics: {
OptimizationPolicy,
OptimizationPolicyThreshold: 3,
},
createNode: CC_EDITOR && function (cb) {
@@ -94,8 +159,20 @@ var Prefab = cc.Class({
},
_instantiate: function () {
var node;
var node, useJit = false;
if (cc.supportJit) {
if (this.optimizationPolicy === OptimizationPolicy.SINGLE_INSTANCE) {
useJit = false;
}
else if (this.optimizationPolicy === OptimizationPolicy.MULTI_INSTANCE) {
useJit = true;
}
else {
// auto
useJit = (this._instantiatedTimes + 1) >= Prefab.OptimizationPolicyThreshold;
}
}
if (useJit) {
// instantiate node
node = this._doInstantiate();
// initialize node
@@ -107,7 +184,8 @@ var Prefab = cc.Class({
// instantiate node
node = this.data._instantiate();
}
++this._instantiatedTimes;
// link prefab in editor
if (CC_EDITOR || CC_TEST) {
// This operation is not necessary, but some old prefab asset may not contain complete data.
+2
View File
@@ -864,6 +864,7 @@ function declareProperties (cls, className, properties, baseClass, mixins, es6)
* @return {Function} - the created class
*
* @example
// define base class
var Node = cc.Class();
@@ -871,6 +872,7 @@ function declareProperties (cls, className, properties, baseClass, mixins, es6)
var Sprite = cc.Class({
name: 'Sprite',
extends: Node,
ctor: function () {
this.url = "";
this.id = 0;