Merge pull request #131 from Sunjnn/master
Add support for creating physics material assets. Works on Unity 2022 onwards and Unity 6, since they use different naming for PhysicsMaterial(PhysicMaterial before Unity 6, and PhysicsMaterial after).
Add naming examples on the server side
TODO: currently unity-mcp only support adding gameobject and specifying adding a physic material, and manage_gameobject.cs does not handle a detailed request such as the copied request well. Will be the future work.
Example:
{
`name`: `BouncyCube`,
`action`: `create`,
`position`: [
6,
2,
0
],
`primitive_type`: `Cube`,
`components_to_add`: [
`Rigidbody`
],
`component_properties`: {
`Rigidbody`: {
`mass`: 1,
`useGravity`: true
},
`BoxCollider`: {
`material`: `Assets/Physics Materials/SuperBouncePhysicsMaterial.physicmaterial`
}
}
}
main
commit
558b051323
|
|
@ -8,6 +8,14 @@ using UnityEditor;
|
|||
using UnityEngine;
|
||||
using UnityMcpBridge.Editor.Helpers; // For Response class
|
||||
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
using PhysicsMaterialType = UnityEngine.PhysicsMaterial;
|
||||
using PhysicsMaterialCombine = UnityEngine.PhysicsMaterialCombine;
|
||||
#else
|
||||
using PhysicsMaterialType = UnityEngine.PhysicMaterial;
|
||||
using PhysicsMaterialCombine = UnityEngine.PhysicMaterialCombine;
|
||||
#endif
|
||||
|
||||
namespace UnityMcpBridge.Editor.Tools
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -177,6 +185,14 @@ namespace UnityMcpBridge.Editor.Tools
|
|||
AssetDatabase.CreateAsset(mat, fullPath);
|
||||
newAsset = mat;
|
||||
}
|
||||
else if (lowerAssetType == "physicsmaterial")
|
||||
{
|
||||
PhysicsMaterialType pmat = new PhysicsMaterialType();
|
||||
if (properties != null)
|
||||
ApplyPhysicsMaterialProperties(pmat, properties);
|
||||
AssetDatabase.CreateAsset(pmat, fullPath);
|
||||
newAsset = pmat;
|
||||
}
|
||||
else if (lowerAssetType == "scriptableobject")
|
||||
{
|
||||
string scriptClassName = properties?["scriptClass"]?.ToString();
|
||||
|
|
@ -972,6 +988,77 @@ namespace UnityMcpBridge.Editor.Tools
|
|||
return modified;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies properties from JObject to a PhysicsMaterial.
|
||||
/// </summary>
|
||||
private static bool ApplyPhysicsMaterialProperties(PhysicsMaterialType pmat, JObject properties)
|
||||
{
|
||||
if (pmat == null || properties == null)
|
||||
return false;
|
||||
bool modified = false;
|
||||
|
||||
// Example: Set dynamic friction
|
||||
if (properties["dynamicFriction"]?.Type == JTokenType.Float)
|
||||
{
|
||||
float dynamicFriction = properties["dynamicFriction"].ToObject<float>();
|
||||
pmat.dynamicFriction = dynamicFriction;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// Example: Set static friction
|
||||
if (properties["staticFriction"]?.Type == JTokenType.Float)
|
||||
{
|
||||
float staticFriction = properties["staticFriction"].ToObject<float>();
|
||||
pmat.staticFriction = staticFriction;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// Example: Set bounciness
|
||||
if (properties["bounciness"]?.Type == JTokenType.Float)
|
||||
{
|
||||
float bounciness = properties["bounciness"].ToObject<float>();
|
||||
pmat.bounciness = bounciness;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
List<String> averageList = new List<String> { "ave", "Ave", "average", "Average" };
|
||||
List<String> multiplyList = new List<String> { "mul", "Mul", "mult", "Mult", "multiply", "Multiply" };
|
||||
List<String> minimumList = new List<String> { "min", "Min", "minimum", "Minimum" };
|
||||
List<String> maximumList = new List<String> { "max", "Max", "maximum", "Maximum" };
|
||||
|
||||
// Example: Set friction combine
|
||||
if (properties["frictionCombine"]?.Type == JTokenType.String)
|
||||
{
|
||||
string frictionCombine = properties["frictionCombine"].ToString();
|
||||
if (averageList.Contains(frictionCombine))
|
||||
pmat.frictionCombine = PhysicsMaterialCombine.Average;
|
||||
else if (multiplyList.Contains(frictionCombine))
|
||||
pmat.frictionCombine = PhysicsMaterialCombine.Multiply;
|
||||
else if (minimumList.Contains(frictionCombine))
|
||||
pmat.frictionCombine = PhysicsMaterialCombine.Minimum;
|
||||
else if (maximumList.Contains(frictionCombine))
|
||||
pmat.frictionCombine = PhysicsMaterialCombine.Maximum;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// Example: Set bounce combine
|
||||
if (properties["bounceCombine"]?.Type == JTokenType.String)
|
||||
{
|
||||
string bounceCombine = properties["bounceCombine"].ToString();
|
||||
if (averageList.Contains(bounceCombine))
|
||||
pmat.bounceCombine = PhysicsMaterialCombine.Average;
|
||||
else if (multiplyList.Contains(bounceCombine))
|
||||
pmat.bounceCombine = PhysicsMaterialCombine.Multiply;
|
||||
else if (minimumList.Contains(bounceCombine))
|
||||
pmat.bounceCombine = PhysicsMaterialCombine.Minimum;
|
||||
else if (maximumList.Contains(bounceCombine))
|
||||
pmat.bounceCombine = PhysicsMaterialCombine.Maximum;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic helper to set properties on any UnityEngine.Object using reflection.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ def register_manage_asset_tools(mcp: FastMCP):
|
|||
path: Asset path (e.g., "Materials/MyMaterial.mat") or search scope.
|
||||
asset_type: Asset type (e.g., 'Material', 'Folder') - required for 'create'.
|
||||
properties: Dictionary of properties for 'create'/'modify'.
|
||||
example properties for Material: {"color": [1, 0, 0, 1], "shader": "Standard"}.
|
||||
example properties for Texture: {"width": 1024, "height": 1024, "format": "RGBA32"}.
|
||||
example properties for PhysicsMaterial: {"bounciness": 1.0, "staticFriction": 0.5, "dynamicFriction": 0.5}.
|
||||
destination: Target path for 'duplicate'/'move'.
|
||||
search_pattern: Search pattern (e.g., '*.prefab').
|
||||
filter_*: Filters for search (type, date).
|
||||
|
|
|
|||
Loading…
Reference in New Issue