ContextActionService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
エクスペリエンスがユーザーの入力をコンテキストのアクションまたは期間限定のアクションにバインドすることを許可します。例えば、プレイヤーが近くにいる間だけドアを開けることを許可する。コードでは、アクションは単にサービスによって使用される文字列 (アクションの名前) で、ユニークなアクショ操作を区別します。アクションストリングは、他のメンバー機能の BindAction と UnbindAction に提供されます。2つのアクションが同じ入力にバインドされている場合、最も最近バインドされたものが優先されます。最も最近のアクションがバインド解除されると、それ以前にバインドされたものが再びコントロールを取得します。このサービスはユーザーの入力に対応しているので、クライアント側でのみ使用できます LocalScripts。
コンテキストとアクション
A コンテキスト は、プレイヤーが何らかのアクション操作実行できる条件です。いくつかの例には、 を保持したり、車の中にいたり、ドアの近くに立ったりすることが含まれます。どんな場合でも、コンテキストが入力されたときに を呼び出し、コンテキストが残されたときに を呼び出すのは、あなたの責任です。
アクション は、そのコンテキストでプレイヤーが実行できるいくつかの入力です。そのようなアクションは、メニューを開くか閉じる、セカンダリツールアクションをトリガーするか、RemoteFunction:InvokeServer() を使用してサーバーにリクエストを送信することができます。アクションは、BindAction と UnbindAction の両方の最初のパラメータとして、ユニークな文字列によって識別されます。文字列は何でもあり得ますが、実行されているのは アクションであり、使用されている入力ではありません 。たとえば、「KeyH」をアクション名として使用しないでください - 代わりに「CarHorn」を使用してください。スクリプトの最上部に定義するアクションは、コードで少なくとも3つの異なる場所で使用するため、最適です。
アクションのコンテキストにバインドする
ほとんどの場合、ContextActionService の BindAction よりも UserInputService.InputBegan を使用する方が良いです。For UserInputService.InputBegan 、接続された機能は、プレイヤーが実行中のアクションのコンテキストにあるかどうかをチェックする必要があります。ほとんどの場合、コンテキストが入力/離れたときに機能を呼び出すよりも難しいです。たとえば、H キーが車の警笛音をトリガーしてプレイヤーがそこに座っている間、プレイヤーはチャットで「こんにちは」と入力したり、他の何かのために H キーを使用したりするかもしれません。別のものが H キーを使用しているかどうかを判断するのは難しくなります (チャットのように) - 車はプレイヤーが意図しなかったときに鳴るかもしれませんプレイヤーが車に入ったり出たときに BindAction と UnbindAction を使用するのではなく、ContextActionService がプレイヤーが車に入ったり出たときに H のキープレスがホンクアクションをトリガーするのは、最も最近にバインドされたアクションのみの場合を確認しま操作。他の何か(チャットのように)がコントロールを取った場合、それをチェックする必要はありません。
バウンドアクションの検査
アクションとバインド入力のリストを見るには、開発者コンソールの「アクションバインディング」タブ(ゲーム中の F9)を調べることができます。これは、Roblox コアスクリプトとデフォルトのカメラ/コントロールスクリプトにバインドされたものを含め、すべてのバインディングを表示します。これは、アクションが正しい時間にバインド/バインド解除されているか、または他のアクションがアクションから入力を盗んでいる場合のデバッグに便利です。たとえば、WASD をバインドしようとしている場合、デフォルトのキャラクター移動スクリプトが同じキーにバインドされている可能性があります。同様に、カメラコントロールスクリプトは、スクリプトがあなたの後に実行されると、右クリック入力を盗むことができます。
キーボードレス入力
このサービスは、ゲームパッドとタッチ入力のサポートに特に役立ちます。ゲームパッド入力の場合、B ボタンを別のメニューに入るとユーザーを前のメニューに戻すアクションにバインドすることを選択する可能性があります。タッチの場合、画面上のタッチボタンはキープレスの代わりに使用できます:これらのボタンはアクションがバインドされている間のみ表示され、これらのボタンの位置、テキスト、および/または画像はこのサービスを通じて構成できます。このサービスが提供するカスタマイズ量はそれほど限られています;通常、ImageButton または TextButton を使用して画面上のボタンを独自に作成するのがより良いアイデアです。
コードサンプル
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.
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 : boolean,inputTypes : Tuple):()
ユーザーの入力をアクション処理機能にバインドして、アクションを実行する。
BindAction のように振る舞いますが、重複する入力タイプのバインドアクションに優先順位を割り当てることもできます (上から下へ)。
特定の Enum.KeyCode を使用して、Enum.UserInputType と Tool.Activation イベントをトリガーするために、ClickDetector をバインドします。
すべてのバインドアクション (キーは に渡された名前、値は で呼び出されたときのテーブル) に関する情報のテーブルを取得します (キーは で呼び出されるときの値です)。
バインドされたアクションの名前が最初にパスされた情報のテーブルを取得するBindAction。
現在 BackpackItem.TextureId に戻す Tool の equipped によって Player 。
タッチボタンでバインドされたアクションの名前を指定して、アクションの説明を設定します。
actionName キーにバインドアクションが含まれている場合、image はタッチボタンの操作像として設定されます。
タッチボタンでバインドされたアクションの名前を指定して、コンテキストボタンフレーム内のボタンの位置を設定します。
タッチボタンでバインドされたアクションの名前を指定すると、ボタンに表示されるテキストを設定します。
名前を指定されたアクションをバインド解除します。
- UnbindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodeForActivation : Enum.KeyCode):()
バインド中に Enum.KeyCode を特定の Enum.UserInputType で解除して、Tool.Activation をトリガーするときにバインドする ContextActionService:BindActivate() と一致させる。
バインドされたすべての機能を削除します。アクション名は残りません。すべてのタッチボタンが削除されます。
タッチ入力ボタンが作成された ImageButton アクションの bound を取得します。
イベント
プロパティ
方法
BindAction
アクションを処理機能に渡されたユーザー入力にバインドします。一致する入力が実行されると、以下の引数でアクション処理機能が呼び出されます。有効な入力の列挙アイテムには、フォロー中の内容が含まれます:Enum.KeyCode、Enum.UserInputType または Enum.PlayerActions 。プレイヤーが コンテキストにアクションを実行できる状態 に入ると、この関数を呼び出します。プレイヤーがコンテキストを離れると、同じ UnbindAction で actionName を呼び出します。CallFunction を使用して、アクションのアクション処理関数を手動で呼び出すことができます。
以下のコードサンプルでは、キー ( H )、ゲームパッドボタン、またはタッチスクリーンボタンを押している間に Sound がどのように played になるかを示しています。
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")
アクションハンドラーパラメータ
アクションハンドラーの機能は、次のパラメータで呼び出されます:
<th>種類</th><th>説明</th></tr><tr><td>1</td><td><code>文字列</code></td><td>元々 BindAction に渡された同じ文字列†</td></tr><tr><td>2</td><td><code>Enum.UserInput状態</code></td><td>入力の状態(開始、変更、終了またはキャンセル)*</td></tr><tr><td>3</td><td><code>入力オブジェクト</code></td><td>入力に関する情報を含むオブジェクト (UserInputType によって変わる)</td></tr>
# |
† 必要に応じて、1つの機能が複数のアクションを一度に処理できるようにします。*キャンセルは、一部の入力が進行中であり、他のアクションが進行中の入力にバインドされているか、進行中のバインドアクションが unbound である場合に送信されます。
アクションバインドスタック
アクションバインドはスタックのように動作します:2つのアクションが同じユーザー入力にバインドされている場合、 最も最近バインドされた アクションハンドラーが使用されます。アクションハンドラーが Enum.ContextActionResult.Pass を返すと、次に最も最近にバインドされたアクションハンドラーが呼び出され、その後、ハンドラーが入力をシンクするまで続きます (nil または Enum.ContextActionResult.Sink を返すなど)。When UnbindAction が呼ばれると、アクションハンドラーはスタックから削除されます。このスタック動作は、BindActionAtPriority でオーバーライドでき、createTouchButton の後の追加の優先度パラメータで、アクションのバインド順序が上書きされる可能性があります (上から下へ)。
タッチボタン
入力タイプに加えて、この関数の第 3 パラメータは、ボタンが TouchEnabled 装置用に作成されるかどうかを制御します。最初のタッチボタンの作作品に際して、 ContextActionGui という名前の が に追加されます。ScreenGui の内部には、Frame と呼ばれる「コンテキストボタンフレーム」が追加されます。バインドアクションが親になるのは、このフレームです; を使用して、カスタマイズのためにそのボタンを回収できます。
パラメータ
実行中のアクションを表す文字列 (例: "HonkHorn" または "OpenDoor")。
バインド入力がトリガーされたときに次のパラメータで呼び出されるアクション処理関数、string (actionName)、Enum.UserInputState および InputObject。
タッチ入力デバイスのアクションに対する GUI ボタンを作成するかどうか。
バインドするアクションに対する入力を表す Enum.KeyCode または Enum.UserInputType の数の任意。
戻り値
コードサンプル
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.
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".
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.
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
BindActionAtPriority は BindAction のように動作しますが、バインドアクションに優先順位が割り当てられることも許可します。複数のアクションが同じ入力にバインドされている場合、アクションのバインド順序に関係なく、優先度の高い機能が呼び出されます。言い換えれば、この機能は BindAction の通常の「スタック」操作作を上書きします。
パラメータ
実行中のアクションを表す文字列 (例: "HonkHorn" または "OpenDoor")。
バインド入力がトリガーされたときに次のパラメータで呼び出されるアクション処理関数、string (actionName)、Enum.UserInputState および InputObject。
タッチ入力デバイスのアクションに対する GUI ボタンを作成するかどうか。
アクションがバインドされる優先度レベル(低いものよりも高い)。
バインドするアクションに対する入力を表す Enum.KeyCode または Enum.UserInputType の数字の任意の数。
戻り値
コードサンプル
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.
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
バインド a Enum.KeyCode は、Enum.UserInputType で使用でき、ClickDetector イベント、Tools 、および GuiButtons を有効にすることができます。指定されたキー/ボタンが押されると、 マウスに送信されたイベントが発動します。これにより、Tool.Activated イベントが発動し、Tool.ManualActivationOnly が真に設定されていない場合があります。ゲームパッド入力の場合、この機能は ButtonR2 Enum.KeyCode をバインドするためにデフォルトの制御スクリプトによって呼び出されます。
指定された Enum.UserInputType は有効であるために Keyboard または Gamepad1 を介して Gamepad8 を通過する必要があることに注意してください。
パラメータ
ゲームパッド8を通じてキーボードまたはゲームパッド1である必要があります。
戻り値
GetAllBoundActionInfo
GetAllBoundActioninfo は、すべてのアクションの名前 (元々は に送信されたもの) をアクション名そのもので呼び出されたときに返されるテーブルにマップするテーブルを返します。この機能を使用すると、現在バインドされているすべてのアクションを調べることができます。優先度レベルやスタックオーダーをデバッグするときに便利です。
戻り値
GetBoundActionInfo
GetBoundActionInfo は、名前を指定されたバインドアクションを記述する次のキーを持つテーブルを返します。すべてのアクションについて一度に同じ情報を取得するには、GetAllBoundActionInfo を使用します。
<th>種類</th><th>説明</th></tr><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>createTouchButton</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 になることを示します。
パラメータ
戻り値
GetCurrentLocalToolIcon
GetCurrentLocalToolIcon は、BackpackItem.TextureId 現在 Tool に equipped によって戻り、または Player 、または nil がそのようなツールがない場合、またはプレイヤーが Character を欠いている場合。
戻り値
ツールのテクスチャIDからのコンテンツストリング、または nil が見つからない場合。
SetDescription
SetDescription は、BindAction にバインドされたアクションの説明を設定します。利用可能なアクションのリストでは、これは指定されたアクショ操作を説明するテキストになります。
名前が、このメソッドがタッチボタンのアクションを作成するための機能家族に関連していると示唆するかもしれませんが、このメソッドはそのようなボタンに影響しません。( SetTitle , SetImage 、 SetPosition )このメソッドは、アクションのテキストの説明を設定するだけで、それ以上はありません。
パラメータ
バインドアクションに最初に送信されたアクションの名前。
「車の警笛を鳴らす」または「インベントリを開く」など、アクションのテキスト説明。
戻り値
コードサンプル
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.
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
このメソッドは、BindAction() によって作成されたタッチボタンに表示される画像を設定します。特に、ImageLabel.Image プロパティを設定し、ImageLabel 内の ImageButton が GetButton によって返されることを指定します。そのようなバインドアクションが存在しない場合 (例:GetButton で何も返されないため、この関数は何もしないし、エラーをスローしません。
この機能は、アクションのタッチボタンをカスタマイズする一連のメソッドの一部です。この家族には、SetPosition と SetTitle が含まれます。
パラメータ
戻り値
コードサンプル
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.
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
このメソッドは、BindAction() によって作成されたタッチボタンの位置を設定します。特に、GuiObject.Position が返される ImageButton の GetButton のプロパティを設定します。そのようなバインドアクションが存在しない場合 (例:GetButton で何も返されないため、この関数は何もしないし、エラーをスローしません。
この機能は、アクションのタッチボタンをカスタマイズする一連のメソッドの一部です。この家族には、SetImage と SetTitle が含まれます。
パラメータ
戻り値
コードサンプル
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.
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
SetTitle は、BindAction によって作成されたタッチボタンに表示されるテキストを設定します。特に、これは 内の の の によって返される の プロパティを設定します。そのようなバインドアクションが存在しない場合 (例:GetButton で何も返されないため、この関数は何もしないし、エラーをスローしません。
この機能は、アクションのタッチボタンをカスタマイズする一連のメソッドの一部です。この家族には、SetImage と SetPosition が含まれます。
パラメータ
戻り値
コードサンプル
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.
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
UnbindAction は、ユーザーの入力から名前のアクションをバインド解除して、アクション処理機能がもはや呼ばれないようにします。この関数を呼び出すと、ユーザーインターフェイスを閉じたり、車を終了したり、 を終了したりするなど、アクションのコンテキストがもう適用可能用できない場合があります。バインドアクションの動作について詳しくは、BindAction を参照してください。
この機能 は、指定された文字列にバインドされたアクションがない場合、エラーを throwしません 。GetAllBoundActionInfo または開発者コンソールの「アクションバインド」タブを使用すると、現在バインドされているアクションを見つけることができます。
パラメータ
戻り値
コードサンプル
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.
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
UnbindActivate は、Enum.KeyCode を使用して、Enum.UserInputType で Tool (または HopperBin ) を有効にするために、BindActivate を使用してバインドを解除します。この機能は、基本的にその機能によって実行されたアクションを取り消します。
パラメータ
元の BindActivate に送信された同じユーザーインプットタイプ。
BindActivate に最初に送信された同じキーコード。
戻り値
UnbindAllActions
バインドされたすべての機能を削除します。アクション名は残りません。すべてのタッチボタンが削除されます。ボタンが手動で操作された場合、きれいにされる保証はありません。
戻り値
GetButton
GetButton は、3番目のパラメータが真であり、デバイスが ImageButton である場合、BindAction によって作成された TouchEnabled を返します。この関数にパラメータを渡す唯一の方法は、BindAction に最初に送信されたアクションの名前と完全に一致する必要があります。
そのようなアクションがバインドされていないか、ボタンが作成されていない場合、この関数は nil を返します。
パラメータ
バインドアクションに最初に送信されたアクションの名前。
戻り値
BindAction によって作成された画像ボタン。