类型 函数 库 graphics.* 返回值 TextureResource 修订 版本 2024.3703 关键词 纹理,性能优化,纹理内存,图像 另请参阅 graphics.releaseTextures() texture:releaseSelf() 纹理加载/管理 (指南) TextureResource
创建一个 TextureResource 对象,允许您访问和管理纹理。这可以用于
创建 TextureResource 对象后,您可以在
在使用 TextureResource 对象之前,您应该了解几个重要的细微差别 — 有关详细信息,请参阅下一节。
使用 graphics.newTexture()
创建的 TextureResource 对象将**不会**自动处置/释放,即使没有显示对象使用该纹理。这可能会导致内存泄漏,因此您应该手动处理此任务。单独的纹理资源对象可以通过 texture:releaseSelf() 处置/释放,或者在更广泛的范围内,可以使用 graphics.releaseTextures() 释放所有共享相同 type 属性的纹理对象。
TextureResource 对象具有 filename 和 baseDir 属性,可用于引用底层纹理。这些"background.png"
指定为纹理的 filename
,请勿尝试在 system.pathForFile() 中使用 texture.filename
作为对同一文件的引用。本质上,这些属性指的是内部内存,而不是文件系统。
如果您不需要维护对 TextureResource 对象的引用,建议您处置/释放它。如果有任何显示对象当前正在使用已释放的纹理,它们不会被破坏 — 相反,它们将在底层持有该纹理,并且如果/当它们被移除时,该纹理将被自动处置。
多次调用 graphics.newTexture()
并引用相同的文件将**不会**创建唯一的 TextureResource 对象。因此,如果您在任何纹理资源对象上调用 texture:releaseSelf(),它将被处置/从内存中移除,并且所有其他使用相同文件指向 graphics.newTexture()
的指针都将变为 nil
。
graphics.newTexture( params )
表。 包含 TextureResource 对象所需参数的表 — 有关详细信息,请参阅下一节。
字符串。 要创建的纹理的 类型。根据此值,其他
"image"
— 创建类型为 TextureResourceBitmap 的 TextureResource 对象。此类型的对象用于local imageTexture = graphics.newTexture( { type="image", filename="icon.png", baseDir=system.ResourceDirectory } )
"canvas"
— 创建类型为 TextureResourceCanvas 的 TextureResource 对象。此纹理资源是local canvasTexture = graphics.newTexture( { type="canvas", width=128, height=128 } )
"maskCanvas"
— 创建类型为 TextureResourceCanvas 的 TextureResource 对象。此纹理资源是
此类型专门用于遮罩。特别是,画布将是提供给 graphics.newMask 的图像,因此其尺寸必须遵守相同的约束。
当对象绘制到画布上时,将使用其“红色”结果,并忽略其余组件。红色值为 0 会生成黑色像素,值为 1 会生成白色像素。中间值会导致相应的灰色阴影。
(**陷阱**:这仅确认在 Windows 和 Android 上有效。其他平台可能仍需要小的修复。)
local maskCanvasTexture = graphics.newTexture( { type="maskCanvas", width=128, height=128 } )
字符串。 仅在 type
为 "image"
时适用。指示要加载的图像文件的名称,相对于 baseDir
(默认为 system.ResourceDirectory
)。
常量。 仅在 type
为 "image"
时适用。指定 filename
所在的基本目录。选项包括 system.ResourceDirectory
、system.DocumentsDirectory
、system.ApplicationSupportDirectory
、system.TemporaryDirectory
和 system.CachesDirectory
。默认为 system.ResourceDirectory
。
数字。 仅在 type
为 "canvas"
或 "maskCanvas"
时适用。指定可以在 TextureResourceCanvas 中渲染对象的宽度。
数字。 仅在 type
为 "canvas"
或 “maskCanvas”
时适用。指定可以在 TextureResourceCanvas 中渲染对象的高度。
数字。 仅在 type
为 "canvas"
或 "maskCanvas"
时适用。指定画布资源渲染到的纹理的水平像素尺寸。
数字。 仅在 type
为 "canvas"
或 "maskCanvas"
时适用。指定画布资源渲染到的纹理的垂直像素尺寸。
-- Create "TextureResource" object of type "TextureResourceBitmap" local backgroundTexture = graphics.newTexture( { type="image", filename="background.png" } ) -- Create display object with the pre-loaded texture local background = display.newImageRect( backgroundTexture.filename, -- "filename" property required backgroundTexture.baseDir, -- "baseDir" property required display.contentWidth, display.contentHeight ) background.x = display.contentCenterX background.y = display.contentCenterY -- If you no longer need the texture, release it to prevent memory leaks backgroundTexture:releaseSelf() -- Alternatively, release all texture objects of a specific type graphics.releaseTextures( { type="image" } )
local tex = graphics.newTexture( { type="canvas", width=128, height=128 } ) -- Create display object with texture as contents local rect = display.newImageRect( tex.filename, -- "filename" property required tex.baseDir, -- "baseDir" property required display.contentWidth, display.contentHeight ) rect.x = display.contentCenterX rect.y = display.contentCenterY -- Create a circle and draw/render it to the texture local circ = display.newCircle( 0, 0, 64 ) circ:setFillColor( { type="gradient", color1={0,0.2,1}, color2={0.8,0.8,0.8}, direction="down" } ) tex:draw( circ ) -- Schedule texture objects to be rendered to texture before next frame tex:invalidate()
local tex = graphics.newTexture{ type = "canvas", width = 128, height = 128 } local back = display.newRect(0, 0, 128, 128) back:setFillColor(1, 0, 0) local rect = display.newRect(0, 0, 32, 64) rect:setFillColor(0, 0, 1) tex:draw(back) tex:draw(rect) tex:invalidate() -- Create a renderable mask and draw an all-white rect into it. local mask = graphics.newTexture{ type = "maskCanvas", width = 512, height = 512 } local mask_back = display.newRect(0, 0, 512 - 6, 512 - 6) mask:draw(mask_back) -- Add some circles in their initial locations. We'll make these black and -- periodically draw them into the mask, above the white rect. local circles = {} for i = 1, 10 do circles[i] = display.newCircle(0, 0, math.random(10, 35)) circles[i]:setFillColor(0) circles[i].radius = math.random(i * 10, 170) circles[i].speed = math.random(1, 7) * .05 mask:draw(circles[i]) end -- Add a background beneath the masked parts. local background = display.newRect(250, 250, mask.width, mask.height) background.fill = { type = "image", filename = tex.filename, baseDir = tex.baseDir } background:setStrokeColor(0, 0, 1) background.alpha = .7 background.strokeWidth = 3 -- These three rects will be affected by the mask. local masked_group = display.newGroup() local r1 = display.newRect(masked_group, 250, 350, 150, 200) local r2 = display.newRect(masked_group, 450, 100, 200, 100) local r3 = display.newRect(masked_group, 150, 150, 100, 200) masked_group:setMask(graphics.newMask(mask.filename, mask.baseDir)) masked_group.maskX, masked_group.maskY = background.x, background.y local x0 = background.x -- Move the background back and forth... timer.performWithDelay(50, function(event) background.x = x0 + math.sin(event.time / 800) * 50 end, 0) -- ...and update the circle arcs along with it. timer.performWithDelay(150, function(event) for i, circ in ipairs(circles) do local angle = (event.time * circ.speed) * .0035 circ.x, circ.y = math.cos(angle) * circ.radius, math.sin(angle) * circ.radius end mask:invalidate("cache") end, 0)