Mouse

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建

鼠标 已被超越 by UserInputServiceContextActionService , 它们覆盖了更广的范围、更富有功能、并支持 0> 跨平台0> 模式。它们仍然支持因其广泛使用而被支持,但您应该强烈考虑使用这些替代方案。

鼠标 对象住有多种 API 对指针,主要是按钮和射光线投射。它可以通过 Player:GetMouse() 调用 Players.LocalPlayer 中的 1> Class.Tool.Equipped1> 访问。它还通过 4> Class.Tool.Equipped4> 事件传递。

  • 它最为明显的是 Icon 属性,该属性改变鼠标的外观。
  • 它不断地将屏幕鼠标位置投射到 3D 世界使用 TargetFilter 属性,存储射线投射结果在 Hit , Class.Mouse
  • Plugins 可以使用 Plugin:GetMouse() 来获取一个 PluginMouse ,它的行为相同。

-- 从一个本地脚本:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- 设置鼠标标志
mouse.Icon = "rbxasset://SystemCursors/Wait"

注意:

概要

属性

活动

属性

只读
未复制
读取并联

此属性表示 CFrame 鼠标在 3D 空间中的位置。注意 Mouse.TargetFilter 和它的子后代将被忽略。

开发人员可以获取命中位置如下所示:


local position = mouse.Hit.Position

打击通常由 Tools 用于向鼠标向第三人称射击武器。

开发人员正在寻找 BasePart 鼠标指向应该使用 Mouse.Target

Mouse.Hit 如何计算?

Hit CFrame 的位置由计算为鼠标内部 Ray (一个延伸版本的 Mouse.UnitRay ) 和 3D 空间中的对象 (例如零件) 之间的交叉点。

Hit CFrame 的方向与 Mouse.UnitRay 的方向相对应。


local unitRayDirection = mouse.UnitRay.Direction
local mouseHitDirection = mouse.Hit.lookVector
-- unitRayDirection ≈ mouseHitDirection
-- the vectors are approximately equal

注意,当计算 Hit CFrame 的方向时,不会使用 Class.Workspace.CurrentCamera 的滚动。

鼠标内部射线将延伸到 1,000 格。如果鼠标不指向 3D 空间中的对象 (例如指向天空时),此属性将与 Workspace.CurrentCamera 的距离为 1,000 格。

代码示例

The code in this sample, when placed inside a LocalScript within StarterPlayerScripts will draw a red laser beam between the character's head and Mouse.Hit at all times.

Note, this beam will pass directly through obstructions in third person as the Mouse's raycasting is done from the Workspace.CurrentCamera not the head.

Mouse.Hit Laser Beam

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
beam.FaceCamera = true
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
beam.Parent = workspace.Terrain
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain
local function onRenderStep()
local character = player.Character
if not character then
beam.Enabled = false
return
end
local head = character:FindFirstChild("Head")
if not head then
beam.Enabled = false
return
end
beam.Enabled = true
local origin = head.Position
local finish = mouse.Hit.Position
attachment0.Position = origin
attachment1.Position = finish
end
RunService.RenderStepped:Connect(onRenderStep)

The code below visualizes the difference between Mouse.Hit and Mouse.Origin. In order to do this, the code uses the Vector3 positions of the hit and origin CFrame values using .p.

The difference is that the origin is "where the mouse came from" (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).

This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.

Mouse Origin vs Mouse Hit vs CurrentCamera Position

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position
local function onButton1Down()
print("Mouse.Hit:", mouse.Hit.Position)
print("camPos:", camPos)
print("Mouse.Origin:", mouse.Origin.Position)
print("Magnitude:", (mouse.Origin.Position - camPos).Magnitude)
end
mouse.Button1Down:Connect(onButton1Down)

Icon

ContentId
读取并联

图标 是一个属性,它确定使用的图像作为指针。如果为空,默认箭头将被使用。当鼠标悬停在GuiButton上时,此属性暂时被忽略。

要完全隐藏鼠标,请不要使用透明图像 – 相反,将 Class.UserInputService.MouseIconEnabled 设置为 false。

