插件开发者 必须 使用 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 中的用户触发的行动必须调用后完成其对体验的更改集。在 之前调用它可能会清理另一个失败的插件,没有设置路径点的,但这是一个很差的理由为自己的插件正当化这种使用。
参数
返回
代码示例
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
撤销最后一次采取的行动,其中存在路径点。