Prefab Class
Extends Asset
Module: cc
Class for prefab handling.
Index
Properties
_createFunctionFunctionCache function to optimize instance creaton.dataNodethe main cc.Node in the prefaboptimizationPolicyPrefab.OptimizationPolicyIndicates the optimization policy for instantiating this prefab.asyncLoadAssetsBooleanIndicates the raw assets of this prefab can be load after prefab loaded.readonlyBooleanloadedBooleanWhether the asset is loaded or noturlStringPoints to the true url of this asset's native object, only valid when asset is loaded and asyncLoadAsset is not enabled.nativeUrlStringReturns the url of this asset's native object, if none it will returns an empty string._nativeStringSerializable url for native asset._nativeAssetObjectThe underlying native asset of this asset if one is available._uuidString_nameString_objFlagsNumbernameStringThe name of the object.isValidBooleanIndicates whether the object is not yet destroyed.
Methods
compileCreateFunctionDynamically translation prefab data into minimized code....toStringReturns the asset's url.serialize应 AssetDB 要求提供这个方法createNodeCreate a new node using this asset in the scene...._setRawAssetSet native file name for this asset.destroyActual object destruction will delayed until before rendering._destructClear all references in the instance._onPreDestroyCalled before the object being destroyed._serializeThe customized serialization for this object._deserializeInit this object from the custom serialized data.
Details
Properties
_createFunction
Cache function to optimize instance creaton.
| meta | description |
|---|---|
| Type | Function |
| Defined in | cocos2d/core/assets/CCPrefab.js:85 |
data
the main cc.Node in the prefab
| meta | description |
|---|---|
| Type | Node |
| Defined in | cocos2d/core/assets/CCPrefab.js:96 |
optimizationPolicy
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.
| meta | description |
|---|---|
| Type | Prefab.OptimizationPolicy |
| Defined in | cocos2d/core/assets/CCPrefab.js:101 |
| Available since | 1.10.0 |
Examples
prefab.optimizationPolicy = cc.Prefab.OptimizationPolicy.MULTI_INSTANCE;
asyncLoadAssets
Indicates the raw assets of this prefab can be load after prefab loaded.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/assets/CCPrefab.js:116 |
readonly
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/assets/CCPrefab.js:124 |
loaded
Whether the asset is loaded or not
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/assets/CCAsset.js:57 |
url
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.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCAsset.js:68 |
nativeUrl
Returns the url of this asset's native object, if none it will returns an empty string.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCAsset.js:85 |
_native
Serializable url for native asset.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCAsset.js:123 |
_nativeAsset
The underlying native asset of this asset if one is available. This property can be used to access additional details or functionality releated to the asset. This property will be initialized by the loader if
_nativeis available.
| meta | description |
|---|---|
| Type | Object |
| Defined in | cocos2d/core/assets/CCAsset.js:131 |
_uuid
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/assets/CCRawAsset.js:46 |
_name
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/platform/CCObject.js:76 |
_objFlags
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/platform/CCObject.js:83 |
name
The name of the object.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/platform/CCObject.js:240 |
Examples
obj.name = "New Obj";
isValid
Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
When an object'sdestroyis called, it is actually destroyed after the end of this frame. SoisValidwill return false from the next frame, whileisValidin the current frame will still be true. If you want to determine whether the current frame has calleddestroy, usecc.isValid(obj, true), but this is often caused by a particular logical requirements, which is not normally required.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/platform/CCObject.js:258 |
Examples
var node = new cc.Node();
cc.log(node.isValid); // true
node.destroy();
cc.log(node.isValid); // true, still valid in this frame
// after a frame...
cc.log(node.isValid); // false, destroyed in the end of last frame
Methods
compileCreateFunction
Dynamically translation prefab data into minimized code.
This method will be called automatically before the first time the prefab being instantiated,
but you can re-call to refresh the create function once you modified the original prefab data in script.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCPrefab.js:145 |
toString
Returns the asset's url.
The Asset object overrides the toString() method of the Object object.
For Asset objects, the toString() method returns a string representation of the object.
JavaScript calls the toString() method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation.
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/assets/CCAsset.js:184 |
serialize
应 AssetDB 要求提供这个方法
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/assets/CCAsset.js:198 |
createNode
Create a new node using this asset in the scene.
If this type of asset dont have its corresponding node type, this method should be null.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCAsset.js:209 |
Parameters
_setRawAsset
Set native file name for this asset.
| meta | description |
|---|---|
| Defined in | cocos2d/core/assets/CCAsset.js:224 |
Parameters
destroy
Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
From the next frame, this object is not usable any more.
You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/platform/CCObject.js:293 |
Examples
obj.destroy();
_destruct
Clear all references in the instance.
NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _destruct: function () { for (var key in this) { if (this.hasOwnProperty(key)) { switch (typeof this[key]) { case 'string': this[key] = ''; break; case 'object': case 'function': this[key] = null; break; } } }
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:427 |
_onPreDestroy
Called before the object being destroyed.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:460 |
_serialize
The customized serialization for this object. (Editor Only)
| meta | description |
|---|---|
| Returns | object |
| Defined in | cocos2d/core/platform/CCObject.js:485 |
Parameters
exportingBoolean
_deserialize
Init this object from the custom serialized data.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:495 |
Parameters
dataObject the serialized json datactx_Deserializer