Skip to content

Commit 264b585

Browse files
dsarnoclaude
andcommitted
fix: address CodeRabbit feedback on resource leaks and shader safety
- Fix material creation to handle missing shaders (URP/HDRP fallbacks) - Add try/finally blocks for RenderTexture resource management - Fix Python closure variable binding (ruff B023) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 290c913 commit 264b585

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

UnityMcpBridge/Editor/Tools/ManageAsset.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,18 @@ private static object CreateAsset(JObject @params)
179179
}
180180
else if (lowerAssetType == "material")
181181
{
182-
Material mat = new Material(Shader.Find("Standard")); // Default shader
183-
// TODO: Apply properties from JObject (e.g., shader name, color, texture assignments)
182+
// Prefer provided shader; fall back to common pipelines
183+
var requested = properties?["shader"]?.ToString();
184+
Shader shader =
185+
(!string.IsNullOrEmpty(requested) ? Shader.Find(requested) : null)
186+
?? Shader.Find("Universal Render Pipeline/Lit")
187+
?? Shader.Find("HDRP/Lit")
188+
?? Shader.Find("Standard")
189+
?? Shader.Find("Unlit/Color");
190+
if (shader == null)
191+
return Response.Error($"Could not find a suitable shader (requested: '{requested ?? "none"}').");
192+
193+
var mat = new Material(shader);
184194
if (properties != null)
185195
ApplyMaterialProperties(mat, properties);
186196
AssetDatabase.CreateAsset(mat, fullPath);
@@ -1261,24 +1271,29 @@ private static object GetAssetData(string path, bool generatePreview = false)
12611271
{
12621272
// Ensure texture is readable for EncodeToPNG
12631273
// Creating a temporary readable copy is safer
1264-
RenderTexture rt = RenderTexture.GetTemporary(
1265-
preview.width,
1266-
preview.height
1267-
);
1268-
Graphics.Blit(preview, rt);
1274+
RenderTexture rt = null;
1275+
Texture2D readablePreview = null;
12691276
RenderTexture previous = RenderTexture.active;
1270-
RenderTexture.active = rt;
1271-
Texture2D readablePreview = new Texture2D(preview.width, preview.height);
1272-
readablePreview.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
1273-
readablePreview.Apply();
1274-
RenderTexture.active = previous;
1275-
RenderTexture.ReleaseTemporary(rt);
1276-
1277-
byte[] pngData = readablePreview.EncodeToPNG();
1277+
try
1278+
{
1279+
rt = RenderTexture.GetTemporary(preview.width, preview.height);
1280+
Graphics.Blit(preview, rt);
1281+
RenderTexture.active = rt;
1282+
readablePreview = new Texture2D(preview.width, preview.height);
1283+
readablePreview.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
1284+
readablePreview.Apply();
1285+
}
1286+
finally
1287+
{
1288+
RenderTexture.active = previous;
1289+
if (rt != null) RenderTexture.ReleaseTemporary(rt);
1290+
}
1291+
1292+
var pngData = readablePreview.EncodeToPNG();
12781293
previewBase64 = Convert.ToBase64String(pngData);
12791294
previewWidth = readablePreview.width;
12801295
previewHeight = readablePreview.height;
1281-
UnityEngine.Object.DestroyImmediate(readablePreview); // Clean up temp texture
1296+
UnityEngine.Object.DestroyImmediate(readablePreview);
12821297
}
12831298
catch (Exception ex)
12841299
{

UnityMcpBridge/UnityMcpServer~/src/tools/manage_script_edits.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ def line_col_from_index(idx: int) -> Tuple[int, int]:
630630
if not m:
631631
continue
632632
# Expand $1, $2... in replacement using this match
633-
def _expand_dollars(rep: str) -> str:
634-
return _re.sub(r"\$(\d+)", lambda g: m.group(int(g.group(1))) or "", rep)
633+
def _expand_dollars(rep: str, _m=m) -> str:
634+
return _re.sub(r"\$(\d+)", lambda g: _m.group(int(g.group(1))) or "", rep)
635635
repl = _expand_dollars(text_field)
636636
sl, sc = line_col_from_index(m.start())
637637
el, ec = line_col_from_index(m.end())
@@ -767,8 +767,8 @@ def line_col_from_index(idx: int) -> Tuple[int, int]:
767767
if not m:
768768
continue
769769
# Expand $1, $2... backrefs in replacement using the first match (consistent with mixed-path behavior)
770-
def _expand_dollars(rep: str) -> str:
771-
return _re.sub(r"\$(\d+)", lambda g: m.group(int(g.group(1))) or "", rep)
770+
def _expand_dollars(rep: str, _m=m) -> str:
771+
return _re.sub(r"\$(\d+)", lambda g: _m.group(int(g.group(1))) or "", rep)
772772
repl_expanded = _expand_dollars(repl)
773773
# Let C# side handle validation using Unity's built-in compiler services
774774
sl, sc = line_col_from_index(m.start())

0 commit comments

Comments
 (0)