Texture2D 类型
继承于 Asset, EventTarget(mixin)
This class allows to easily create OpenGL or Canvas 2D textures from images or raw data.
索引
属性(properties)
genMipmapsBoolean是否为纹理设置生成 mipmaps。packableBoolean设置纹理是否允许参与合图。loadedBoolean贴图是否已经成功加载widthNumber贴图像素宽度heightNumber贴图像素高度urlStringcc.Asset.urlis deprecated, please use nativeUrl instead_uuidStringnativeUrlString返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。refCountNumber引用的数量_nativeString保存原生资源的 URL。_nativeAssetObject此资源依赖的底层原生资源(如果有的话)。_nameString_objFlagsNumbernameString该对象的名称。isValidBoolean表示该对象是否可用(被 destroy 后将不可用)。
方法
getImpl返回渲染器内部贴图对象updateUpdate texture options, not available in Canvas render mode....initWithElement用 HTML Image 或 Canvas 对象初始化贴图。initWithData使用一个存储在 ArrayBufferView 中的图像数据(raw data)初始化数据。getHtmlElementObj获取当前贴图对应的 HTML Image 或 Canvas 对象,只在 Web 平台下有效。destroy销毁该贴图,并立即释放它对应的显存。getPixelFormat获取纹理的像素格式。hasPremultipliedAlpha检查纹理在上传 GPU 时预乘选项是否开启。handleLoadedTexture贴图加载事件处理器。descriptioncc.Texture2D 描述。releaseTexture释放纹理,请使用 destroy 替代。setWrapMode设置纹理包装模式。setFilters设置纹理贴图缩小和放大过滤器算法选项。setFlipY设置贴图的纵向翻转选项。setPremultiplyAlpha设置贴图的预乘选项。hasEventListener检查事件目标对象是否有为特定类型的事件注册的回调。on注册事件目标的特定事件类型回调。off删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。once注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent通过事件对象派发事件clear销毁记录的事件toString返回资源的 URL。serialize应 AssetDB 要求提供这个方法。createNode使用该资源在场景中创建一个新节点。_setRawAsset为此资源设置原生文件名。addRef增加资源的引用decRef减少资源的引用并尝试进行自动释放。_destruct清除实例中的所有引用。_onPreDestroy在对象被销毁之前调用。_serialize为此对象定制序列化。_deserialize从自定义序列化数据初始化此对象。
事件
load当该资源加载成功后触发该事件
Details
属性(properties)
genMipmaps
是否为纹理设置生成 mipmaps。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:402 |
packable
设置纹理是否允许参与合图。 如果需要在自定义 Effect 中使用纹理 UV,需要禁止该选项。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:422 |
loaded
贴图是否已经成功加载
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:514 |
width
贴图像素宽度
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:523 |
height
贴图像素高度
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:532 |
url
cc.Asset.urlis deprecated, please use nativeUrl instead
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/asset-manager/deprecated.js:728 |
| 废弃(Deprecated) | cc.Asset.url is deprecated, please use cc.Asset.nativeUrl instead |
_uuid
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:57 |
nativeUrl
返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:81 |
refCount
引用的数量
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/assets/CCAsset.js:115 |
_native
保存原生资源的 URL。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:131 |
_nativeAsset
此资源依赖的底层原生资源(如果有的话)。 此属性可用于访问与资源相关的其他详细信息或功能。 如果
_native可用,则此属性将由加载器初始化。
| meta | description |
|---|---|
| 类型 | Object |
| 定义于 | cocos2d/core/assets/CCAsset.js:142 |
_name
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/platform/CCObject.js:76 |
_objFlags
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/platform/CCObject.js:83 |
name
该对象的名称。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/platform/CCObject.js:240 |
示例
obj.name = "New Obj";
isValid
表示该对象是否可用(被 destroy 后将不可用)。
当一个对象的destroy调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始isValid就会返回 false,而当前帧内isValid仍然会是 true。如果希望判断当前帧是否调用过destroy,请使用cc.isValid(obj, true),不过这往往是特殊的业务需求引起的,通常情况下不需要这样。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/platform/CCObject.js:258 |
示例
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
方法
getImpl
返回渲染器内部贴图对象
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:551 |
update
Update texture options, not available in Canvas render mode. image, format, premultiplyAlpha can not be updated in native.
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:571 |
参数列表
optionsObject
initWithElement
用 HTML Image 或 Canvas 对象初始化贴图。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:660 |
参数列表
elementHTMLImageElement | HTMLCanvasElement
示例
var img = new Image();
img.src = dataURL;
texture.initWithElement(img);
initWithData
使用一个存储在 ArrayBufferView 中的图像数据(raw data)初始化数据。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:692 |
参数列表
getHtmlElementObj
获取当前贴图对应的 HTML Image 或 Canvas 对象,只在 Web 平台下有效。
注意:
texture 默认参与动态合图,如果需要获取到正确的 Html 元素对象,需要先设置 texture.packable 为 false
| meta | description |
|---|---|
| 返回 | HTMLImageElement | HTMLCanvasElement |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:735 |
destroy
销毁该贴图,并立即释放它对应的显存。(继承自 cc.Object.destroy)
销毁后,该对象不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:750 |
getPixelFormat
获取纹理的像素格式。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:772 |
hasPremultipliedAlpha
检查纹理在上传 GPU 时预乘选项是否开启。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:784 |
handleLoadedTexture
贴图加载事件处理器。v2.0 之后你将不在需要手动执行这个函数,它会在贴图加载成功之后自动执行。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:799 |
参数列表
premultipliedBoolean
description
cc.Texture2D 描述。
| meta | description |
|---|---|
| 返回 | String |
| 定义于 | cocos2d/core/assets/CCTexture2D.js:856 |
releaseTexture
释放纹理,请使用 destroy 替代。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:867 |
| 废弃(Deprecated) | since v2.0 |
setWrapMode
设置纹理包装模式。 若纹理贴图尺寸是 NPOT(non power of 2),则只能使用 Texture2D.WrapMode.CLAMP_TO_EDGE。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:879 |
参数列表
wrapSTexture2D.WrapModewrapTTexture2D.WrapMode
setFilters
设置纹理贴图缩小和放大过滤器算法选项。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:897 |
参数列表
minFilterTexture2D.FiltermagFilterTexture2D.Filter
setFlipY
设置贴图的纵向翻转选项。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:913 |
参数列表
flipYBoolean
setPremultiplyAlpha
设置贴图的预乘选项。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCTexture2D.js:929 |
参数列表
premultiplyBoolean
hasEventListener
检查事件目标对象是否有为特定类型的事件注册的回调。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/event/event-target.js:69 |
参数列表
typeString The type of event.
on
注册事件目标的特定事件类型回调。这种类型的事件应该被 emit 触发。
| meta | description |
|---|---|
| 返回 | Function |
| 定义于 | cocos2d/core/event/event-target.js:77 |
参数列表
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
示例
eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:119 |
参数列表
typeString A string representing the event type being removed.callbackFunction The callback to remove.targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
示例
// register fire eventListener
var callback = eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
targetOff
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:163 |
参数列表
targetObject The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:182 |
参数列表
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
示例
eventTarget.once('fire', function () {
cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent
通过事件对象派发事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:208 |
参数列表
eventEvent
clear
销毁记录的事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:221 |
toString
返回资源的 URL。
Asset 对象将会重写 Object 对象的 toString() 方法。
对于 Asset 对象,toString() 方法返回该对象的字符串表示形式。
当资源要表示为文本值时或在字符串连接时引用时,JavaScript 会自动调用 toString() 方法。
| meta | description |
|---|---|
| 返回 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:212 |
serialize
应 AssetDB 要求提供这个方法。
| meta | description |
|---|---|
| 返回 | String |
| 定义于 | cocos2d/core/assets/CCAsset.js:232 |
createNode
使用该资源在场景中创建一个新节点。
如果这类资源没有相应的节点类型,该方法应该是空的。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCAsset.js:246 |
参数列表
_setRawAsset
为此资源设置原生文件名。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/assets/CCAsset.js:261 |
参数列表
addRef
增加资源的引用
| meta | description |
|---|---|
| 返回 | Asset |
| 定义于 | cocos2d/core/assets/CCAsset.js:283 |
decRef
减少资源的引用并尝试进行自动释放。
| meta | description |
|---|---|
| 返回 | Asset |
| 定义于 | cocos2d/core/assets/CCAsset.js:301 |
_destruct
清除实例中的所有引用。
注意:此方法不会清除在 CCObject 实例中定义的 getter 或 setter。如果需要,你可以重写 _destruct 方法。例如:
_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 |
|---|---|
| 定义于 | cocos2d/core/platform/CCObject.js:427 |
_onPreDestroy
在对象被销毁之前调用。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/CCObject.js:483 |
_serialize
为此对象定制序列化。
| meta | description |
|---|---|
| 返回 | object |
| 定义于 | cocos2d/core/platform/CCObject.js:511 |
参数列表
exportingBoolean
_deserialize
从自定义序列化数据初始化此对象。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/CCObject.js:524 |
参数列表
dataObject the serialized json datactx_Deserializer
事件
load Event
当该资源加载成功后触发该事件