45 lines
1.4 KiB
Lua
Raw Permalink Normal View History

2025-01-04 23:00:19 +08:00
--- 文件工具
FileTool = FileTool or {}
FileTool.IsServer = nil;
-- 缓存的资产
FileTool.CacheAsset = {};
---@param AssetPath string
---@param CallBackFunc fun(LoadObject:UObject,resID:int32)
---@param Obj table CallBackFunc拥有者
---@param SaveAsset bool 是否保存
function FileTool.AsyncLoadAsset(AssetPath, CallBackFunc, Obj, SaveAsset)
if not UE.IsValid(FileTool.CacheAsset[AssetPath]) then
local softObjPath = KismetSystemLibrary.MakeSoftObjectPath(AssetPath);
STExtraBlueprintFunctionLibrary.GetAssetByAssetReferenceAsync(softObjPath, ObjectExtend.CreateDelegate(UGCGameSystem.GameState, function(LoadObject, resID)
if Obj ~= nil then
CallBackFunc(Obj, LoadObject);
else
CallBackFunc(LoadObject);
end
if UE.IsValid(LoadObject) and SaveAsset then
FileTool.CacheAsset[AssetPath] = LoadObject;
end
end), true);
else
if Obj ~= nil then
CallBackFunc(Obj, FileTool.CacheAsset[AssetPath]);
else
CallBackFunc(FileTool.CacheAsset[AssetPath]);
end
end
end
---@param AssetPath string
---@param SaveAsset bool 是否保存
function FileTool.LoadAsset(AssetPath, SaveAsset)
if not UE.IsValid(FileTool.CacheAsset[AssetPath]) then
local TargetAsset = UE.LoadObject(AssetPath);
if SaveAsset and UE.IsValid(TargetAsset) then
FileTool.CacheAsset[AssetPath] = TargetAsset;
end
return TargetAsset;
end
return FileTool.CacheAsset[AssetPath];
end