概要
方法
MultiEditTextAsync(edits: {any}):Tuple |
活動
SelectionChanged(positionLine: number,positionCharacter: number,anchorLine: number,anchorCharacter: number):RBXScriptSignal |
ViewportChanged(startLine: number,endLine: number):RBXScriptSignal |
API 參考
方法
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
返回
範例程式碼
ScriptDocument: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" |
返回
範例程式碼
ScriptDocument.SelectionChanged 和 ScriptDocument:GetLine()
--!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
返回
範例程式碼
ScriptDocument:獲取行數()
--!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
範例程式碼
ScriptDocument:GetScript()
--!nocheck
-- 在 Command Bar 中運行以下代碼,當腳本打開時
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
返回
範例程式碼
ScriptDocument:HasSelectedText() 和 :GetSelectedText()
--!nocheck
-- 在 Command Bar 中運行以下代碼,前提是有一個腳本打開
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
返回
範例程式碼
ScriptDocument:GetSelectionStart() 和 :GetSelectionEnd()
--!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
返回
範例程式碼
ScriptDocument:GetSelectionStart() 和 :GetSelectionEnd()
--!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
返回
範例程式碼
ScriptDocument:HasSelectedText() 和 :GetSelectedText()
--!nocheck
-- 在 Command Bar 中運行以下代碼,前提是有一個腳本打開
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
返回
範例程式碼
ScriptDocument:是命令列嗎()
--!nocheck
-- 在命令列中運行以下代碼
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("命令列文檔:", document)
end
endRequestSetSelectionAsync
返回
範例程式碼
腳本文件:RequestSetSelectionAsync()
--!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:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("選擇已設置!")
else
print(`設置選擇失敗,原因:{err}`)
end
else
print("沒有開啟的腳本")
end活動
SelectionChanged
ScriptDocument.SelectionChanged(
範例程式碼
ScriptDocument.SelectionChanged 和 ScriptDocument:GetLine()
--!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("-- 假文本\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}, 結束行: {endLine}`)
end
-- 連接視口變更事件到上面的函數,該函數打印更新後視口的開始和結束行
scriptDocument.ViewportChanged:Connect(onViewportChanged)