【Roblox】ProximityPromptのカスタムUI(3)~ObjectText, ActionTextの表示~
1. はじめに
今回は、ProximityPromptのカスタムUI第三回です。
まだ前回までの記事を読んでいない方は、先にこちらをご覧ください。
前回までに、表示の切り替えや、ProximityPromptInputTypeへの対応などを行いましたが、今回はその続きとして、ObjectTextとActionTextの表示を実装します。
バージョン:0.672.0.6720706
2. テキスト用のFrameを追加する
まずは、ProximityPromptのObjectTextとActionTextを表示するためのFrameを追加します。
ObjectTextとは、ProximityPromptでアクセスする対象を表示するための欄で、ActionTextはその対象にどんなアクションを行うのかを表示するための欄です。

ProximityPrompt | Documentation - Roblox Creator Hubより引用
と言っても、使用方法が制限されているわけでもないので、仕組み的には単に二つのText表示欄が用意されている、というものですね。
それでは、InputFrameをコピーして、BaseFrameの子に追加してください。
名前は「TextFrame」としておきます。
子に入っているRoundFrame以下は不要なので削除してください。

次に、BaseFrameの子にUIListLayoutを追加してください。

UIListLayout
こちらの記事でも機能の一部をご紹介していますが、UIListLayoutとは、複数のGuiObjectを自動的に整列させる機能をもったオブジェクトです。
これを使って、InputFrameとTextFrameを並べたいと思います。
追加したUIListLayoutのFillDirectionをHorizontalにしてください。
同じ階層下のGuiObjectが、横方向に並べられるようになります。
次に、TextFrameのLayoutOrderを1に設定してください。
LayoutOrderは、UIListLayoutのような整列を行うオブジェクトの並び順を指定するプロパティで、デフォルトは0になっているので、1のTextFrameの優先順位は下がり、確実に右側に来るようになります。
以下の画像のようになっていればOKです。

ちなみに、今回は使いませんが、UIListLayoutのSortOrderをNameにすれば名前順でソートすることも可能です。
続いて、BaseFrameと同様にAutomaticSizeをXに、Size.Xは0に設定してください。
これは、中にTextを追加した時、Textがどのような幅になっても自動的に対応するためです。
これで、TextFrameの設定は完了です。
次に、TextFrameの子にTextLabelを追加してください。
名前は「ObjectText」としておきます。

まずはAutomaticSizeをXY、Sizeは全て0に設定し、Textの内容に応じた大きさになるようにします。
BackgroundTransparencyは1で透明にし、TextColor3はグレー(178, 178, 178)にします。
FontFaceはBuilder Sansに設定します。
Textは確認用に、そのまま「Object」と入れておきましょう。
さらに、今作成したObjectTextをコピーして、TextFrameの子に追加してください。
名前は「ActionText」に変更します。

TextColor3は白(255, 255, 255)にし、TextSizeを19に設定します。
さらに、FontFaceのWeightをMediumに設定し、文字を少し太くします。
Textはこちらも確認用に、ProximityPromptのデフォルトに合わせて「Interact」にしておきます。
そして、LayoutOrderを1に設定します。
最後に、UIListLayoutをTextFrameの子に追加します。

先ほどと同様、これで二つのTextの配置を調整します。
今度は縦に並べるので、FillDirectionはVerticalのままでOKです。
今回は、VerticalAlignmentをCenterに設定します。
これで、縦方向の配置が中央揃えになります。
以下の画像のようになっていればOKです。

さて、これでText領域は追加されましたが、全体の幅がAutomaticSizeによって丁度Textの端までになってしまいます。
右側にもう少し余白が欲しいので、それ用のFrameを追加します。
BaseFrameの子にFrameを追加してください。
名前は「PaddingFrame」としておきます。
今までと同様透明なFrameにしたら、LayoutOrderを2にしてください。
これでTextFrameのさらに右側に来るようになります。
そして、Size.X.Offsetは23、Size.Y.Scaleは1にしてください。

これで、右側に23の余白ができるわけです。
最終的に、以下の画像のようになっていればOKです。

