Lua插件在读取目录下的文件时报错
想通过CS.System.IO.DirectoryInfo:GetFiles()来获取目录下有哪些文件,可是报错了。想问下为什么GetDirectories()可以调用,GetFiles()却不行。
下面是测试代码:
[16:08:53] [Test] PluginPath = D:\Unity\FGUIProject\plugins\Test, testPath = D:\Unity\FGUIProject
[16:08:53] directoryInfo = D:\Unity\FGUIProject: -1112562640
[16:08:53] allDirectories = System.IO.DirectoryInfo[]: -1127617904, Length=4
[16:08:53] directory = D:\Unity\FGUIProject\.objs: -155161072, FullName = D:\Unity\FGUIProject\.objs
[16:08:53] directory = D:\Unity\FGUIProject\assets: 323539712, FullName = D:\Unity\FGUIProject\assets
[16:08:53] directory = D:\Unity\FGUIProject\plugins: 802240496, FullName = D:\Unity\FGUIProject\plugins
[16:08:53] directory = D:\Unity\FGUIProject\settings: 1280941280, FullName = D:\Unity\FGUIProject\settings
[16:08:53] LuaException: [string "Plugin-com.fairygui.plugin.test"]:32: attempt to call a nil value (method 'GetFiles')
stack traceback:
[string "Plugin-com.fairygui.plugin.test"]:32: in function <[string "Plugin-com.fairygui.plugin.test"]:13>
XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) (at <00000000000000000000000000000000>:0)
XLua.LuaFunction.Action[T] (T a) (at <00000000000000000000000000000000>:0)
UnityEngine.AI.NavMesh+OnNavMeshPreUpdate.Invoke () (at <00000000000000000000000000000000>:0)
FairyGUI.EventBridge.CallInternal (FairyGUI.EventContext context) (at <00000000000000000000000000000000>:0)
FairyGUI.EventDispatcher.InternalDispatchEvent (System.String strType, FairyGUI.EventBridge bridge, System.Object data, System.Object initiator) (at <00000000000000000000000000000000>:0)
...
下面是测试代码:
---@type CS.System.IO.DirectoryInfo下面是报错信息:
local DirectoryInfo = CS.System.IO.DirectoryInfo
---@type CS.System.IO.FileInfo
local FileInfo = CS.System.IO.FileInfo
---@param fmt string
local function printf(fmt, ...)
App.consoleView:Log(string.format(tostring(fmt), ...))
end
---@type CS.FairyEditor.Component.MenuBar
local toolMenu = App.menu:GetSubMenu("tool")
toolMenu:AddItem("Test", "TestItem", 0, false, function(menuItem)
-- 测试的目录为工程目录
local testPath = App.project.basePath
printf("[Test] PluginPath = %s, testPath = %s", PluginPath, testPath)
-- 获取目录信息
local directoryInfo = DirectoryInfo(testPath)
printf("directoryInfo = %s", directoryInfo)
-- 获取目录下所有文件夹
local allDirectories = directoryInfo:GetDirectories()
printf("allDirectories = %s, Length=%s", allDirectories, allDirectories.Length)
-- 遍历目录
for i = 0, allDirectories.Length-1 do
local directory = allDirectories[i]
printf("directory = %s, FullName = %s", directory, directory.FullName)
end
-- 获取目录下所有文件
local allFiles = directoryInfo:GetFiles()
printf("allFiles = %s, Length=%s", allFiles, allFiles.Length)
-- 遍历文件
for j = 0, allFiles.Length-1 do
local file = allFiles[j]
printf("file = %s, FullName=%s", file, file.FullName)
end
end)
function onDestroy()
toolMenu:RemoveItem("TestItem")
end
[16:08:53] [Test] PluginPath = D:\Unity\FGUIProject\plugins\Test, testPath = D:\Unity\FGUIProject
[16:08:53] directoryInfo = D:\Unity\FGUIProject: -1112562640
[16:08:53] allDirectories = System.IO.DirectoryInfo[]: -1127617904, Length=4
[16:08:53] directory = D:\Unity\FGUIProject\.objs: -155161072, FullName = D:\Unity\FGUIProject\.objs
[16:08:53] directory = D:\Unity\FGUIProject\assets: 323539712, FullName = D:\Unity\FGUIProject\assets
[16:08:53] directory = D:\Unity\FGUIProject\plugins: 802240496, FullName = D:\Unity\FGUIProject\plugins
[16:08:53] directory = D:\Unity\FGUIProject\settings: 1280941280, FullName = D:\Unity\FGUIProject\settings
[16:08:53] LuaException: [string "Plugin-com.fairygui.plugin.test"]:32: attempt to call a nil value (method 'GetFiles')
stack traceback:
[string "Plugin-com.fairygui.plugin.test"]:32: in function <[string "Plugin-com.fairygui.plugin.test"]:13>
XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) (at <00000000000000000000000000000000>:0)
XLua.LuaFunction.Action[T] (T a) (at <00000000000000000000000000000000>:0)
UnityEngine.AI.NavMesh+OnNavMeshPreUpdate.Invoke () (at <00000000000000000000000000000000>:0)
FairyGUI.EventBridge.CallInternal (FairyGUI.EventContext context) (at <00000000000000000000000000000000>:0)
FairyGUI.EventDispatcher.InternalDispatchEvent (System.String strType, FairyGUI.EventBridge bridge, System.Object data, System.Object initiator) (at <00000000000000000000000000000000>:0)
...
没有找到相关结果
已邀请:
3 个回复
谷主
赞同来自: Langdex
Langdex
赞同来自:
谷主
赞同来自: