ContextActionService

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ

ช่วยให้ประสบการณ์ผูกการป้อนข้อมู

บริบทและการกระทํา

คอนเท็กส์ เป็นเพียงเงื่อนไขในขณะที่ผู้เล่นสามารถประกอบด้วยการป

การกระทําเป็นเพ

ผูกการกระทําเชิญตําแหน่ง

มันดีกว่าที่จะใช้ ContextActionService ของ Class.UserInputService

ตรวจสอบการกระทําของขีดจํากัด

เพื่อดูรายการ

การป้อนข้อมูลโดยไม่ต้องใช้คีย์บอร์ด

บริการนี้มีประโยชน์อย่างมากสำหรับการสนับสนุนเกมพาดและการป้อนข้อมูลแตะ สำหรับการป

ตัวอย่างโค้ด

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

สรุป

วิธีการ

  • BindAction(actionName : string,functionToBind : function,createTouchButton : bool,inputTypes : Tuple):void

    ผูกการป้อนของผู้ใช้ไปยังการกระทําที่มีหน้าที่จัดการการกระทํา

  • BindActionAtPriority(actionName : string,functionToBind : function,createTouchButton : bool,priorityLevel : number,inputTypes : Tuple):void

    ทำงานเหมือน BindAction แต่ยังสามารถให้ได้ว่ามีความสำคัญสูงสุดสำหรับการจัดเก็บไว้ล่วงหน้าสำหรับประเภทการป้อนข้อมูลที่ซ้องกัน (สูงกว่าก่อน)

  • BindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodesForActivation : Tuple):void

    ผูก Enum.KeyCode ด้วย Enum.UserInputType เฉพาะเพื่อเรียกใช้ Tool.Activation และ 1> Class.ClickDetector1> เหตุการณ์

  • รับตารางข้อมูลเกี่ยวกับการดำเนินการทั้งหมด (ปุ่มคือชื่อที่ผ่านไปยัง BindAction มีค่าเป็นตารางจาก GetBoundActionInfo คีย์)

  • รับตารางข้อมูลเกี่ยวกับการกระทําที่เป็นเชื่อมโยง ให้ชื่อเดิมของมันผ่านไปยัง BindAction

  • กลับ Class.BackpackItem.TextureId ของ Class.Tool ปัจจุบัน 4> Class.Tool.Equipped|equipped4> โดย 7> Class.Player7>

  • SetDescription(actionName : string,description : string):void

    ให้ชื่อการกระทําที่เกี่ยวข้องด้วยปุ่มสัมผัสตั้งค่าคำอธิบายของการกระทํา

  • SetImage(actionName : string,image : string):void

    หาก actionName กุญแจมีการเชื่อมโยงการกระทํา แล้ว image จะตั้งเป็นรูปภาพของปุ่มสัมผัส

  • SetPosition(actionName : string,position : UDim2):void

    ให้ชื่อของการกระทําที่เกี่ยวข้องด้วยปุ่มสัมผัสตําแหน่งปุ่มใน ContextButtonFrame

  • SetTitle(actionName : string,title : string):void

    ให้ชื่อการกระทําที่เกี่ยวข้องด้วยปุ่มสัมผัสตั้งแต่ตัวอักษรที่ปรากฏบนปุ่ม

  • UnbindAction(actionName : string):void

    ถอดการผูกจากการใส่ชื่อของการใส่ชื่อ

  • UnbindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodeForActivation : Enum.KeyCode):void

    ปลดการเชื่อมโยง Enum.KeyCode ด้วย Enum.UserInputType ที่เฉพาะเจาะจงจากการเรียกใช้ Tool.Activation เมื่อผูกกับ 1> Class.Tool.Activation1> เมื่อใดก็ตาม

  • ลบระเบียบาลให้หมด ไม่มีชื่อการดำเนินการใดๆ จะเหลืออยู่ ปุ่มสัมผัสทั้งหมดจะถูกลบออก

  • GetButton(actionName : string):Instance
    ผลตอบแทน

    ดึง ImageButton ของ a bound เครื่องมือที่มีปุ่มป้อนแตะที่สร้างขึ้น

อีเวนต์

คุณสมบัติ

วิธีการ

BindAction

void

ผูกการกระทําให้กับผู้ใช้ที

ตัวอย่างโค้ดด้านล่างแสดงให้เห็นว่า Sound สามารถเป็น played ได้ในขณะที่ปุ่มกุญแจ ( H ) ปุ่มเกมแพดหรือปุ่มหน้าจอแตะได้กด


local ContextActionService = game:GetService("ContextActionService")
-- เสียงแตรรถ
local honkSound = Instance.new("Sound", workspace)
honkSound.Looped = true
honkSound.SoundId = "rbxassetid://9120386436"
local function handleAction(actionName, inputState, inputObject)
if actionName == "HonkHorn" then
if inputState == Enum.UserInputState.Begin then
honkSound:Play()
else
honkSound:Pause()
end
end
end
-- เมื่อผู้เล่นนั่งอยู่ในยานพาหนะ:
ContextActionService:BindAction("HonkHorn", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
-- เมื่อผู้เล่นออก:
ContextActionService:UnbindAction("HonkHorn")

ตัวแปรตัวประมวลผลการดำเนินการ

ตัวประมวลผลการดำเนินการเรียกใช้ด้วยพารามิเตอร์ต่อไปนี้:


<tr>
<td>1</td>
<td><code>สตริง</code></td>
<td>สตริงเดียวกันที่ถูกส่งมาให้แก่ BindAction เมื่อเก่า</td>
</tr>
<tr>
<td>2</td>
<td><code>เมนู.UserInputState</code></td>
<td>สถานะของการป้อนข้อมูล (เริ่มต้น เปลี่ยน จบหรือยกเลิก)\*</td>
</tr>
<tr>
<td>3</td>
<td><code>วัตถุป้อนข้อมูล</code></td>
<td>ตัวอักษรที่มีข้อมูลเกี่ยวกับการป้อนข้อมูล (มันแตกต่างกันไปขึ้นอยู่กับ UserInputType)</td>
</tr>
#ชนิดคำอธิบาย

※ นี้ช่วยให้คุณสามารถจัดการหลายการกระทําในครั้งเดียวหากจําเป็น *ยกเลิกจะส่งหากมีการป้อนข้อมูลอยู่ในขั้นตอนการประมวลผลหรือหากมีการผูกคุณสมบัติของการประมวลผล * Class.

สแต็คการผูกการกระทํา

การผ

แตะปุ่ม

นอกเหนือจากประเภทของการป้อนข้อมูล ตัวแปรที่

พารามิเตอร์

actionName: string

ตัวอักษรที่แทนที่การดำเนินการที่กำลังดำเนินอยู่ (เช่น "HonkHorn" หรือ "OpenDoor")

functionToBind: function

การจัดการการใช้งานแบบตัวอักษร เรียกด้วยพารามิเตอร์ต่อไปนี้เมื่อการเรียกใช้งานของตัวอักษรเกิดขึ้น: string (actionName), Enum.UserInputState และเป็นตัวอักษรการป้อนข้อมูล

createTouchButton: bool

ว่าปุ่ม GUI ควรสร้างสำหรับการดำเนินการบนอุปกรณ์ป้อนสัมผัส

inputTypes: Tuple

จํานวนเงินใด ๆ ของ Enum.KeyCode หรือ Enum.UserInputType ที่แทนที่การใส่ข้อมูลเพื่อผูกกับการกระทํา


ส่งค่ากลับ

void

ตัวอย่างโค้ด

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

This code sample uses ContextActionService to bind an action named "BoundAction" to a general action handler function on the F key. Place this in a LocalScript inside StarterPlayerScripts and press F to see the message "Handling action: BoundAction".

General Action Handler

local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Handling action: " .. actionName)
print(inputObj.UserInputType)
end
-- Since this function does not return anything, this handler will
-- "sink" the input and no other action handlers will be called after
-- this one.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)

This code sample demonstrates how BindAction acts like a stack. It binds two actions, FirstAction (Z, X, and C keys) and SecondAction (Z and X keys) to two action handling functions. The second one will pass on a certain input (the X key).

Both actions use the Z and X keys, however the second handler will pass input only if X is pressed. So, when X is pressed, the second handler is called and then the first. The first action is also bound to the C key, and can be triggered even though the other two inputs are "covered" by the second action.