在体验中获取/设置用户鼠标图标的方法,您应该使用 UserInputService.MouseIconMouse.Icon 将在新的 API 为插件设置鼠标指针后被淘汰。

设计一个鼠标

创建自己的鼠标指针时,以下指南可能会很有用:

  • 用户使用的图像尺寸会决定 cursor 的大小。
  • 图像的 中心 是发生鼠标输入的地方。
  • 默认鼠标图像是 64x64 像素,鼠标占用 17x24 像素的空间。

System Cursors for PluginMouse

使用 Plugin:GetMouse()MouseEnter 中获取时,您可


<tbody>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-Pointer.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/Arrow)</code></td>
<td>默认点击和选择。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-PointingHand.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/PointingHand</code></td>
<td>将鼠标悬停在一个有效链接/按钮上。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-OpenHand.png">
</img>
</td>
<td><code>rbxasset://系统曲鼠/打开手</code></td>
<td>将鼠标悬停在拖动物物品上。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-GrabbingHand.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/ClosedHand</code></td>
<td>拖动一个物品。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-IBeam.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/IBeam)</code></td>
<td>在文本字段上悬停。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeNS.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeNS</code></td>
<td>将鼠标悬停在垂直缩放手柄上。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeEW.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeEW</code></td>
<td>将鼠标悬停在横向调整手柄。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeNESW.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeNESW</code></td>
<td>将鼠标悬停在一个角落上移动处理手柄。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeNWSE.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeNWSE</code></td>
<td>将鼠标悬停在一个角落上移动处理手柄。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeAll.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeAll</code></td>
<td>将鼠标悬停在多向旋转手柄上。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeSplitV.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SplitNS</code></td>
<td>将鼠标悬停在垂直“分割”手柄。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-ResizeSplitH.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/SplitEW</code></td>
<td>将鼠标悬停在一个横向“分割”手柄上。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-Forbidden.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/Forbidden)</code></td>
<td>将鼠标悬停在锁定/禁用的物品目上。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-Wait.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/Wait</code></td>
<td>指示操作正在进行中。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-Busy.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/Busy</code></td>
<td>表明系统正忙。</td>
</tr>
<tr>
<td>
<img src="../../../assets/legacy/Mouse-Icon-Crosshair.png">
</img>
</td>
<td><code>rbxasset://SystemCursors/Cross</code></td>
<td>将鼠标悬停在一个选择区上。</td>
</tr>
</tbody>
看\*资产建议使用

* 这些外观是模拟的 - 实际外观取决于您的操作系统。

代码示例

This example changes the Players.LocalPlayer mouse icon to look like a dragon image.

Dragon Mouse Icon

local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
mouse.Icon = "http://www.roblox.com/asset?id=163023520"

Origin

只读
未复制
读取并联

Class.Mouse 属性是一个 CFrame 指示鼠标起始位置的。它位于 Workspace.CurrentCamera 和向鼠标 3D 位置方向。

Mouse.UnitRay 从原始位置开始,并且在同一方向上延伸。


local unitRay = mouse.UnitRay
local origin = mouse.Origin
-- 单位射线方向 = 起始 p
-- unitRay.Direction ≈ origin.lookVector

在 3D 空间中 Mouse 的位置,请参阅 Mouse.Hit

代码示例

The code below visualizes the difference between Mouse.Hit and Mouse.Origin. In order to do this, the code uses the Vector3 positions of the hit and origin CFrame values using .p.

The difference is that the origin is "where the mouse came from" (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).

This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.

Mouse Origin vs Mouse Hit vs CurrentCamera Position

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position
local function onButton1Down()
print("Mouse.Hit:", mouse.Hit.Position)
print("camPos:", camPos)
print("Mouse.Origin:", mouse.Origin.Position)
print("Magnitude:", (mouse.Origin.Position - camPos).Magnitude)
end
mouse.Button1Down:Connect(onButton1Down)

Target

只读
未复制
读取并联

在 3D 空间中的对象指向 mouse

注意:

  • 如果 Mouse.TargetFilter 已被设置,目标过滤器和其子将被忽略。
  • 当鼠标不指向BasePart,例如指向天空时,目标将为零。
  • 开发者在 3D 空间中寻找鼠标位置应该使用 Mouse.Hit

代码示例

The following code sample, when placed in StarterPlayerScripts will create a tool in the player's backpack that, once equipped, will change the BasePart.BrickColor of every BasePart the player clicks on.

Color Randomizer Tool

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.random()
end
end)
end)

TargetFilter

读取并联

这个属性决定一个对象在计算 Mouse.HitMouse.Target 时会忽略。对象的后裔也会被忽略,因此在设置此属性时可以忽略多个对象,只要它们是该属性设置的后裔。此属性对于过滤

这个属性可以设置为任何 Instance 或零,例如:


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.Model

注意, CharacterPlayers.LocalPlayer 被自动忽略。

TargetSurface

只读
未复制
读取并联

这个属性表示 Enum.NormalId 表面上的 BasePart ,鼠标指向的地面。此属性是由世界位置的鼠标 (Mouse.Hit ) 和指向的地面 (1> Class.Mouse.Target1>) 得到的。

当鼠标不指向零件时,此属性不具义义。例如,当鼠标指向天空时。在这些情况下,此属性设置为“右”。在使用此属性之前,请检查鼠标的目标是否为零。


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- 检查是否存在鼠标指向的部分
if mouse.Target then
print("The mouse is pointing to the " .. mouse.TargetSurface.Name .. " side of " .. mouse.Target.Name)
else
print("The mouse is not pointing at anything.")
end

代码示例

The code in this sample, when placed in a LocalScript inside StarterPlayerScripts will set the surface of any BasePart clicked on to a random surface.

Surface Randomizer

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local surfaceTypes = {
Enum.SurfaceType.Smooth,
Enum.SurfaceType.Glue,
Enum.SurfaceType.Weld,
Enum.SurfaceType.Studs,
Enum.SurfaceType.Inlet,
Enum.SurfaceType.Universal,
Enum.SurfaceType.Hinge,
Enum.SurfaceType.Motor,
}
local function onMouseClick()
-- make sure the mouse is pointing at a part
local target = mouse.Target
if not target then
return
end
local surfaceType = surfaceTypes[math.random(1, #surfaceTypes)]
local surface = mouse.TargetSurface
local propertyName = surface.Name .. "Surface"
mouse.Target[propertyName] = surfaceType
end
mouse.Button1Down:Connect(onMouseClick)

UnitRay

只读
未复制
读取并联

UnitRay 属性是一个向鼠标位置在 3D 空间中指向的 Ray 。它起源于 Mouse.Hit 的 8> Clas工作间.Camera.CFrame|CFrame 。 像所有单位射线一样,它有一个距离 1 的 1> Class.Workspace.CurrentCamera1> 。


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
print(mouse.UnitRay.Direction.magnitude) -- Always 1

ViewSizeX

只读
未复制
读取并联

ViewSizeX 属性描述游戏窗口的横向组件的大小,以游戏窗口的像素数量。

代码示例

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)

ViewSizeY

只读
未复制
读取并联

ViewSizeY 属性描述游戏窗口的大小的垂直组件,用于以像素计量。这个长度包括顶部栏的空间。

代码示例

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
只读
未复制
读取并联

当检测到屏幕上鼠标位置的变化时,您可以使用 ContextActionService:BindAction()Enum.UserInputType.MouseMovementUserInputService.InputChanged ,这两者都描述了

X 属性描述鼠标位置在屏幕上的横向组件。位置是以像素相对于顶部左角,在顶部栏下表示的。此属性可以与 Mouse.Y 使用,产生一个 Vector2 代表鼠标位置的 Datatype.Vector。


local position = Vector2.new(mouse.X, mouse.Y)

这个属性不会发射 Changed 或从 GetPropertyChangedSignal 返回的信号。 使用 Mouse.Move 事件。

代码示例

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
只读
未复制
读取并联

当检测到屏幕上鼠标位置的变化时,您可以使用 ContextActionService:BindAction()Enum.UserInputType.MouseMovementUserInputService.InputChanged ,这两者都描述了

鼠标位置在屏幕上的垂直组件描述在屏幕上的鼠标位置。位置是以像素相对于顶部左角,在顶部栏下计量的。此属性可以与 Mouse.X 使用,产生一个 Vector2 代表鼠标位置的 Class.Mouse.X。


local position = Vector2.new(mouse.X, mouse.Y)

这个属性不会发射 Changed 或从 GetPropertyChangedSignal 返回的信号。 使用 Mouse.Move 事件。

代码示例

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)