ちなみに、この余白はDefaultのUIだとUIPaddingというオブジェクトによって作られているのですが、現状だとUIPaddingとAutomaticSizeが影響しあった時に不具合があり、意図しない挙動になってしまうことが多いので、今回はFrameを入れることで作成しています。
3. ProximityPromptの内容を反映する
次に、Scriptから各Textの内容をUIに反映します。
PromptUIControllerのCreateメソッドに、以下のコードを追加してください。
-- UIに対して動作を設定し、破棄関数を返します.
function promptUIController:Create(prompt: ProximityPrompt, promptUI: BillboardGui, inputType: Enum.ProximityPromptInputType)
-- 各Tweenを一斉実行するための配列.
local tweensForButtonHoldBegin = {}
local tweensForButtonHoldEnd = {}
local tweensForFadeOut = {}
local tweensForFadeIn = {}
local fadeDuration = 0.2
local tweenInfoFast = TweenInfo.new(fadeDuration)
-- BaseFrameの設定.
local baseFrame = promptUI:FindFirstChild("BaseFrame")::Frame
do
------- 省略 -------
end
+ -- TextFrameを取得.
+ local textFrame = baseFrame:FindFirstChild("TextFrame")::Frame
+ local paddingFrame = baseFrame:FindFirstChild("PaddingFrame")::Frame
+
+ -- ActionTextの設定.
+ local actionText = textFrame:FindFirstChild("ActionText")::TextLabel
+ do
+ -- Tweenの作成.
+ local fadeOut = TweenService:Create(actionText, tweenInfoFast, {TextTransparency = 1})
+ local fadeIn = TweenService:Create(actionText, tweenInfoFast, {TextTransparency = actionText.TextTransparency})
+
+ -- リストに追加.
+ table.insert(tweensForButtonHoldBegin, fadeOut)
+ table.insert(tweensForButtonHoldEnd, fadeIn)
+ table.insert(tweensForFadeOut, fadeOut)
+ table.insert(tweensForFadeIn, fadeIn)
+
+ -- 初期の透明度を1に.
+ actionText.TextTransparency = 1
+ end
+
+ -- ObjectTextの設定.
+ local objectText = textFrame:FindFirstChild("ObjectText")::TextLabel
+ do
+ -- Tweenの作成.
+ local fadeOut = TweenService:Create(objectText, tweenInfoFast, {TextTransparency = 1})
+ local fadeIn = TweenService:Create(objectText, tweenInfoFast, {TextTransparency = objectText.TextTransparency})
+
+ -- リストに追加.
+ table.insert(tweensForButtonHoldBegin, fadeOut)
+ table.insert(tweensForButtonHoldEnd, fadeIn)
+ table.insert(tweensForFadeOut, fadeOut)
+ table.insert(tweensForFadeIn, fadeIn)
+
+ -- 初期の透明度を1に.
+ objectText.TextTransparency = 1
+ end
+
-- RoundFrameのUIの表示と終了のTweenを作成しリストに追加.
local inputFrame = baseFrame:FindFirstChild("InputFrame")::Frame
do
------- 省略 -------
end
------- 省略 -------
-- ProximityPromptの情報を元にUIを更新する関数を定義.
local function updateUIFromPrompt()
+ local hasActionText = (prompt.ActionText ~= nil and prompt.ActionText ~= "")
+ local hasObjectText = (prompt.ObjectText ~= nil and prompt.ObjectText ~= "")
+ textFrame.Visible = hasActionText or hasObjectText
+ paddingFrame.Visible = textFrame.Visible
+
+ if textFrame.Visible then
+ -- ActionTextの設定.
+ actionText.Text = prompt.ActionText
+ actionText.AutoLocalize = prompt.AutoLocalize
+ actionText.RootLocalizationTable = prompt.RootLocalizationTable
+
+ -- ObjectTextの設定.
+ objectText.Visible = hasObjectText
+ if hasObjectText then
+ objectText.Text = prompt.ObjectText
+ objectText.AutoLocalize = prompt.AutoLocalize
+ objectText.RootLocalizationTable = prompt.RootLocalizationTable
+ end
+ end
+
-- AutomaticSizeが反映されるのはRenderStepped直前のため、次のRenderSteppedまで待機する.
RunService.RenderStepped:Wait()
-- Size.XはBaseFrameのXに合わせる.
promptUI.Size = UDim2.fromOffset(baseFrame.AbsoluteSize.X, promptUI.Size.Y.Offset)
-- Prompt.UIOffsetを反映.
-- ProximityPrompt.UIOffsetはピクセル単位の値だが、BillboardGui.SizeOffsetはBillboardGui自体の大きさを基準としたScaleの値なので、BillboardGuiのサイズで割ることで変換している.
promptUI.SizeOffset = Vector2.new(prompt.UIOffset.X / promptUI.Size.X.Offset, prompt.UIOffset.Y / promptUI.Size.Y.Offset)
end
-- Changedイベントに接続し、ProximityPromptプロパティが変更された際にUIを更新.
table.insert(connections, prompt.Changed:Connect(updateUIFromPrompt))
-- 一度実行しておき、UIを更新.
updateUIFromPrompt()
------- 省略 -------
end
Createメソッドの中で、各TextのFadeのTweenの設定を行う処理と、updateUIFromPrompt関数の中で表示の反映と切り替えを行う処理が追加されました。
前者は今まで通りFadeを設定しているだけですね。
後者は、まず二つのテキストが存在するかどうかをチェックし、どちらも無ければTextFrameとPaddingFrame自体の表示を消しています。
片方でも存在すれば、存在するものを表示しつつ、ProximityPromptのプロパティの反映を行っている形です。
AutoLocalizeとRootLocalizationTableは、Textの自動ローカライズに関する設定です。
ProximityPrompt側の設定を、TextLabelにそのまま反映しています。
さて、それではさっそく実行して確認してみます。