Test this code out by pasting it into a LocalScript within StarterPlayerScripts, then pressing Z, X and C. Observe which action handlers are called with what actions.

Stacked Action Handlers

local ContextActionService = game:GetService("ContextActionService")
-- Define an action handler for FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler One: " .. actionName)
end
-- This action handler returns nil, so it is assumed that
-- it properly handles the action.
end
-- Binding the action FirstAction (it's on the bottom of the stack)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Define an action handler for SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler Two: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Returning nil implicitly Sinks inputs
return Enum.ContextActionResult.Sink
end
end
-- Binding SecondAction over the first action (since it bound more recently, it is on the top of the stack)
-- Note that SecondAction uses the same keys as
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority

void

BindActionAtPriority ทำงานเหมือน BindAction แต่ยังสามารถให้พิเศษได้ว่าจะมีความสำคัญได้รับการกำหนดไว้หรือไม่ หากมีหลายการกำหนดไว้ในลำดับเดียวกันก็จะเ

พารามิเตอร์

actionName: string

ตัวอักษรที่แทนที่การดำเนินการที่กำลังดำเนินอยู่ (เช่น "HonkHorn" หรือ "OpenDoor")

functionToBind: function

การจัดการการใช้งานแบบตัวอักษร เรียกด้วยพารามิเตอร์ต่อไปนี้เมื่อการเรียกใช้งานของตัวอักษรเกิดขึ้น: string (actionName), Enum.UserInputState และเป็นตัวอักษรการป้อนข้อมูล

createTouchButton: bool

ว่าปุ่ม GUI ควรสร้างสำหรับการดำเนินการบนอุปกรณ์ป้อนสัมผัส

priorityLevel: number

ระดับความสามารถที่ควรจะผูกกับการดำเนินการ (มากขึ้นจากด้านล่าง)

inputTypes: Tuple

ใด ๆ จำนวนของ Enum.KeyCode หรือ Enum.UserInputType แอ็คชัน


ส่งค่ากลับ

void

ตัวอย่างโค้ด

This code sample demonstrates how ContextActionService:BindActionAtPriority() can be used to bind actions out of order yet still have the same priority levels. Normally, BindAction() would operate on order (last bound action has highest priority), but priority levels override this. You can test this code by pasting it into a Script with RunContext = Client in ReplicatedStorage.

ContextActionService BindAction Priorities

local ContextActionService = game:GetService("ContextActionService")
local INPUT_KEY1 = Enum.KeyCode.Q
local INPUT_KEY2 = Enum.KeyCode.E
local function handleThrow(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
local function handlePunch(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
-- Without specifying priority, the most recently bound action is called first,
-- so pressing INPUT_KEY1 prints "Punch" and then sinks the input.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Here we bind both functions in the same order as above, but with explicitly swapped priorities.
-- That is, we give "Throw" a higher priority of 2 so it will be called first,
-- despite "Punch" still being bound more recently.
-- Pressing INPUT_KEY2 prints "Throw" and then sinks the input.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActivate

void

หมายเหตุว่า Enum.UserInputType ที่ระบุต้องเป็น Keyboard หรือ Gamepad1 ผ่าน 2>Gamepad82> ในอีกทางหนึ่งเพื่อให้เป็นประโยชน์

พารามิเตอร์

userInputTypeForActivation: Enum.UserInputType

ต้องเป็น Keyboard หรือ Gamepad1 ผ่าน Gamepad8

keyCodesForActivation: Tuple

ส่งค่ากลับ

void

GetAllBoundActionInfo

GetAllBindActioninfo ส่งตารางที่แสดงชื่อของการดำเนินการทั้งหมด (เดิมถูกส่งไปที่ BindAction เป็นตารางที่สร้างขึ้นโดย Class.ContextActionService:GetBindActionInfo()|


ส่งค่ากลับ

GetBoundActionInfo

GetBoundActionInfo กลับมาเป็นตารางที่มีข้อมูลต่อไปนี้ ซึ่งอธิบายการกระทําผูกที่มีชื่อให้ชื่อว่า เพื่อรับข้อมูลเดียวกันสําหรับทุกการกระทําในครั้งเดียวกันใช้ Class.ContextActionService:GetAllBindActionInfo()|GetAllBindActionInfo</


<tr>
<td><code>สแต็คคำสั่ง</code></td>
<td>จํานวน</td>
<td>
อธิบายดัชนีของการดำเนินการบนสแต็ค (เพิ่มขึ้น)
</td>
</tr>
<tr>
<td><code>ระดับความสามารถ優先級</code> \*</td>
<td>จํานวน</td>
<td>
อธิบายระดับ <code>Class.ContextActionService:BindActionAtPriority()|priority</code> แอ็คชัน
</td>
</tr>
<tr>
<td><code>สร้างปุ่มแสดงผลแห่งตัวเรา</code></td>
<td>บูล</td>
<td>
อธิบายว่าคุณสมบัติเมาส์ควรถูกสร้างบน <code>Class.UserInputService.TouchEnabled|TouchEnabled</code> อุปกรณ์
</td>
</tr>
<tr>
<td><code>ใส่ชนิดของการป้อน</code></td>
<td>ตาราง</td>
<td>
ประเภทการป้อนข้อมูลที่ผ่านไปยัง <code>Class.ContextActionService:BindAction()|BindAction</code> สำหรับการดำเนินการนี้จะเรียก
</td>
</tr>
<tr>
<td><code>คำอธิบาย</code> ※</td>
<td>สตริง</td>
<td>
คำอธิบายของแอคชั่นที่กำหนดโดย <code>Class.ContextActionService:SetDescription()|SetDescription</code>
</td>
</tr>
<tr>
<td><code>ชื่อเรื่อง</code> ※</td>
<td>สตริง</td>
<td>
ชื่อแอคชั่นที่กำหนดโดย <code>Class.ContextActionService:SetTitle()|SetTitle</code>
</td>
</tr>
<tr>
<td><code>รูปภาพ</code> ※</td>
<td>สตริง</td>
<td>
รูปภาพของปุ่มสัมผัสของการกระทําที่กําหนดโดย <code>Class.ContextActionService:SetImage() | SetImage</code>
</td>
</tr>
ชื่อชนิดคำอธิบาย

ระดับความสำคัญยังคงรวมอยู่แม้ว่า BindActionAtPriority จะไม่ได้ใช้งาน - โดยปกติจะเป็น 2000

※ ระบุว่าสาขานี้จะเป็น nil แอ็คชัน

พารามิเตอร์

actionName: string

ส่งค่ากลับ

GetCurrentLocalToolIcon

GetCurrentLocalToolIcon จะกลับมาที่ BackpackItem.TextureId ของ Tool ปัจจุบัน equipped โดย 1> Class.Player1> หรือ 4> nil4> ห


ส่งค่ากลับ

สตริงเนื้อหาจาก TextureId ของเครื่องมือหรือไม่มีหากสามารถพบได้

SetDescription

void

ตั้งค่าคำอธิบายจะตั้งค่าคำอธิบายของการกระทําที่ผูกโดย BindAction ในรายการการกระทําที่มีอยู่ นี่จะเป็นข้อความที่อธิบายการกระทําที่ให้ไว้

แม้ว่าชื่อนี้อาจชี้ถึงว่าวิธีนี้เกี่ยวข้องกับครอบครัวของฟังก์ชันที่ปรับแต่งปุ่มสัมผัสสำหรับการสร้างพวกเขา ( Class.ContextAction

พารามิเตอร์

actionName: string

ชื่อของการกระทํานั้นเดิมเปลี่ยนเป็น BindAction

description: string

คำอธิบายข้อความของการกระทํา เช่น "Honk the car's horn" หรือ "คลังไอเท็ม"


ส่งค่ากลับ

void

ตัวอย่างโค้ด

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetImage

void

วิธีนี้ตั้งรูปภาพที่แสดงบนปุ่มสัมผัสที่สร้างโดย BindAction() โดยเฉพาะ มันตั้ง

คุณสมบัตินี้เป็นส่วนหนึ่งของครอบครัวของวิธีการที่ปรับแต่งปุ่มสัมผัสของการกระทํา วิธีอื่น ๆ ในครอบครัวนี้รวมถึง SetPosition และ SetTitle

พารามิเตอร์

actionName: string

ชื่อของการกระทํานั้นเดิมเปลี่ยนเป็น BindAction

image: string

ตั้งค่า


ส่งค่ากลับ

void

ตัวอย่างโค้ด

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetPosition

void

วิธีนี้ตั้งตำแหน่งปุ่มสัมผัสที่สร้างโดย BindAction() โดยเฉพาะ มันตั้งค่า GuiObject.Position สมบัติของ

คุณสมบัตินี้เป็นส่วนหนึ่งของครอบครัวของวิธีการที่ปรับแต่งปุ่มสัมผัสของการกระทํา วิธีอื่น ๆ ในครอบครัวนี้รวมถึง SetImage และ SetTitle

พารามิเตอร์

actionName: string

ชื่อของการกระทํานั้นเดิมเปลี่ยนเป็น BindAction

position: UDim2

ตําแหน่งภายใน ContextButtonFrame


ส่งค่ากลับ

void

ตัวอย่างโค้ด

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetTitle

void

ตั้งชื่อจะตั้งข้อความที่แสดงบนปุ่มสัมผัสที่สร้างโดย BindAction โดยเฉพาะ นั่นค

คุณสมบัตินี้เป็นส่วนหนึ่งของครอบครัวของวิธีการที่ปรับแต่งปุ่มสัมผัสของการกระทํา วิธีอื่น ๆ ในครอบครัวนี้รวมถึง SetImage และ SetPosition

พารามิเตอร์

actionName: string

ชื่อของการกระทํานั้นเดิมเปลี่ยนเป็น BindAction

title: string

ข้อความที่แสดงบนปุ่ม


ส่งค่ากลับ

void

ตัวอย่างโค้ด

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

UnbindAction

void

UnbindAction จะยกเลิกการผูกของการกระทําโดยชื่อจากผู้ใช้เพื่อให้การจัดการของการกระทําจะไม่ถูกเรียกอีกต่อไป โปรดใช้งานฟั

ฟังก์ชันนี้ จะไม่ ทิ้งข้อผิดพลาดหากไม่มีการกระทําด้วยสตริงที่กําหนด โดยใช้ GetAllBoundActionInfo หรือแท็บ "การผูกการกระทํา" ของผู้ให้บ

พารามิเตอร์

actionName: string

ส่งค่ากลับ

void

ตัวอย่างโค้ด

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

UnbindActivate

void

ยกเลิกการเผื่อนใช้ยกเลิกการใช้งาน Enum.KeyCode (หรือ Class.HopperBin โดยใช้ Enum.UserInputType นี้ ฟังก์ชันนี้ทำงานในลักษ

พารามิเตอร์

userInputTypeForActivation: Enum.UserInputType

ปุ่มเดียวกัน UserInputType เดิมที่ส่งไปยัง BindActivate

keyCodeForActivation: Enum.KeyCode

รหัสลับเดียวกันที่ส่งไปยัง BindActivate เดิม

ค่าเริ่มต้น: "Unknown"

ส่งค่ากลับ

void

UnbindAllActions

void

ลบระเบิดทั้งหมด ไม่มีชื่อการกระทำที่เหลืออยู่ ปุ่มสัมผัสทั้งหมดจะถูกลบออก หากปุ่มถูกกดโดยมนุษย์โดยตรง จะไม่มีการรับประกันว่าจะได้รับการแก้ไข


ส่งค่ากลับ

void

GetButton

ผลตอบแทน

GetButton รีเทิร์น Class.ImageButton ที่สร้างขึ้นโดย Class.ContextActionService:BindAction หากมีปารามิเตอร์ที่สามเป็นจริงและอุปกรณ์เป็น ImageButton เท่านั้น พารามิเตอร์เดียวสำหร

หากไม่มีการกระทําดังกล่าวถูกผูกหรือหากไม่ได้สร้างปุ่มนี้จะกลับมาเป็น nil

พารามิเตอร์

actionName: string

ชื่อของการกระทํานั้นเดิมเปลี่ยนเป็น BindAction


ส่งค่ากลับ

ImageButton ที่สร้างขึ้นโดย BindAction

อีเวนต์

LocalToolEquipped

เกิดขึ้นเมื่อผู้เล่นปัจจุบันใส่ Tool

พารามิเตอร์

toolEquipped: Instance

LocalToolUnequipped

เกิดขึ้นเมื่อผู้เล่นปัจจุบันถอน Tool

พารามิเตอร์

toolUnequipped: Instance