载具初始化时序
中文 | English
在仓库根目录运行以下命令,用本机 Unity MCP 回放本页示例。
python Docs/Manual/manual/tools/tutorials.py run lua-vehicle-init-timing
点击展开 MCP 操作点击收起 MCP 操作
准备教学样例。${变量} 由配方提供。
{
"variables": {
"script": "${fixture}/VehicleTiming.lua",
"prefab": "${fixture}/Tutorial_VehicleTiming.prefab"
},
"calls": [
{
"tool": "invoke",
"arguments": {
"path": "static:UnityEditor.AssetDatabase/CreateFolder",
"args": [
"${fixtureParent}",
"${folder}"
]
},
"expect": {
"contains": "(String)"
}
}
]
}
在 Lua API 2 及以上客户端,把需要载具部件的逻辑放到 OnVehicleReady。脚本先运行 OnStarted。如果载具已经装配完成,随后立即调用 OnVehicleReady,否则在装配完成后调用一次。
将脚本绑定到载具上的 LuaBehaviorMono,用 InitVehicleDependencies 初始化自己的部件。兼容旧客户端时,先通过 TryGetTankInitSystem 获取载具,再检查 IsLoaded 或订阅加载事件。示例在行为销毁时解除旧版订阅。
Behavior()
Property = {}
local VehicleTiming = class("VehicleTiming")
function VehicleTiming:OnVehicleReady(vehicle)
if vehicle == nil or self.vehicle ~= nil then
return
end
self.vehicle = vehicle
self:InitVehicleDependencies()
end
function VehicleTiming:InitVehicleDependencies()
print("[VehicleTiming] vehicle is ready")
end
function VehicleTiming:OnStarted()
if (GAME_API_VERSION or 1) >= 2 then
return
end
local found, vehicle = self.script:TryGetTankInitSystem()
if not found or vehicle == nil then
return
end
if vehicle.IsLoaded then
self:OnVehicleReady(vehicle)
return
end
self.legacyVehicle = vehicle
self.legacyLoaded = function()
self:OnVehicleReady(vehicle)
end
VehicleAPI.RegisterVehicleLoadedEvent(vehicle, self.legacyLoaded)
end
function VehicleTiming:OnDestroyed()
if self.legacyLoaded ~= nil then
VehicleAPI.UnRegisterVehicleLoaedEvent(self.legacyVehicle, self.legacyLoaded)
self.legacyVehicle = nil
self.legacyLoaded = nil
end
end
return VehicleTiming
点击展开 MCP 操作点击收起 MCP 操作
[
{
"tool": "invoke",
"arguments": {
"path": "static:System.IO.File/Copy",
"args": [
"../../Docs/Manual/tutorials/lua-vehicle-init-timing/example.lua",
"${script}"
]
},
"expect": {
"contains": "OK:"
}
},
{
"tool": "invoke",
"arguments": {
"path": "static:UnityEditor.AssetDatabase/ImportAsset",
"args": [
"${script}",
"ForceSynchronousImport"
]
},
"expect": {
"contains": "OK:"
}
},
{
"tool": "prefab_edit",
"arguments": {
"json": {
"path": "${prefab}",
"mode": "create",
"component": "ShanghaiWindy.Core.Lua.LuaBehaviorMono",
"children": [],
"references": [
{
"property": "luaBehavior",
"asset": "${script}",
"type": "ShanghaiWindy.Core.Lua.LuaBehaviorScript"
}
]
}
},
"expect": {
"equals": {
"ok": true
}
}
},
{
"tool": "inspect",
"arguments": {
"path": "asset:${prefab}/LuaBehaviorMono/luaBehavior/name"
},
"expect": {
"contains": "(String) VehicleTiming"
}
}
]
回放验证新版不重复订阅、旧版延迟初始化和注销路径。实际装配时序在游戏中检查。
点击展开 MCP 操作点击收起 MCP 操作
[
{
"tool": "invoke",
"arguments": {
"path": "static:ShanghaiWindy.Core.AssetBundleManager/LuaEnv/DoString",
"args": [
"local E = setmetatable({ Behavior = function() end, class = function() return {} end, print = function() end }, { __index = _G })\nlocal subscriptions, removals, inits = 0, 0, 0\nlocal callback\nE.VehicleAPI = {\n RegisterVehicleLoadedEvent = function(v, cb) subscriptions = subscriptions + 1; callback = cb end,\n UnRegisterVehicleLoaedEvent = function(v, cb) assert(cb == callback); removals = removals + 1 end\n}\n\nlocal B = assert(loadfile('../../Docs/Manual/tutorials/lua-vehicle-init-timing/example.lua', 't', E))()\nlocal function instance(t) return setmetatable(t, { __index = B }) end\nlocal vehicle = { IsLoaded = false }\nlocal script = { TryGetTankInitSystem = function() return true, vehicle end }\nlocal init = function() inits = inits + 1 end\nE.GAME_API_VERSION = 2\nlocal current = instance({ script = script, InitVehicleDependencies = init })\ncurrent:OnStarted()\nassert(subscriptions == 0 and current.vehicle == nil)\ncurrent:OnVehicleReady(vehicle)\ncurrent:OnVehicleReady(vehicle)\nassert(inits == 1 and current.vehicle == vehicle)\nE.GAME_API_VERSION = nil\nlocal legacy = instance({ script = script, InitVehicleDependencies = init })\nlegacy:OnStarted()\nassert(subscriptions == 1 and legacy.vehicle == nil)\ncallback()\ncallback()\nassert(inits == 2 and legacy.vehicle == vehicle)\nlegacy:OnDestroyed()\nassert(removals == 1)\nreturn \"Lua example passed\"\n",
"manual-lua-vehicle-init-timing",
"null"
]
},
"expect": {
"contains": "Lua example passed"
}
}
]