これで、ProximityPromptのObjectTextとActionTextが反映されるようになりました!
Textの長さに応じて、UIの長さも変化しています!
4. サンプルコード
今回作成した最終的なサンプルコードを掲載しておきます。
PromptUIDispatcher(前回から変更ありません)
--!strict
-- Service
local ProximityPromptService = game:GetService("ProximityPromptService")
local Players = game:GetService("Players")
-- Variable
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local Folder = game:GetService("ReplicatedStorage"):WaitForChild("PromptUIFolder")
-- プロンプト用のScreenGuiを取得する関数.
local function getScreenGui()
-- プロンプト用のScreenGui取得.
local screenGui = PlayerGui:FindFirstChild("ProximityPrompts")
-- 存在しなかったら作成.
if screenGui == nil then
screenGui = Instance.new("ScreenGui")
screenGui.Name = "ProximityPrompts"
screenGui.ResetOnSpawn = false
screenGui.Parent = PlayerGui
end
-- 取得ないし作成したScreenGuiを返す.
return screenGui
end
-- PromptのUIを作成し、破棄関数を返します.
local function createPrompt(prompt: ProximityPrompt, inputType: Enum.ProximityPromptInputType, gui: ScreenGui)
-- UIを複製.
local promptUI = Folder:WaitForChild("PromptUI"):Clone()
-- BillboardGuiのターゲットと親を設定.
promptUI.Adornee = prompt.Parent
promptUI.Parent = gui
-- PromptUIControllerをrequire
local promptController = require(Folder:WaitForChild("PromptUIController"))
-- 動作を設定して破棄関数を返す.
return promptController:Create(prompt, promptUI, inputType)
end
-- ProximityPromptService.PromptShownに、カスタムUIの表示を接続します.
local function onLoad()
-- ProximityPromptService.PromptShownに接続.
ProximityPromptService.PromptShown:Connect(function(prompt: ProximityPrompt, inputType: Enum.ProximityPromptInputType)
-- デフォルトだったら(カスタムじゃなかったら)処理しない.
if prompt.Style == Enum.ProximityPromptStyle.Default then
return
end
-- プロンプト用のScreenGuiを取得.
local gui = getScreenGui()
-- カスタムUI作成、戻り値で破棄関数が渡される.
local cleanupFunction = createPrompt(prompt, inputType, gui)
-- PromptHidden待ち.
-- PromptHiddenはProximityPromptから離れた時等だけではなく、InputTypeが切り替わった時にも発火する.
prompt.PromptHidden:Wait()
-- 破棄関数を実行.
cleanupFunction()
end)
end
-- 実行.
onLoad()
PromptUIController
--!strict
-- PromptUIの動作を設定するModule
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Folder = script.Parent
local ButtonImageProvider = require(Folder:WaitForChild("ButtonImageProvider"))
local promptUIController = {}
-- Tweenの配列を全てPlayする関数.
local function tweenPlayAll(tweens: {Tween})
for _, v in ipairs(tweens) do
v:Play()
end
end
-- UIに対して動作を設定し、破棄関数を返します.
function promptUIController:Create(prompt: ProximityPrompt, promptUI: BillboardGui, inputType: Enum.ProximityPromptInputType)
-- 各Tweenを一斉実行するための配列.
local tweensForButtonHoldBegin = {}
local tweensForButtonHoldEnd = {}
local tweensForFadeOut = {}
local tweensForFadeIn = {}
local fadeDuration = 0.2
local tweenInfoFast = TweenInfo.new(fadeDuration)
-- BaseFrameの設定.
local baseFrame = promptUI:FindFirstChild("BaseFrame")::Frame
do
-- Tweenの作成.
local fadeOut = TweenService:Create(baseFrame, tweenInfoFast, {BackgroundTransparency = 1})
local fadeIn = TweenService:Create(baseFrame, tweenInfoFast, {BackgroundTransparency = baseFrame.BackgroundTransparency})
-- リストに追加.
table.insert(tweensForButtonHoldBegin, fadeOut)
table.insert(tweensForButtonHoldEnd, fadeIn)
table.insert(tweensForFadeOut, fadeOut)
table.insert(tweensForFadeIn, fadeIn)
-- 初期の透明度を1に.
baseFrame.BackgroundTransparency = 1
end
-- TextFrameを取得.
local textFrame = baseFrame:FindFirstChild("TextFrame")::Frame
local paddingFrame = baseFrame:FindFirstChild("PaddingFrame")::Frame
-- ActionTextの設定.
local actionText = textFrame:FindFirstChild("ActionText")::TextLabel
do
-- Tweenの作成.
local fadeOut = TweenService:Create(actionText, tweenInfoFast, {TextTransparency = 1})
local fadeIn = TweenService:Create(actionText, tweenInfoFast, {TextTransparency = actionText.TextTransparency})
-- リストに追加.
table.insert(tweensForButtonHoldBegin, fadeOut)
table.insert(tweensForButtonHoldEnd, fadeIn)
table.insert(tweensForFadeOut, fadeOut)
table.insert(tweensForFadeIn, fadeIn)
-- 初期の透明度を1に.
actionText.TextTransparency = 1
end
-- ObjectTextの設定.
local objectText = textFrame:FindFirstChild("ObjectText")::TextLabel
do
-- Tweenの作成.
local fadeOut = TweenService:Create(objectText, tweenInfoFast, {TextTransparency = 1})
local fadeIn = TweenService:Create(objectText, tweenInfoFast, {TextTransparency = objectText.TextTransparency})
-- リストに追加.
table.insert(tweensForButtonHoldBegin, fadeOut)
table.insert(tweensForButtonHoldEnd, fadeIn)
table.insert(tweensForFadeOut, fadeOut)
table.insert(tweensForFadeIn, fadeIn)
-- 初期の透明度を1に.
objectText.TextTransparency = 1
end
-- RoundFrameのUIの表示と終了のTweenを作成しリストに追加.
local inputFrame = baseFrame:FindFirstChild("InputFrame")::Frame
do
local roundFrame = inputFrame:FindFirstChild("RoundFrame")::Frame
table.insert(tweensForFadeOut, TweenService:Create(roundFrame, tweenInfoFast, { BackgroundTransparency = 1 }))
table.insert(tweensForFadeIn, TweenService:Create(roundFrame, tweenInfoFast, { BackgroundTransparency = roundFrame.BackgroundTransparency }))
-- 初期の透明度を1に.
roundFrame.BackgroundTransparency = 1
end
-- ボタンアイコンの生成.
do
-- ボタンアイコンとそのTweenを取得.
local buttonTable = ButtonImageProvider:GetButtonImage(prompt, inputType, tweenInfoFast)
if buttonTable then
-- ボタンアイコンの親をInputFrameに.
buttonTable.ButtonImage.Parent = inputFrame
-- FadeのTweenを配列に追加.
table.move(buttonTable.TweensForFadeIn, 1, #buttonTable.TweensForFadeIn, #tweensForFadeIn + 1, tweensForFadeIn)
table.move(buttonTable.TweensForFadeOut, 1, #buttonTable.TweensForFadeOut, #tweensForFadeOut + 1, tweensForFadeOut)
end
end
-- InputTypeがタッチの場合か、ProximityPromptがクリック可能設定である場合、入力受付用のボタンを設定する.
if inputType == Enum.ProximityPromptInputType.Touch or prompt.ClickablePrompt then
-- TextButtonを複製.
local button = script:WaitForChild("TextButton"):Clone()::TextButton
button.Parent = promptUI
local buttonDown = false -- 入力状態を示すフラグ.
-- 入力時の処理.
button.InputBegan:Connect(function(input: InputObject)
if (input.UserInputType == Enum.UserInputType.Touch
or input.UserInputType == Enum.UserInputType.MouseButton1)
and input.UserInputState ~= Enum.UserInputState.Change
then
-- 入力の開始をProximityPromptに伝える.
prompt:InputHoldBegin()
buttonDown = true
end
end)
-- 入力終了時の処理.
button.InputEnded:Connect(function(input: InputObject)
if input.UserInputType == Enum.UserInputType.Touch
or input.UserInputType == Enum.UserInputType.MouseButton1
then
if buttonDown then
-- 入力の終了をProximityPromptに伝える.
buttonDown = false
prompt:InputHoldEnd()
end
end
end)
-- BillboardGuiが入力を受付けるようにActiveをtrueに.
promptUI.Active = true
end
-- イベントを接続.
local connections = {}
-- Holdが必要なProximityPromptの場合.
if prompt.HoldDuration > 0 then
table.insert(connections, prompt.PromptButtonHoldBegan:Connect(function(playerWhoTriggered: Player)
tweenPlayAll(tweensForButtonHoldBegin)
end))
table.insert(connections, prompt.PromptButtonHoldEnded:Connect(function(playerWhoTriggered: Player)
tweenPlayAll(tweensForButtonHoldEnd)
end))
end
table.insert(connections, prompt.Triggered:Connect(function(playerWhoTriggered: Player)
tweenPlayAll(tweensForFadeOut)
end))
table.insert(connections, prompt.TriggerEnded:Connect(function(playerWhoTriggered: Player)
tweenPlayAll(tweensForFadeIn)
end))
-- ProximityPromptの情報を元にUIを更新する関数を定義.
local function updateUIFromPrompt()
local hasActionText = (prompt.ActionText ~= nil and prompt.ActionText ~= "")
local hasObjectText = (prompt.ObjectText ~= nil and prompt.ObjectText ~= "")
textFrame.Visible = hasActionText or hasObjectText
paddingFrame.Visible = textFrame.Visible
if textFrame.Visible then
-- ActionTextの設定.
actionText.Text = prompt.ActionText
actionText.AutoLocalize = prompt.AutoLocalize
actionText.RootLocalizationTable = prompt.RootLocalizationTable
-- ObjectTextの設定.
objectText.Visible = hasObjectText
if hasObjectText then
objectText.Text = prompt.ObjectText
objectText.AutoLocalize = prompt.AutoLocalize
objectText.RootLocalizationTable = prompt.RootLocalizationTable
end
end
-- AutomaticSizeが反映されるのはRenderStepped直前のため、次のRenderSteppedまで待機する.
RunService.RenderStepped:Wait()
-- Size.XはBaseFrameのXに合わせる.
promptUI.Size = UDim2.fromOffset(baseFrame.AbsoluteSize.X, promptUI.Size.Y.Offset)
-- Prompt.UIOffsetを反映.
-- ProximityPrompt.UIOffsetはピクセル単位の値だが、BillboardGui.SizeOffsetはBillboardGui自体の大きさを基準としたScaleの値なので、BillboardGuiのサイズで割ることで変換している.
promptUI.SizeOffset = Vector2.new(prompt.UIOffset.X / promptUI.Size.X.Offset, prompt.UIOffset.Y / promptUI.Size.Y.Offset)
end
-- Changedイベントに接続し、ProximityPromptプロパティが変更された際にUIを更新.
table.insert(connections, prompt.Changed:Connect(updateUIFromPrompt))
-- 一度実行しておき、UIを更新.
updateUIFromPrompt()
-- 開幕のFadeInを実行.
tweenPlayAll(tweensForFadeIn)
-- 後始末を行う関数を定義.
local function cleanup()
-- イベント接続を破棄.
for _, v in ipairs(connections) do
v:Disconnect()
end
-- FadeOut.
tweenPlayAll(tweensForFadeOut)
task.wait(fadeDuration)
-- 破棄.
promptUI:Destroy()
end
-- 破棄関数を返す.
return cleanup
end
return promptUIController
ButtonImageProvider(前回から変更ありません)
--!strict
-- ProximityPromptとProximityPromptInputTypeの情報から、適切なボタンアイコンを複製して、
-- FadeのTweenと一緒に返すメソッドを持つモジュールです.
-- Service
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
-- ボタンアイコン画像.
local GamepadButtonImage = {
[Enum.KeyCode.ButtonX] = "rbxasset://textures/ui/Controls/xboxX.png",
[Enum.KeyCode.ButtonY] = "rbxasset://textures/ui/Controls/xboxY.png",
[Enum.KeyCode.ButtonA] = "rbxasset://textures/ui/Controls/xboxA.png",
[Enum.KeyCode.ButtonB] = "rbxasset://textures/ui/Controls/xboxB.png",
[Enum.KeyCode.DPadLeft] = "rbxasset://textures/ui/Controls/dpadLeft.png",
[Enum.KeyCode.DPadRight] = "rbxasset://textures/ui/Controls/dpadRight.png",
[Enum.KeyCode.DPadUp] = "rbxasset://textures/ui/Controls/dpadUp.png",
[Enum.KeyCode.DPadDown] = "rbxasset://textures/ui/Controls/dpadDown.png",
[Enum.KeyCode.ButtonSelect] = "rbxasset://textures/ui/Controls/xboxView.png",
[Enum.KeyCode.ButtonStart] = "rbxasset://textures/ui/Controls/xboxmenu.png",
[Enum.KeyCode.ButtonL1] = "rbxasset://textures/ui/Controls/xboxLB.png",
[Enum.KeyCode.ButtonR1] = "rbxasset://textures/ui/Controls/xboxRB.png",
[Enum.KeyCode.ButtonL2] = "rbxasset://textures/ui/Controls/xboxLT.png",
[Enum.KeyCode.ButtonR2] = "rbxasset://textures/ui/Controls/xboxRT.png",
[Enum.KeyCode.ButtonL3] = "rbxasset://textures/ui/Controls/xboxLS.png",
[Enum.KeyCode.ButtonR3] = "rbxasset://textures/ui/Controls/xboxRS.png",
[Enum.KeyCode.Thumbstick1] = "rbxasset://textures/ui/Controls/xboxLSDirectional.png",
[Enum.KeyCode.Thumbstick2] = "rbxasset://textures/ui/Controls/xboxRSDirectional.png",
}
-- キーボードアイコン画像.
local KeyboardButtonImage = {
[Enum.KeyCode.Backspace] = "rbxasset://textures/ui/Controls/backspace.png",
[Enum.KeyCode.Return] = "rbxasset://textures/ui/Controls/return.png",
[Enum.KeyCode.LeftShift] = "rbxasset://textures/ui/Controls/shift.png",
[Enum.KeyCode.RightShift] = "rbxasset://textures/ui/Controls/shift.png",
[Enum.KeyCode.Tab] = "rbxasset://textures/ui/Controls/tab.png",
}
-- キーボード特殊文字アイコン画像.
local KeyboardButtonIconMapping = {
["'"] = "rbxasset://textures/ui/Controls/apostrophe.png",
[","] = "rbxasset://textures/ui/Controls/comma.png",
["`"] = "rbxasset://textures/ui/Controls/graveaccent.png",
["."] = "rbxasset://textures/ui/Controls/period.png",
[" "] = "rbxasset://textures/ui/Controls/spacebar.png",
}
-- 一部キーコードとテキストのマッピング.
local KeyCodeToTextMapping = {
[Enum.KeyCode.LeftControl] = "Ctrl",
[Enum.KeyCode.RightControl] = "Ctrl",
[Enum.KeyCode.LeftAlt] = "Alt",
[Enum.KeyCode.RightAlt] = "Alt",
[Enum.KeyCode.F1] = "F1",
[Enum.KeyCode.F2] = "F2",
[Enum.KeyCode.F3] = "F3",
[Enum.KeyCode.F4] = "F4",
[Enum.KeyCode.F5] = "F5",
[Enum.KeyCode.F6] = "F6",
[Enum.KeyCode.F7] = "F7",
[Enum.KeyCode.F8] = "F8",
[Enum.KeyCode.F9] = "F9",
[Enum.KeyCode.F10] = "F10",
[Enum.KeyCode.F11] = "F11",
[Enum.KeyCode.F12] = "F12",
}
local ButtonIconProvider = {}
export type ButtonImageTable = {
ButtonImage: ImageLabel,
TweensForFadeIn: {Tween},
TweensForFadeOut: {Tween}
}
-- 適切なボタンアイコンを複製し、FadeのTweenと一緒に返すメソッド.
function ButtonIconProvider:GetButtonImage(prompt: ProximityPrompt, inputType: Enum.ProximityPromptInputType, tweenInfo: TweenInfo): ButtonImageTable?
local buttonImage
local tweensForFadeOut = {}
local tweensForFadeIn = {}
-- ボタンアイコンの生成.
-- タッチ入力の場合.
if inputType == Enum.ProximityPromptInputType.Touch then
-- タッチ入力用のボタンアイコンを作成.
buttonImage = script:WaitForChild("TouchButtonImage"):Clone()
-- ゲームパッド入力の場合.
elseif inputType == Enum.ProximityPromptInputType.Gamepad then
-- ProximityPromptに紐づけられているGamepadのKeyCodeに応じた画像のボタンアイコンを作成.
if GamepadButtonImage[prompt.GamepadKeyCode] then
buttonImage = script:WaitForChild("GamepadButtonImage"):Clone()
buttonImage.Image = GamepadButtonImage[prompt.GamepadKeyCode]
end
-- それ以外(キーボード入力)の場合.
else
-- キーボード入力用のボタンアイコンのベースを作成.
buttonImage = script:WaitForChild("KeyButtonImage"):Clone()
-- ProximityPromptに紐づけられているKeyCodeを文字列で取得.
local buttonTextString = UserInputService:GetStringForKeyCode(prompt.KeyboardKeyCode)
-- 専用画像を用いるキーボード用ボタンアイコン画像を取得.
local buttonTextImage = KeyboardButtonImage[prompt.KeyboardKeyCode]
-- 取得できなかった場合、特殊文字用のボタンアイコン画像を取得.
if buttonTextImage == nil then
buttonTextImage = KeyboardButtonIconMapping[buttonTextString]
end
-- さらに取得できなかった場合、専用画像を用いないキーとしての処理を行う.
if buttonTextImage == nil then
-- Ctrlなどの一部のキーコード用の文字列を取得しなおす.
if KeyCodeToTextMapping[prompt.KeyboardKeyCode] then
buttonTextString = KeyCodeToTextMapping[prompt.KeyboardKeyCode]
end
end
-- この時点で画像が取得できている(専用画像を用いるキーコードだった)場合.
if buttonTextImage then
local icon = script:WaitForChild("KeyIconImage"):Clone()
icon.Image = buttonTextImage
-- アイコン部分用の表示と終了のTweenを作成.
table.insert(tweensForFadeOut, TweenService:Create(icon, tweenInfo, { ImageTransparency = 1 }))
table.insert(tweensForFadeIn, TweenService:Create(icon, tweenInfo, { ImageTransparency = icon.ImageTransparency }))
-- 初期の透明度を1に.
icon.ImageTransparency = 1
-- ButtonImageの子にする.
icon.Parent = buttonImage
-- 専用画像を用いないキーで、文字列が取得できている場合.
elseif buttonTextString ~= nil and buttonTextString ~= "" then
local buttonText = script:WaitForChild("KeyIconText"):Clone()
buttonText.Text = buttonTextString
-- 二文字以上ならTextSizeを12に下げる.
if string.len(buttonTextString) > 2 then
buttonText.TextSize = 12
end
-- アイコン部分用の表示と終了のTweenを作成.
table.insert(tweensForFadeOut, TweenService:Create(buttonText, tweenInfo, { TextTransparency = 1 }))
table.insert(tweensForFadeIn, TweenService:Create(buttonText, tweenInfo, { TextTransparency = buttonText.TextTransparency }))
-- 初期の透明度を1に.
buttonText.TextTransparency = 1
-- ButtonImageの子にする.
buttonText.Parent = buttonImage
else
-- 非対応のキーコードなので破棄して終了.
buttonImage:Destroy()
return nil
end
end
-- ボタンアイコン用の表示と終了のTweenを作成.
if buttonImage then
table.insert(tweensForFadeOut, TweenService:Create(buttonImage, tweenInfo, { ImageTransparency = 1 }))
table.insert(tweensForFadeIn, TweenService:Create(buttonImage, tweenInfo, { ImageTransparency = buttonImage.ImageTransparency }))
-- 初期の透明度を1に.
buttonImage.ImageTransparency = 1
else
-- ボタンアイコンを生成できていなかったら終了.
return nil
end
-- tableにして返す.
local result = {
ButtonImage = buttonImage,
TweensForFadeIn = tweensForFadeIn,
TweensForFadeOut = tweensForFadeOut
}::ButtonImageTable
return result
end
return ButtonIconProvider
5. まとめ
- ObjectTextは、ProximityPromptのアクセス対象を表示するText枠
- ActionTextは、そのアクセス対象にどんなアクションを行うのかを表示するためのText枠
- UIListLayoutは、GuiObjectを整列させることができるオブジェクト
- LayoutOrderは、UIListLayoutなどの整列順を指定するプロパティ
- AutomaticSizeを使って、Textの長さに応じてUIの幅を自動調整できる
- UIを表示する際、各Textの有無に応じて、FrameやTextLabelの表示を切り替える
- さらに、ProximityPrompt側のTextに関する設定をTextLabelに反映する
今回は、ProximityPromptのObjectTextとActionTextの表示を実装しました。
これで、ボタンを押したら即動作するProximityPromptのUIに必要な機能は揃いました。
次回は最後に残った要素として、Hold時の進捗ゲージを実装します!
長く続いたProximityPromptのカスタムUIシリーズも次回でラストです。
ぜひそちらもご覧ください!
最後までご覧いただき、ありがとうございました!
6. 参考
当社ではRobloxを活用したゲームの開発、 また企業の商品やサービスの認知度拡大に寄与する3Dワールドの制作など、 Robloxにおける様々な活用支援を行っております。 Robloxのコンテンツ開発をご検討されている企業様は、お気軽にご相談ください。 landho.co.jp/
Discussion