概要
方法
MultiEditTextAsync(edits: {any}):Tuple |
活动
SelectionChanged(positionLine: number,positionCharacter: number,anchorLine: number,anchorCharacter: number):RBXScriptSignal |
ViewportChanged(startLine: number,endLine: number):RBXScriptSignal |
API 参考
方法
CloseAsync
返回
代码示例
脚本文档:CloseAsync
--!nocheck
-- 在命令栏中运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- 找到第一个打开的脚本文档
for _, document in documents do
-- 命令栏不能关闭,所以不要选择它
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`关闭了 {scriptDocument.Name}`)
else
warn(`关闭 {scriptDocument.Name} 失败,原因是: {err}`)
end
else
print("没有打开的脚本")
endEditTextAsync
ForceSetSelectionAsync
返回
代码示例
脚本文档:ForceSetSelectionAsync()
--!nocheck
-- 在打开脚本时在命令栏中运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- 获取光标当前行的文本
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- 强制选择整行文本
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("选择已设置!")
else
print(`选择设置失败,原因: {err}`)
end
else
print("没有打开的脚本")
endGetLine
参数
| 默认值:"nil" |
返回
代码示例
脚本文档.选定更改 和 脚本文档:获取行()
--!nocheck
-- 在打开脚本时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`选定: 行 {positionLine}, 字符 {positionCharacter}`)
print(`锚点: 行 {anchorLine}, 字符 {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`选定行文本: {lineText}`)
end)
else
print("没有打开的脚本")
endGetLineCount
返回
代码示例
脚本文档:获取行数()
--!nocheck
-- 在脚本打开时在命令栏中运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local lineCount = scriptDocument:GetLineCount()
print(`脚本有 {lineCount} 行!`)
else
print("没有打开的脚本")
endGetScript
代码示例
脚本文档:获取脚本()
--!nocheck
-- 在脚本打开时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local openScript = scriptDocument:GetScript()
print(`当前打开的脚本: {openScript:GetFullName()}`)
else
print("没有打开的脚本")
endGetSelectedText
返回
代码示例
脚本文档:HasSelectedText() 和 :GetSelectedText()
--!nocheck
-- 在打开脚本时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`当前选择的文本: {selectedText}`)
else
print("当前没有选择的文本")
end
end)
else
print("没有打开脚本")
endGetSelectionEnd
返回
代码示例
脚本文档:获取选择开始() 和 :获取选择结束()
--!nocheck
-- 在脚本打开时,在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`选择开始: 行 {startLine}, 字符 {startCharacter}`)
print(`选择结束: 行 {endLine}, 字符 {endCharacter}`)
else
print("没有打开的脚本")
endGetSelectionStart
返回
代码示例
脚本文档:获取选择开始() 和 :获取选择结束()
--!nocheck
-- 在脚本打开时,在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`选择开始: 行 {startLine}, 字符 {startCharacter}`)
print(`选择结束: 行 {endLine}, 字符 {endCharacter}`)
else
print("没有打开的脚本")
endGetText
返回
代码示例
脚本文档:获取文本()
--!nocheck
-- 在脚本打开时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local text = scriptDocument:GetText()
print(`脚本内容: {text}`)
else
print("没有打开的脚本")
endGetViewport
返回
代码示例
脚本文档:获取视口
--!nocheck
-- 在打开脚本时,在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local firstLine, lastLine = scriptDocument:GetViewport()
print(`当前查看的行数从 {firstLine} 到 {lastLine}`)
else
print("没有打开的脚本")
endHasSelectedText
返回
代码示例
脚本文档:HasSelectedText() 和 :GetSelectedText()
--!nocheck
-- 在打开脚本时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`当前选择的文本: {selectedText}`)
else
print("当前没有选择的文本")
end
end)
else
print("没有打开脚本")
endIsCommandBar
返回
代码示例
脚本文档:IsCommandBar()
--!nocheck
-- 在命令栏中运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("命令栏文档:", document)
end
endRequestSetSelectionAsync
返回
代码示例
脚本文档:请求选择异步()
--!不检查
-- 在脚本打开时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- 获取光标当前行的文本
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- 强制选择整行文本
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("设置选择成功!")
else
print(`设置选择失败,原因是: {err}`)
end
else
print("没有打开的脚本")
end活动
SelectionChanged
ScriptDocument.SelectionChanged(
代码示例
脚本文档.选定更改 和 脚本文档:获取行()
--!nocheck
-- 在打开脚本时在命令栏运行以下代码
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`选定: 行 {positionLine}, 字符 {positionCharacter}`)
print(`锚点: 行 {anchorLine}, 字符 {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`选定行文本: {lineText}`)
end)
else
print("没有打开的脚本")
endViewportChanged
代码示例
连接到 ScriptDocument.ViewportChanged
--!nocheck
--[[
要运行:
1. 确保输出视图已打开
2. 在命令栏中运行以下代码
3. 在打开的脚本窗口中上下滚动
来自 ViewportChanged 事件的打印语句将出现在输出中
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- 创建跨越多行的文本
local dummyText = string.rep("-- Dummy Text\n", 60)
-- 创建一个包含虚拟文本的脚本并打开它
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`打开脚本失败,因为: {err}`)
return
end
-- 获取对打开脚本的引用
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`脚本视口已更改 - startLine: {startLine}, endLine: {endLine}`)
end
-- 将 ViewportChanged 事件连接到上述函数,该函数打印更新的视口的起始行和结束行
scriptDocument.ViewportChanged:Connect(onViewportChanged)