Display radar contacts in a mod
中文 | 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-radar-screen
Click to expand MCP actionsClick to collapse MCP actions
Prepare the teaching sample. The recipe provides ${variables}.
{
"variables": {
"script": "${fixture}/RadarScreen.lua",
"prefab": "${fixture}/Tutorial_RadarScreen.prefab",
"vehicleData": "${fixture}/Tutorial_RadarData.asset"
},
"calls": [
{
"tool": "invoke",
"arguments": {
"path": "static:UnityEditor.AssetDatabase/CreateFolder",
"args": [
"${fixtureParent}",
"${folder}"
]
},
"expect": {
"contains": "(String)"
}
}
]
}
1. Configure the ground radar
Select the vehicle's VehicleTextData. Under Misc → AA Radar, expand groundAARadarData and enable hasRadar. The replay uses a 12,000 m detection range, 360° rotating search, and a rotation rate of 60°/s. For a directional radar, turn full rotation off and adjust the azimuth and elevation half angles.
| Field | Meaning |
|---|---|
maxDetectionRange |
Maximum detection range in metres |
isFullRotation, fullRotationRate |
Full-rotation search and its rate in degrees per second |
searchAzimuthHalfAngle, searchElevationHalfAngle |
Half angles of the search region. The defaults are 45° / 30°. |
minElevationAngle, detectGroundTargets |
The ground-clutter filter defaults to 2° minimum elevation. Allowing ground contacts skips this filter. Range and scan coverage still apply. |
displayRanges |
The default display scales are 2,500 / 5,000 / 10,000 / 20,000 m. |
canUsePD, pdMinDetectableSpeed |
Pulse-Doppler mode and its speed threshold. The default threshold is 30 m/s. |
hasIFF |
Show friendly returns and mark them as friendly. |
Click to expand MCP actionsClick to collapse MCP actions
[
{
"tool": "invoke",
"arguments": {
"path": "static:UnityEditor.AssetDatabase/CopyAsset",
"args": [
"Packages/com.shanghaiwindy.coreresources/Editor/DocumentationSamples/vehicle-text-data/Tutorial_VehicleTextData.asset",
"${vehicleData}"
]
},
"expect": {
"contains": "(Boolean) True"
}
},
{
"tool": "inspect_set",
"arguments": {
"path": "asset:${vehicleData}/groundAARadarData/hasRadar",
"value": "true"
},
"expect": {
"contains": "OK:"
}
},
{
"tool": "inspect_set",
"arguments": {
"path": "asset:${vehicleData}/groundAARadarData/maxDetectionRange",
"value": "12000"
},
"expect": {
"contains": "OK:"
}
},
{
"tool": "inspect_set",
"arguments": {
"path": "asset:${vehicleData}/groundAARadarData/isFullRotation",
"value": "true"
},
"expect": {
"contains": "OK:"
}
},
{
"tool": "inspect_set",
"arguments": {
"path": "asset:${vehicleData}/groundAARadarData/fullRotationRate",
"value": "60"
},
"expect": {
"contains": "OK:"
}
},
{
"tool": "inspect",
"arguments": {
"path": "asset:${vehicleData}/groundAARadarData/hasRadar"
},
"expect": {
"contains": "(Boolean) True"
}
},
{
"tool": "inspect",
"arguments": {
"path": "asset:${vehicleData}/groundAARadarData/maxDetectionRange"
},
"expect": {
"contains": "(Single) 12000"
}
}
]
2. Bind the script and update the list
- Save the script below as
RadarScreen.luaand import it. On a child object of the vehicle prefab, add LuaBehaviorMono, assign the script to Behavior, save, and repack. LuaBehaviorMono walkthrough OnVehicleReadyobtains the radar and subscribes to lock changes. The example refreshes contact text every 0.2 seconds. Unity can callOnGUIseveral times per frame, so it draws the cached text.- Read the latest scan's contacts from index
0toGetContactCount(radar) - 1.GetContactreturns success, target, distance, azimuth, elevation, altitude, radial speed, and friendly status in that order. A chaff return has aniltarget. Distance and altitude use metres, and radial speed uses m/s. Azimuth and elevation use the radar's frame of reference. UseGetContactWorldAzimuthfor a north-up display. UseGetScanAzimuthfor antenna yaw relative to its mount, from −180° to 180°.
Behavior()
Property = {}
local RadarScreen = class("RadarScreen")
local GUI = CS.UnityEngine.GUI
local Rect = CS.UnityEngine.Rect
function RadarScreen:OnVehicleReady(vehicle)
local ok, radar = RadarAPI.TryGetRadar(vehicle)
if not ok then
self.script:SetUpdateEnabled(false)
return
end
self.vehicle = vehicle
self.radar = radar
self.lines = {}
self.text = ""
RadarAPI.OnLockChanged(self.script, radar, function(locked, target)
if locked and target ~= nil then
print("[Radar] locked " .. VehicleAPI.GetDisplayName(target))
elseif not locked then
print("[Radar] lock lost")
end
end)
self.script:SetUpdateInterval(0.2)
end
function RadarScreen:OnUpdated()
self.visible = self.radar ~= nil
and VehicleAPI.IsLocalPlayer(self.vehicle)
and RadarAPI.IsActive(self.radar)
if not self.visible then
return
end
local written = 0
for index = 0, RadarAPI.GetContactCount(self.radar) - 1 do
local ok, target, distance, azimuth, elevation, altitude, radialSpeed, isFriendly =
RadarAPI.GetContact(self.radar, index)
if ok then
local name = target ~= nil and VehicleAPI.GetDisplayName(target) or "?"
local bearing = RadarAPI.GetContactWorldAzimuth(self.radar, index)
written = written + 1
self.lines[written] = string.format("%s %s %.1f km %03.0f° %.0f m",
isFriendly and "+" or "!", name, distance / 1000, bearing, altitude)
end
end
for index = written + 1, #self.lines do
self.lines[index] = nil
end
self.text = table.concat(self.lines, "\n")
end
function RadarScreen:OnGUI()
if self.visible then
GUI.Label(Rect(20, 120, 360, 300), self.text)
end
end
return RadarScreen
Click to expand MCP actionsClick to collapse MCP actions
[
{
"tool": "invoke",
"arguments": {
"path": "static:System.IO.File/Copy",
"args": [
"../../Docs/Manual/tutorials/lua-api2-radar-screen/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) RadarScreen"
}
}
]
3. Check detection and lock in the playground
- Drive the vehicle and turn the radar on. Bring an aircraft into the scan area and compare its displayed bearing, distance, and altitude. Turn radar off and check that the list disappears. Remote-player replicas do not scan on the local client.
TryLock,Unlock,CycleTarget, andSwitchModeoperate on the locally controlled vehicle.TryLockneeds a contact on the radar scope and still takes the normal acquisition time. The script prints a[Radar]entry when the lock is acquired or lost.
The MCP replay checks configuration, script binding, the missing-radar case, chaff display, expired rows, and list hiding when the radar is inactive. Detection and locking need an in-game scene.
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 radar, aircraft = {}, {}\nlocal count, text, updating, active = 2, nil, true, true\nE.CS = { UnityEngine = { GUI = { Label = function(_, value) text = value end },\n Rect = function(...) return { ... } end } }\nE.VehicleAPI = { IsLocalPlayer = function(v) return v.localPlayer end,\n GetDisplayName = function(_) return \"Example\" end }\nE.RadarAPI = {\n TryGetRadar = function(v) return v.hasRadar, v.hasRadar and radar or nil end,\n OnLockChanged = function(owner, r, cb) assert(owner and r == radar and cb) end,\n IsActive = function(_) return active end,\n GetContactCount = function(_) return count end,\n GetContact = function(r, i)\n assert(r == radar and i >= 0 and i <= 1)\n if i == 0 then return true, aircraft, 2500, 45, 2, 500, 40, true end\n return true, nil, 3100, 50, 2, 600, 35, false\n end,\n GetContactWorldAzimuth = function(_, i) return i == 0 and 270 or 280 end,\n}\n\nlocal B = assert(loadfile('../../Docs/Manual/tutorials/lua-api2-radar-screen/example.lua', 't', E))()\nlocal function instance(t) return setmetatable(t, { __index = B }) end\nlocal script = { SetUpdateEnabled = function(_, enabled) updating = enabled end,\n SetUpdateInterval = function(_, interval) assert(interval == 0.2) end }\nlocal unavailable = instance({ script = script })\nunavailable:OnVehicleReady({})\nunavailable:OnUpdated()\nassert(updating == false and not unavailable.visible)\nlocal sample = instance({ script = script })\nsample:OnVehicleReady({ hasRadar = true, localPlayer = true })\nsample:OnUpdated()\nsample:OnGUI()\nassert(string.find(text, \"Example 2.5 km 270\", 1, true))\nassert(string.find(text, \"? 3.1 km 280\", 1, true))\ncount = 1\nsample:OnUpdated()\nassert(not string.find(sample.text, \"?\", 1, true))\nactive = false\nsample:OnUpdated()\nassert(not sample.visible)\nlocal ok = CS.ShanghaiWindy.Core.API.RadarAPI.TryGetRadar(nil)\nassert(ok == false)\nreturn \"Lua example passed\"\n",
"manual-lua-api2-radar-screen",
"null"
]
},
"expect": {
"contains": "Lua example passed"
}
}
]