方法

活动

Button1Down

当玩家按下左键时,Button1Down 也会发生。

此外,您可以从一个 Tool 访问。例如,当放置在一个 LocalScript 中时,代码下面的按钮1向下打印,当左键点按时:


local Tool = script.Parent --确保这是一个工具对象
Tool.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)

开发人员可以使用 BasePartMouse.Hit 以及 Mouse.Target 属性,在世界空间中查找鼠标的位置。

有关获取鼠标对象的信息,请参阅Mouse页面。

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The following code sample, when placed in StarterPlayerScripts will create a tool in the player's backpack that, once equipped, will change the BasePart.BrickColor of every BasePart the player clicks on.

Color Randomizer Tool

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.random()
end
end)
end)

Button1Up

在左键点按释放时发射。

有关获得 Mouse 对象的信息,请参阅 Mouse 页面。

开发人员可以查看世界空间中的鼠标位置,并且使用 BasePartMouse.Hit 属性来指向任何 Mouse.Target

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The follow example changes the BasePart.BrickColor of every BasePart the player clicks and releases their mouse on.

In order for a part to change color, the player's mouse must be over the part both when the player presses their left mouse button down and when the player releases their left mouse button.

Color Randomizer Tool (Button1Up)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local target = nil
local function button1Down()
target = mouse.Target
end
local function button1Up()
if target == mouse.Target then
target.BrickColor = BrickColor.random()
end
end
mouse.Button1Down:Connect(button1Down)
mouse.Button1Up:Connect(button1Up)

Button2Down

按钮2下甚至会在玩家按下右键时触发。

此外,您可以从一个 Tool 访问。例如,当它放置在一个 LocalScript 中时,代码下面的 Button2Down 打印 Button2Down 每当右键点按时:


local Tool = script.Parent --确保这是一个工具对象
Tool.Equipped:Connect(function(Mouse)
Mouse.Button2Down:Connect(function()
print("Button2Down")
end)
end).

开发人员可以使用 BasePartMouse.Hit 以及 Mouse.Target 属性,在世界空间中查找鼠标的位置。

有关获取鼠标对象的信息,请参阅Mouse页面。

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The following code sample, when placed in StarterPlayerScripts will create a tool in the player's backpack that, once equipped, will change the BasePart.BrickColor of every BasePart the player clicks on.

Color Randomizer Tool

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
tool.Equipped:Connect(function(mouse)
mouse.Button2Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.random()
end
end)
end)

Button2Up

发生在右键点按释放时。


