Skip to content

Commit fc0c01b

Browse files
committed
Refactor validation logic in GameObject.Component.Add and GameObject.Component.Modify to improve error handling
1 parent 8be5c61 commit fc0c01b

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

Unity-MCP-Plugin/Assets/root/Editor/Scripts/API/Tool/GameObject.Component.Add.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ GameObjectRef gameObjectRef
4444
if (!gameObjectRef.IsValid(out var gameObjectValidationError))
4545
throw new ArgumentException(gameObjectValidationError, nameof(gameObjectRef));
4646

47+
if (componentNames == null)
48+
throw new ArgumentNullException(nameof(componentNames), "No component names provided.");
49+
50+
if (componentNames.Length == 0)
51+
throw new ArgumentException("No component names provided.", nameof(componentNames));
52+
4753
return MainThread.Instance.Run(() =>
4854
{
4955
var go = gameObjectRef.FindGameObject(out var error);
@@ -53,12 +59,6 @@ GameObjectRef gameObjectRef
5359
if (go == null)
5460
throw new Exception("GameObject not found.");
5561

56-
if (componentNames == null)
57-
throw new ArgumentNullException(nameof(componentNames), "No component names provided.");
58-
59-
if (componentNames.Length == 0)
60-
throw new ArgumentException("No component names provided.", nameof(componentNames));
61-
6262
var response = new AddComponentResponse();
6363

6464
foreach (var componentName in componentNames)

Unity-MCP-Plugin/Assets/root/Editor/Scripts/API/Tool/GameObject.Component.Modify.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public ModifyComponentResponse ModifyComponent
4444
SerializedMember componentDiff
4545
)
4646
{
47-
return MainThread.Instance.Run(() =>
48-
{
49-
if (!gameObjectRef.IsValid(out var gameObjectValidationError))
50-
throw new ArgumentException(gameObjectValidationError, nameof(gameObjectRef));
47+
if (!gameObjectRef.IsValid(out var gameObjectValidationError))
48+
throw new ArgumentException(gameObjectValidationError, nameof(gameObjectRef));
5149

52-
if (!componentRef.IsValid(out var componentValidationError))
53-
throw new ArgumentException(componentValidationError, nameof(componentRef));
50+
if (!componentRef.IsValid(out var componentValidationError))
51+
throw new ArgumentException(componentValidationError, nameof(componentRef));
5452

53+
return MainThread.Instance.Run(() =>
54+
{
5555
var go = gameObjectRef.FindGameObject(out var error);
5656
if (error != null)
5757
throw new Exception(error);

Unity-MCP-Plugin/Assets/root/Editor/Scripts/API/Tool/GameObject.Modify.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public Logs? Modify
4444
SerializedMemberList gameObjectDiffs
4545
)
4646
{
47-
return MainThread.Instance.Run(() =>
48-
{
49-
if (gameObjectRefs.Count == 0)
50-
throw new Exception("No GameObject references provided. Please provide at least one GameObject reference.");
47+
if (gameObjectRefs.Count == 0)
48+
throw new ArgumentException("No GameObject references provided. Please provide at least one GameObject reference.", nameof(gameObjectRefs));
5149

52-
if (gameObjectDiffs.Count != gameObjectRefs.Count)
53-
throw new Exception($"The number of {nameof(gameObjectDiffs)} and {nameof(gameObjectRefs)} should be the same. " +
54-
$"{nameof(gameObjectDiffs)}: {gameObjectDiffs.Count}, {nameof(gameObjectRefs)}: {gameObjectRefs.Count}");
50+
if (gameObjectDiffs.Count != gameObjectRefs.Count)
51+
throw new ArgumentException($"The number of {nameof(gameObjectDiffs)} and {nameof(gameObjectRefs)} should be the same. " +
52+
$"{nameof(gameObjectDiffs)}: {gameObjectDiffs.Count}, {nameof(gameObjectRefs)}: {gameObjectRefs.Count}", nameof(gameObjectDiffs));
5553

54+
return MainThread.Instance.Run(() =>
55+
{
5656
var logs = new Logs();
5757

5858
for (int i = 0; i < gameObjectRefs.Count; i++)

0 commit comments

Comments
 (0)