一个 ImageButton 与渲染相关的行为类似于一个 ImageLabel ,加上一个 GuiButton 的额外行为。
概要
属性
当 ImageButton 被悬停时使用的纹理ID。
由 ImageButton 元素显示的图像内容。读写到 ImageContent 。
决定渲染图像将如何被着色。
用户界面元素显示的图像内容。支持资产 URI 和 EditableImage 对象。
要显示的图像子区的像素偏移。
确定要显示的图像子区域的像素大小。
决定渲染图像的透明度。
指示图像是否已从 Roblox 网站完全加载。
当按下 ImageButton 时将使用的纹理 ID。
选择按钮的图像重新样本模式。
决定图像在源图像与显示尺寸不同的 UI 元素中显示时会缩放的方式。
设置 9 切片图像的切割边界。
将 9 片边按指定比率缩放。
设置图像按钮的砖块比例。
决定是否当鼠标悬停或单击时,按钮会自动更改颜色。
当 被悬停时会播放的实例。
如果 true 在图形用户界面元素可见时,鼠标不会锁定,除非右键按钮向下。
一个当 HapticEffect 按下时会播放的实例,当 GuiButton 按下时。
一个指示对象是否已选择的 boolean 属性。
根据一列预定义风格设置 GuiButton 的风格。
决定这个 UI 元素是否沉没输入。
确定 GuiObject 的起始点,相对于其绝对尺寸。
决定是否基于子内容进行缩放。
决定 GuiObject 背景颜色。
决定 GuiObject 背景和边界的透明度。
决定 GuiObject 边框的颜色。
决定边界 GuiObject 是如何相对于其尺寸布置。
决定 GuiObject 边框的像素宽度。
决定是否描渲染父级 GUI 元素边界外的子元素 GuiObjects。
决定玩家的鼠标是否在 GuiObject 上被主动按下。
控制使用 GuiObject 与 UIGridStyleLayout 时的排序顺序。
设置当游戏手柄选择器向下移动时将选择的 GuiObject 。
设置当游戏手柄选择器移至左侧时将选择的 GuiObject 。
设置当游戏手柄选择器移至右侧时将选择的 GuiObject 。
设置当游戏手柄选择器向上移动时将选择的 GuiObject 。
确定 GuiObject 的像素和坐标位置。
决定旋转 GuiObject 的角度数。
确定是否可以由游戏手柄选择 GuiObject 。
覆盖用于游戏手柄的默认选择装饰。
游戏手柄 UI 选择选择的 GuiObjects 顺序。
决定 GuiObject 的像素和积分尺寸。
混合属性 BackgroundTransparency 和 TextTransparency 。
决定是否渲染 GuiObject 和其子孙。
决定 GuiObject 对其他人的相对渲染顺序。
方法
继承自GuiObject的方法- TweenPosition(endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
平滑地将图形用户界面移至新的 UDim2 。
- TweenSize(endSize : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
- TweenSizeAndPosition(endSize : UDim2,endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
平滑地将图形用户界面移至新的尺寸和位置。
活动
继承自GuiButton的活动当按钮激活时发火。
当用户的鼠标完全点击 GuiButton 时发生火焰。
当用户按下左键时,在 GuiButton 上按下左键时发生火焰。
当用户释放左鼠标按钮时,发生在 GuiButton 上。
当用户的鼠标完全右键单击 GuiButton 时发生火焰。
当用户按下鼠标右键向下在 GuiButton 上时,发生火焰。
当用户释放鼠标右键时,火焰从 GuiButton 上。
当用户开始通过人机界面设备(鼠标按钮向下、触摸开始、键盘按钮向下等)进行交互时,发射。
当用户通过人机界面设备(鼠标按钮向下、触摸开始、键盘按钮向下等)更改他们的互动方式时,发射。
当用户停止通过人机界面设备(鼠标按钮向下、触摸开始、键盘按钮向下等)进行交互时,发射。
当用户将鼠标移至图形用户界面元素时发生火焰。
当用户将鼠标移出 GUI 元素时发生火焰。
在用户移动鼠标时,无论它在哪个 GUI 元素内,都会发生火焰。
当用户滚动鼠标轮返回时,火焰当鼠标悬停在图形用户界面元素上。
当用户滚动鼠标轮时向前移动时,触发当鼠标悬停在图形用户界面元素上时。
在使用游戏手柄选择器时将 GuiObject 聚焦时发射。
当游戏手柄选择器停止关注 GuiObject 时发射。
当玩家开始、继续和停止长按 UI 元素时,发生火焰。
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
当玩家在 UI 元素上移动手指时发生火焰。
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
当玩家使用界面元素上的两个手指执行捏或拉动动作时,发生火焰。
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
当玩家使用界面元素上的两个手指执行旋转动作时,发生火焰。
当玩家在 UI 元素上执行滑动动作时发生火焰。
当玩家在 UI 元素上执行了点击动作时发生火焰。
- SelectionChanged(amISelected : boolean,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
当游戏手柄选择移动到、离开或在连接的 或任何子手柄中更改时,发生火焰。
属性
HoverImageContent
Image
该属性是一种内容类型属性,应该保存 Roblox 上上传的贴花或图像的资产 ID。它与 Roblox 中加载图像相关的功能与 Decal.Texture 相同。渲染的图像将使用 ImageColor3 进行颜色化。
请注意,通过调整 ScaleType 属性可以使图像渲染为瓷砖、缩放适合或 9 切割来实现。
代码示例
This code sample causes an ImageLabel/ImageButton to display a red padlock. When the mouse is hovered, it changes to a green unlocked padlock.
local imageLabel = script.Parent
-- The images in this example are 64x64
imageLabel.Size = UDim2.new(0, 64, 0, 64)
local function unlock()
imageLabel.Image = "rbxassetid://284402785" -- Unlocked padlock (64x64)
imageLabel.ImageColor3 = Color3.new(0, 0.5, 0) -- Dark green
end
local function lock()
imageLabel.Image = "rbxassetid://284402752" -- Locked padlock (64x64)
imageLabel.ImageColor3 = Color3.new(0.5, 0, 0) -- Dark red
end
-- Connect events; our default state is locked
imageLabel.MouseEnter:Connect(unlock)
imageLabel.MouseLeave:Connect(lock)
lock()
ImageColor3
该属性决定图像是如何被着色的。当设置为白色时,不会发生颜色化。该属性对于重复使用图像资产非常有用:如果源图像完全白色透明,您可以一次设置图像的整个颜色与此属性。
代码示例
This code sample causes an ImageLabel/ImageButton to display a red padlock. When the mouse is hovered, it changes to a green unlocked padlock.
local imageLabel = script.Parent
-- The images in this example are 64x64
imageLabel.Size = UDim2.new(0, 64, 0, 64)
local function unlock()
imageLabel.Image = "rbxassetid://284402785" -- Unlocked padlock (64x64)
imageLabel.ImageColor3 = Color3.new(0, 0.5, 0) -- Dark green
end
local function lock()
imageLabel.Image = "rbxassetid://284402752" -- Locked padlock (64x64)
imageLabel.ImageColor3 = Color3.new(0.5, 0, 0) -- Dark red
end
-- Connect events; our default state is locked
imageLabel.MouseEnter:Connect(unlock)
imageLabel.MouseLeave:Connect(lock)
lock()
ImageContent
该属性应持有 资产 URI 或对 EditableImage 对象的引用。资产 URI 可以引用到 Roblox 上传的图标或图像。它与加载图像相关的功能与 Decal.Texture 相同。
渲染的图像将使用 ImageColor3 进行颜色化。通过调整 ScaleType 属性,可以使图像渲染为瓷砖状、适应缩放或 9 切片来显示。
ImageRectOffset
该属性决定图像区域(从左上角)要显示的像素偏移量,可以与 ImageRectSize 一起显示图像的部分。
代码示例
This code sample uses ImageRectOffset/ImageRectSize in order to play an animation of a man throwing a punch
-- Place this in an ImageLabel/ImageButton with size 256x256
local imageLabel = script.Parent
-- The following image is 1024x1024 with 12 frames (256x256)
-- The frames play an animation of a man throwing a punch
imageLabel.Image = "rbxassetid://848623155"
imageLabel.ImageRectSize = Vector2.new(256, 256)
-- The order of the frames to be displayed (left-to-right, then top-to-bottom)
local frames = {
Vector2.new(0, 0),
Vector2.new(1, 0),
Vector2.new(2, 0),
Vector2.new(3, 0),
Vector2.new(0, 1),
Vector2.new(1, 1),
Vector2.new(2, 1),
Vector2.new(3, 1),
Vector2.new(0, 2),
Vector2.new(1, 2),
Vector2.new(2, 2),
Vector2.new(3, 2),
}
-- Animate the frames one at a time in a loop
while true do
for _, frame in ipairs(frames) do
imageLabel.ImageRectOffset = frame * imageLabel.ImageRectSize
task.wait(0.1)
end
end
ImageRectSize
该属性决定要显示的图像区域的像素大小,可以与 ImageRectOffset 一起部分显示图像。如果任何维度设置为 0,整个图像将显示而不是设置为 。
代码示例
This code sample uses ImageRectOffset/ImageRectSize in order to play an animation of a man throwing a punch
-- Place this in an ImageLabel/ImageButton with size 256x256
local imageLabel = script.Parent
-- The following image is 1024x1024 with 12 frames (256x256)
-- The frames play an animation of a man throwing a punch
imageLabel.Image = "rbxassetid://848623155"
imageLabel.ImageRectSize = Vector2.new(256, 256)
-- The order of the frames to be displayed (left-to-right, then top-to-bottom)
local frames = {
Vector2.new(0, 0),
Vector2.new(1, 0),
Vector2.new(2, 0),
Vector2.new(3, 0),
Vector2.new(0, 1),
Vector2.new(1, 1),
Vector2.new(2, 1),
Vector2.new(3, 1),
Vector2.new(0, 2),
Vector2.new(1, 2),
Vector2.new(2, 2),
Vector2.new(3, 2),
}
-- Animate the frames one at a time in a loop
while true do
for _, frame in ipairs(frames) do
imageLabel.ImageRectOffset = frame * imageLabel.ImageRectSize
task.wait(0.1)
end
end
ImageTransparency
该属性决定元素渲染图像的阿尔法。一个值 0 是完全不透明的,而另一个值 1 是完全透明的(隐形)。该属性的行为类似于 GuiObject.BackgroundTransparency 或 BasePart.Transparency 。
如果您通过设置 ImageTransparency 为 1 来禁用图像渲染,将导致一个简单的长方形,可用作按钮。然而,使用空白 TextButton 可能会更好。
IsLoaded
此属性表示 Image 属性是否已从 Roblox 完全加载。被审核人拒绝的图像永远不会加载。
代码示例
This code sample measures how long an ImageLabel or ImageButton takes to load an image. If the image was already loaded, this will be 0.
local imageLabel = script.Parent
local startTime = workspace.DistributedGameTime
-- Wait for the image to load
while not imageLabel.IsLoaded do
task.wait()
end
-- Measure and display how long it took to load
local deltaTime = workspace.DistributedGameTime - startTime
print(("Image loaded in %.3f seconds"):format(deltaTime))
PressedImageContent
ResampleMode
决定图像在缩放时的外观。默认情况下,图像会在显示时将纹理的粗糙度调整为纹理记忆中粗糙度大于或小于其尺寸的水平。相反,Enum.ResamplerMode.Pixelated 保留图像像素的锋利边缘。
ScaleType
该属性决定在 UI 元素的绝对尺寸与源图像的尺寸不同时,Image 属性以何种方式渲染时。
默认情况下,这个属性是 Enum.ScaleType.Stretch ,这将简单地伸展/压缩图像尺寸,使其完全适合 UI 元素的空间。因为透明像素在上传到 Roblox 时设置为黑色,透明图像应该应用阿尔法混合以避免缩放图像周围的黑色轮廓。
对于 Enum.ScaleType.Slice , 在缩放时,角仍然是源图像尺寸。图像的边缘将延伸到图像的宽度/高度。最后,图像中心将伸展以填充图像的中心区域。要了解更多关于 9 切片图像的信息,请参阅 UI 9 切片设计。
对于 Enum.ScaleType.Tile ,每个图像瓷砖的大小由 TileSize 属性决定。
SliceScale
将 9 片边按指定比率缩放。这意味着 9 片的边缘会像你上传了新版本的纹理扩展一样增长。默认为 1.0 .
作为 9 片边界的乘数,它对于重复使用一个圆角图像以多个半径有用。
还看到 ScaleType ,它决定如果图像在源图像与显示元素的大小不同的 UI 元素中显示,图像将以何种方式缩放。
TileSize
设置图像的开始位置左上角的 ImageButton 瓷砖大小。默认值 值是 ; 的缩放组件将根据 的大小缩放地砖,而偏移组件是在原始像素中。例如, 的比例意味着瓷砖的尺寸将是相应轴的一半。
此属性只有如果 ScaleType 属性设置为 Enum.ScaleType.Tile 才会激活。