Search Results for

    Show / Hide Table of Contents

    用弹药数量控制导弹挂架模型

    中文 | English

    在仓库根目录运行以下命令,用本机 Unity MCP 回放本页示例。

    python Docs/Manual/manual/tools/tutorials.py run lua-api2-missile-rack
    
    点击展开 MCP 操作点击收起 MCP 操作

    准备教学样例。${变量} 由配方提供。

    {
      "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)"
          }
        }
      ]
    }
    

    把脚本挂到载具模型,按层级顺序放置 Missile_1、Missile_2 等子物体。设置 weaponIndex 和用于匹配对象名的 modelPrefix。weaponIndex 默认值 1 表示第二个武器,第一个武器填 0。modelPrefix 区分大小写,可以匹配名称中的任意位置。取一个只会命中该组模型的字符串。

    脚本在 OnVehicleReady 中调用一次 TransformAPI.FindAllDeep,按层级缓存名称中含该字符串的后代对象,包括未激活的对象。随后按武器当前弹量显示列表中最前面的模型。

    WeaponAPI.OnAmmoChanged 在弹药消耗或补给时刷新显示。通过 self.script 订阅会让回调与脚本生命周期绑定,销毁时自动解除订阅;不使用 OnUpdated 轮询。弹量减少时先隐藏数组末尾的模型。

    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
    
    点击展开 MCP 操作点击收起 MCP 操作
    [
      {
        "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"
        }
      }
    ]
    

    回放核对初始数量、消耗和补给逻辑。实际开火和补给在游戏中验证。

    点击展开 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 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"
        }
      }
    ]
    
    In this article
    Back to top Panzer War Document