-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCollisionMatrixOps.cs
More file actions
163 lines (141 loc) · 5.8 KB
/
Copy pathCollisionMatrixOps.cs
File metadata and controls
163 lines (141 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
using MCPForUnity.Editor.Helpers;
namespace MCPForUnity.Editor.Tools.Physics
{
internal static class CollisionMatrixOps
{
public static object GetCollisionMatrix(JObject @params)
{
var p = new ToolParams(@params);
string dimension = (p.Get("dimension") ?? "3d").ToLowerInvariant();
if (dimension != "3d" && dimension != "2d")
return new ErrorResponse($"Invalid dimension: '{dimension}'. Use '3d' or '2d'.");
if (dimension == "2d")
{
#if !MCP_HAS_PHYSICS_2D
return new ErrorResponse("Physics 2D module (com.unity.modules.physics2d) is not installed.");
#endif
}
else
{
#if !MCP_HAS_PHYSICS
return new ErrorResponse("Physics module (com.unity.modules.physics) is not installed.");
#endif
}
var layers = new List<object>();
var populatedIndices = new List<int>();
for (int i = 0; i < 32; i++)
{
string name = LayerMask.LayerToName(i);
if (string.IsNullOrEmpty(name)) continue;
layers.Add(new { index = i, name });
populatedIndices.Add(i);
}
var matrix = new Dictionary<string, Dictionary<string, bool>>();
foreach (int i in populatedIndices)
{
string nameA = LayerMask.LayerToName(i);
var row = new Dictionary<string, bool>();
foreach (int j in populatedIndices)
{
if (j > i) continue;
string nameB = LayerMask.LayerToName(j);
bool collides;
if (dimension == "2d")
{
#if MCP_HAS_PHYSICS_2D
collides = !Physics2D.GetIgnoreLayerCollision(i, j);
#else
return new ErrorResponse("Physics 2D module (com.unity.modules.physics2d) is not installed.");
#endif
}
else
{
#if MCP_HAS_PHYSICS
collides = !UnityEngine.Physics.GetIgnoreLayerCollision(i, j);
#else
return new ErrorResponse("Physics module (com.unity.modules.physics) is not installed.");
#endif
}
row[nameB] = collides;
}
matrix[nameA] = row;
}
return new
{
success = true,
message = $"Collision matrix retrieved ({dimension}).",
data = new { layers, matrix }
};
}
public static object SetCollisionMatrix(JObject @params)
{
var p = new ToolParams(@params);
string dimension = (p.Get("dimension") ?? "3d").ToLowerInvariant();
if (dimension != "3d" && dimension != "2d")
return new ErrorResponse($"Invalid dimension: '{dimension}'. Use '3d' or '2d'.");
var layerAToken = p.GetRaw("layer_a");
var layerBToken = p.GetRaw("layer_b");
if (layerAToken == null)
return new ErrorResponse("'layer_a' parameter is required.");
if (layerBToken == null)
return new ErrorResponse("'layer_b' parameter is required.");
int layerA = ResolveLayer(layerAToken);
int layerB = ResolveLayer(layerBToken);
if (layerA < 0 || layerA >= 32)
return new ErrorResponse($"Invalid layer_a: '{layerAToken}'. Layer not found or out of range.");
if (layerB < 0 || layerB >= 32)
return new ErrorResponse($"Invalid layer_b: '{layerBToken}'. Layer not found or out of range.");
bool collide = p.GetBool("collide", true);
if (dimension == "2d")
{
#if MCP_HAS_PHYSICS_2D
Physics2D.IgnoreLayerCollision(layerA, layerB, !collide);
MarkSettingsDirty("ProjectSettings/Physics2DSettings.asset");
#else
return new ErrorResponse("Physics 2D module (com.unity.modules.physics2d) is not installed.");
#endif
}
else
{
#if MCP_HAS_PHYSICS
UnityEngine.Physics.IgnoreLayerCollision(layerA, layerB, !collide);
MarkSettingsDirty("ProjectSettings/DynamicsManager.asset");
#else
return new ErrorResponse("Physics module (com.unity.modules.physics) is not installed.");
#endif
}
string nameA = LayerMask.LayerToName(layerA);
string nameB = LayerMask.LayerToName(layerB);
if (string.IsNullOrEmpty(nameA)) nameA = layerA.ToString();
if (string.IsNullOrEmpty(nameB)) nameB = layerB.ToString();
return new
{
success = true,
message = $"Collision between '{nameA}' and '{nameB}' set to {(collide ? "enabled" : "disabled")} ({dimension}).",
data = new { layer_a = nameA, layer_b = nameB, collide, dimension }
};
}
private static void MarkSettingsDirty(string assetPath)
{
var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
if (assets != null && assets.Length > 0)
EditorUtility.SetDirty(assets[0]);
}
private static int ResolveLayer(JToken token)
{
if (token.Type == JTokenType.Integer)
{
int idx = token.Value<int>();
return idx >= 0 && idx < 32 ? idx : -1;
}
string name = token.ToString();
if (int.TryParse(name, out int parsed))
return parsed >= 0 && parsed < 32 ? parsed : -1;
return LayerMask.NameToLayer(name);
}
}
}