mouse.Button2Up:Connect(function()
print("button 2 up!")
end

有关获得 Mouse 对象的信息,请参阅 Mouse 页面。

开发人员可以查看世界空间中的鼠标位置,并且使用 BasePartMouse.Hit 属性来指向任何 Mouse.Target

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The follow example changes the BasePart.BrickColor of every BasePart the player clicks and releases their right mouse button on.

In order for a part to change color, the player's mouse must be over the part both when the player presses their right mouse button down and when the player releases their right mouse button.

Color Randomizer Tool (Button2Up)

local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
local target = nil
mouse.Button2Down:Connect(function()
target = mouse.Target
end)
mouse.Button2Up:Connect(function()
if target == mouse.Target then
target.BrickColor = BrickColor.random()
end
end)

Idle

每次心跳时,鼠标不会被传到另一个鼠标事件。

注意,此事件不应用于确定鼠标是否仍然。它每次心跳都会在 Mouse.Move 事件之间发射。

有关获得 Mouse 对象的信息,请参阅 Mouse 页面。

开发人员可以查看世界空间中的鼠标位置,并且使用 BasePartMouse.Hit 属性来指向任何 Mouse.Target

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

This example demonstrates how mouse events are passed during each frame

Mouse.Idle

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local events = {
"Button1Down",
"Button1Up",
"Button2Down",
"Button2Up",
"Idle",
"Move",
"WheelBackward",
"WheelForward",
"KeyDown",
"KeyUp",
}
local currentEvent
local frame = 0
local function processInput()
frame = frame + 1
print("Frame", frame, "- mouse event was passed to", currentEvent)
end
for _, event in pairs(events) do
mouse[event]:Connect(function()
currentEvent = event
end)
end
RunService:BindToRenderStep("ProcessInput", Enum.RenderPriority.Input.Value, processInput)

Move

移动鼠标时发射。

注意,该事件是在鼠标位置更新时触发的,因此在移动时会再次触发。

有关获得 Mouse 对象的信息,请参阅 Mouse 页面。

开发人员可以查看世界空间中的鼠标位置,并且使用 BasePartMouse.Hit 属性来指向任何 Mouse.Target


mouse.Move:Connect(function()
local position = mouse.Hit.p
local target = mouse.Target
print(target, position)
end)

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The example below allows the local player to move parts with their mouse.

When the player presses their left mouse button over a part, that part is the mouse's target and becomes the point. Until the player releases their left mouse button, that part will move to the mouse's world position when the player moves their mouse.

Note that the Mouse.TargetFilter property allows the code to ignore the part being moved when determining the mouse's world position.

The code should work as expected when placed in a LocalScript.

Move Parts with the Mouse

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local point
local down
local function selectPart()
if mouse.Target and not mouse.Target.Locked then
point = mouse.Target
mouse.TargetFilter = point
down = true
end
end
local function movePart()
if down and point then
local posX, posY, posZ = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
point.Position = Vector3.new(posX, posY, posZ)
end
end
local function deselectPart()
down = false
point = nil
mouse.TargetFilter = nil
end
mouse.Button1Down:Connect(selectPart)
mouse.Button1Up:Connect(deselectPart)
mouse.Move:Connect(movePart)

WheelBackward

轮向后退事件触发,当鼠标轮向后滚动。 可能的使用例子包括在第一人称FPS击游戏中切换枪械瞄准器或在第三人称视角下缩放玩家的相镜头。

这可以与滚动前进事件, Mouse.WheelForward 一起使用。

有关获得 Mouse 对象的信息,请参阅 Mouse 页面。

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The below example assumes that you have already got the player's mouse (and set it as a variable named 'mouse'), whether by use of a Tool, HopperBin or the Player:GetMouse() method. It will print "Wheel went backwards!" when the player scrolls backwards.

Mouse.WheelBackward

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onWheelBackward()
print("Wheel went backwards!")
end
mouse.WheelBackward:Connect(onWheelBackward)

WheelForward

当鼠标轮向前滚动时,轮前进事件触发。 可能的使用包括在第一人称FPS击游戏中切换枪械瞄准器或在第二人称视角下缩放玩家的相镜头。

这可以与滚动向后事件, Mouse.WheelBackward 一起使用。

有关获得 Mouse 对象的信息,请参阅 Mouse 页面。

注意,开发人员建议使用 UserInputService 而不是新工作中的 Mouse 对象。


代码示例

The below example assumes that you have already got the player's mouse (and set it as a variable named 'mouse'), whether by use of a Tool, HopperBin or the Player:GetMouse() method. It will print "Wheel went forward!" when the player scrolls forwards.

Mouse.WheelForward

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onWheelBackward()
print("Wheel went forward!")
end
mouse.WheelForward:Connect(onWheelBackward)