deea7d2a56
Addresses review feedback on the Unity 6.5 GetInstanceID migration: - UnityObjectIdCompatExtensions: drop the reflective method lookup in favor of a simple #if UNITY_6000_5_OR_NEWER / #else split calling GetEntityId() or GetInstanceID() directly. Also wrap the class in the MCPForUnity.Runtime.Helpers namespace. - UnityTypeConverters: remove the reflective EntityIdToObject probe and call EditorUtility.EntityIdToObject(EntityId) directly under the same version gate. Serialize entityID as EntityId.ToULong() rather than ToString(), since Unity's docs explicitly warn that the textual form is not a stable serialization contract. - Drop #pragma warning disable 0619 from 22 files that no longer make any direct calls to obsolete APIs. The remaining 7 files still need it (FindObjectsOfType, InstanceIDToObject fallback) and are left as-is — those deprecations are out of scope for this PR. - Add the MCPForUnity.Runtime.Helpers using to every file that calls GetInstanceIDCompat() now that the extension method lives in a namespace.
50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
#nullable disable
|
|
using System.Collections.Generic;
|
|
using MCPForUnity.Editor.Helpers;
|
|
using Newtonsoft.Json.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using MCPForUnity.Runtime.Helpers;
|
|
|
|
namespace MCPForUnity.Editor.Tools.GameObjects
|
|
{
|
|
internal static class GameObjectDelete
|
|
{
|
|
internal static object Handle(JToken targetToken, string searchMethod)
|
|
{
|
|
List<GameObject> targets = ManageGameObjectCommon.FindObjectsInternal(targetToken, searchMethod, true);
|
|
|
|
if (targets.Count == 0)
|
|
{
|
|
return new ErrorResponse($"Target GameObject(s) ('{targetToken}') not found using method '{searchMethod ?? "default"}'.");
|
|
}
|
|
|
|
List<object> deletedObjects = new List<object>();
|
|
foreach (var targetGo in targets)
|
|
{
|
|
if (targetGo != null)
|
|
{
|
|
string goName = targetGo.name;
|
|
int goId = targetGo.GetInstanceIDCompat();
|
|
// Note: Undo.DestroyObjectImmediate doesn't work reliably in test context,
|
|
// so we use Object.DestroyImmediate. This means delete isn't undoable.
|
|
// TODO: Investigate Undo.DestroyObjectImmediate behavior in Unity 2022+
|
|
Object.DestroyImmediate(targetGo);
|
|
deletedObjects.Add(new { name = goName, instanceID = goId });
|
|
}
|
|
}
|
|
|
|
if (deletedObjects.Count > 0)
|
|
{
|
|
string message =
|
|
targets.Count == 1
|
|
? $"GameObject '{((dynamic)deletedObjects[0]).name}' deleted successfully."
|
|
: $"{deletedObjects.Count} GameObjects deleted successfully.";
|
|
return new SuccessResponse(message, deletedObjects);
|
|
}
|
|
|
|
return new ErrorResponse("Failed to delete target GameObject(s).");
|
|
}
|
|
}
|
|
}
|