Match the missile rack models to the ammo count
中文 | English
Run this command at the repository root to replay the examples through the local Unity MCP.
python Docs/Manual/manual/tools/tutorials.py run lua-api2-missile-rack
Click to expand MCP actionsClick to collapse MCP actions
Prepare the teaching sample. The recipe provides ${variables}.
{
"variables": {
"script": "${fixture}/MissileRack.lua",
"prefab": "${fixture}/Tutorial_MissileRack.prefab"
},
"calls": [
{
"tool": "invoke",
"arguments": {
"path": "static:UnityEditor.AssetDatabase/CreateFolder",
"args": [
"${fixtureParent}",
"${folder}"
]
},
"expect": {
"contains": "(String)"
}
}
]
}
Attach the script to the vehicle model and add children such as Missile_1 and Missile_2 in hierarchy order. Set weaponIndex and the text to match in object names, modelPrefix. The default index 1 selects the second weapon. Use 0 for the first. modelPrefix matches a case-sensitive substring anywhere in the name, so choose text unique to this group of models.
The script calls TransformAPI.FindAllDeep once in OnVehicleReady to cache matched descendants in hierarchy order, including inactive objects. It then shows the first models in the list according to the weapon's current ammo count.
WeaponAPI.OnAmmoChanged refreshes the display when ammo is spent or replenished. Subscribing with self.script ties the callback to the script's lifetime, so the subscription is removed automatically on destruction; there is no OnUpdated polling. When ammo decreases, the models at the end disappear first.
Behavior()
Property = {
weaponIndex = { type = "number", value = 1 },
modelPrefix = { type = "string", value = "Missile_" },
}
local MissileRack = class("MissileRack")
function MissileRack:OnVehicleReady(vehicle)
self.weapon = WeaponAPI.GetWeapon(vehicle, self.weaponIndex)
if self.weapon == nil then
return
end
self.models = TransformAPI.FindAllDeep(self.transform, self.modelPrefix)
self:Refresh()
WeaponAPI.OnAmmoChanged(self.script, self.weapon, function()
self:Refresh()
end)
end
function MissileRack:Refresh()
local left = WeaponAPI.GetTotalAmmo(self.weapon)
for index = 0, self.models.Length - 1 do
self.models[index].gameObject:SetActive(index < left)
end
end
return MissileRack
Click to expand MCP actionsClick to collapse MCP actions
[
{
"tool": "invoke",
"arguments": {
"path": "static:System.IO.File/Copy",
"args": [
"../../Docs/Manual/tutorials/lua-api2-missile-rack/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": [
"Missile_1",
"Missile_2"
],
"references": [
{
"property": "luaBehavior",
"asset": "${script}",
"type": "ShanghaiWindy.Core.Lua.LuaBehaviorScript"
}
],
"values": {
"instanceLuaProperty": {
"numberProperties": [
{
"variableName": "weaponIndex",
"value": 1
}
],
"stringProperties": [
{
"variableName": "modelPrefix",
"value": "Missile_"
}
]
}
}
}
},
"expect": {
"equals": {
"ok": true
}
}
},
{
"tool": "inspect",
"arguments": {
"path": "asset:${prefab}/LuaBehaviorMono/luaBehavior/name"
},
"expect": {
"contains": "(String) MissileRack"
}
}
]
The replay checks the initial count, consumption and resupply logic. Verify actual firing and resupply in game.
Click to expand MCP actionsClick to collapse MCP actions
[
{
"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 rounds, callback, searches = 2, nil, 0\nlocal weapon = {}\nlocal visible = {}\nlocal function model(index)\n return { gameObject = { SetActive = function(_, value) visible[index] = value end } }\nend\nE.WeaponAPI = {\n GetWeapon = function(_, index) if index == 1 then return weapon end end,\n GetTotalAmmo = function(w) assert(w == weapon); return rounds end,\n OnAmmoChanged = function(owner, w, cb) assert(owner and w == weapon); callback = cb end,\n}\nE.TransformAPI = { FindAllDeep = function(_, prefix)\n assert(prefix == \"Missile_\"); searches = searches + 1\n return { Length = 2, [0] = model(1), [1] = model(2) }\nend }\n\nlocal B = assert(loadfile('../../Docs/Manual/tutorials/lua-api2-missile-rack/example.lua', 't', E))()\nlocal function instance(t) return setmetatable(t, { __index = B }) end\ninstance({ weaponIndex = 0 }):OnVehicleReady({})\nassert(searches == 0 and callback == nil)\ninstance({ weaponIndex = 1, modelPrefix = \"Missile_\", script = {} }):OnVehicleReady({})\nassert(visible[1] and visible[2])\nrounds = 1\ncallback()\nassert(visible[1] and not visible[2])\nrounds = 2\ncallback()\nassert(visible[1] and visible[2] and searches == 1)\nlocal go = CS.UnityEditor.AssetDatabase.LoadMainAssetAtPath('${prefab}')\nlocal actual = CS.ShanghaiWindy.Core.API.TransformAPI.FindAllDeep(go.transform, \"Missile_\")\nassert(actual.Length == 2 and actual[0].name == \"Missile_1\" and actual[1].name == \"Missile_2\")\nreturn \"Lua example passed\"\n",
"manual-lua-api2-missile-rack",
"null"
]
},
"expect": {
"contains": "Lua example passed"
}
}
]