插件開發者 必須 使用 ChangeHistoryService 告訴 Studio 如何撤消和重做他們的插件對體驗所做的變更記錄。在進行變更之前,插件會呼叫 ChangeHistoryService:TryBeginRecording() ,記住它指派的標識符,然後進行變更後,插件會呼叫 ChangeHistoryService:FinishRecording() 來完成記錄。
插件也可以透過 ChangeHistoryService:Undo() 或 ChangeHistoryService:Redo() 程式式地呼叫撤消或重做。
ChangeHistoryService 在運行時不啟用,因此在執行中呼叫其方法沒有效果。
概要
方法
- FinishRecording(identifier : string,operation : Enum.FinishRecordingOperation,finalOptions : Dictionary?):()
通知工作室已識別的錄音結束,並將最後一步操作完成錄音。
返回是否有可以撤消的行動,如果有,則返回最後一個。
返回是否有可以撤消的行動,如果有,則返回最後一個。
執行被取消的最後一個行動。
清除歷史記錄,導致所有撤消/重做路點被移除。
設置是否啟用變更歷史服務。
設置一個新的路點,可用作撤消或重做點。
開始記錄對數據模型進行的更改變更到記錄中。
撤消最後一次執行的行動,其中有一個路徑點存在。
活動
- OnRecordingFinished(name : string,displayName : string?,identifier : string?,operation : Enum.FinishRecordingOperation,finalOptions : Dictionary?):RBXScriptSignal
當使用者完成行動作時發射。參數來自 TryBeginRecording() 和 FinishRecording() 。
當使用者開始動作動時發射。參數來自 TryBeginRecording() 。
當使用者反轉撤消指令時發射。路徑描述已撤消的類型行動。
當使用者在工作室取消行動時,發射點會描述已取消的類型行動。
屬性
方法
FinishRecording
參數
從上一個呼叫到 TryBeginRecording() 的記錄進行標示。如果操作是 Enum.ChangeHistoryService.FinishRecordingOperation.Cancel , 這個值被忽略,記錄由上下文決定。
指定要執行的操作。
可選擇的值表傳送到 OnFinishRecording 。
返回
範例程式碼
To commit an undo/redo record, you need to first call TryBeginRecording() followed by calling FinishRecording().
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")
local toolbar = plugin:CreateToolbar("Example Plugin")
local button = toolbar:CreateButton("Neon it up", "", "")
button.Click:Connect(function()
local parts = {}
for _, part in pairs(Selection:Get()) do
if part:IsA("BasePart") then
parts[#parts + 1] = part
end
end
if #parts < 1 then
-- Nothing to do.
return
end
local recording = ChangeHistoryService:TryBeginRecording("Set selection to neon")
if not recording then
-- Handle error here. This indidcates that your plugin began a previous
-- recording and never completed it. You may only have one recording
-- per plugin active at a time.
return
end
for _, part in pairs(parts) do
part.Material = Enum.Material.Neon
end
ChangeHistoryService:FinishRecording(recording, Enum.FinishRecordingOperation.Commit)
end)
Redo
執行被取消的最後一個行動。
返回
ResetWaypoints
清除歷史記錄,導致所有撤消/重做路點被移除。
返回
SetEnabled
設置是否啟用變更歷史服務。當設為 false 時,撤消/重做列表將被清除,並不會重新填充。當再次設為真實時,原始列表不會被恢復,但進一步操作將再次附加到列表
參數
返回
SetWaypoint
此方法將很快被 淘汰 以支持 TryBeginRecording() 。
ChangeHistoryService 追蹤插件歷史記錄作為一個屬性變更的流。SetWaypoint()在那個屬性變更流中的切割,使撤消和重做行動知道停止在哪裡。
依據慣例,Studio 中的使用者呼叫的行動必須 在完成其對體驗的變更後呼叫 SetWaypoint()之後。在 之前呼叫它可能會清理另一個失敗設置路徑點的插件,但這是一個很差的理由來為自己的外掛程式正當化這種使用。
參數
返回
範例程式碼
In order for the waypoints to work correctly, you need to set one both before AND after you perform the action that should be able to be undone.
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")
local toolbar = plugin:CreateToolbar("Example Plugin")
local button = toolbar:CreateButton("Neon it up", "", "")
button.Click:Connect(function()
local parts = {}
for _, part in pairs(Selection:Get()) do
if part:IsA("BasePart") then
parts[#parts + 1] = part
end
end
if #parts > 0 then
-- Calling SetWaypoint before the work will not cause any issues, however
-- it is redundant, only the call AFTER the work is needed.
--ChangeHistoryService:SetWaypoint("Setting selection to neon")
for _, part in pairs(parts) do
part.Material = Enum.Material.Neon
end
-- Call SetWaypoint AFTER completing the work
ChangeHistoryService:SetWaypoint("Set selection to neon")
else
-- Nothing to do. You do not need to call SetWaypoint in the case where
-- the action did not end up making any changes to the experience.
end
end)
TryBeginRecording
此方法開始記錄,以跟蹤數據模型的變更。你 必須 在進行變更之前呼叫它,以避免未來警告或錯誤。
當錄製完成時,您可以使用返回的錄製標識符號來呼叫 FinishRecording() 以完成錄製並更新撤消/重做堆。
此方法將返回 nil 如果無法開始記錄。如果插件已經在進行錄音,或者使用者正在 運行 或 播放 模式,錄音將失敗。
您可以使用 IsRecordingInProgress() 來檢查外掛程式的錄製狀態。
參數
返回
範例程式碼
To commit an undo/redo record, you need to first call TryBeginRecording() followed by calling FinishRecording().
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")
local toolbar = plugin:CreateToolbar("Example Plugin")
local button = toolbar:CreateButton("Neon it up", "", "")
button.Click:Connect(function()
local parts = {}
for _, part in pairs(Selection:Get()) do
if part:IsA("BasePart") then
parts[#parts + 1] = part
end
end
if #parts < 1 then
-- Nothing to do.
return
end
local recording = ChangeHistoryService:TryBeginRecording("Set selection to neon")
if not recording then
-- Handle error here. This indidcates that your plugin began a previous
-- recording and never completed it. You may only have one recording
-- per plugin active at a time.
return
end
for _, part in pairs(parts) do
part.Material = Enum.Material.Neon
end
ChangeHistoryService:FinishRecording(recording, Enum.FinishRecordingOperation.Commit)
end)
Undo
撤消最後一次執行的行動,其中有一個路徑點存在。