-
-
Notifications
You must be signed in to change notification settings - Fork 308
fix: [AI] includeStackTrace not working & DontDestroyOnLoad objects missing in gameobject-find #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
248c961
35548c5
dfb2e18
2c21cda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,74 @@ | ||
| /* | ||
| ┌──────────────────────────────────────────────────────────────────┐ | ||
| │ Author: Ivan Murzak (https://github.com/IvanMurzak) │ | ||
| │ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │ | ||
| │ Copyright (c) 2025 Ivan Murzak │ | ||
| │ Licensed under the Apache License, Version 2.0. │ | ||
| │ See the LICENSE file in the project root for more information. │ | ||
| └──────────────────────────────────────────────────────────────────┘ | ||
| */ | ||
| * Author: Ivan Murzak (https://github.com/IvanMurzak) | ||
| * Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) | ||
| * Copyright (c) 2025 Ivan Murzak | ||
| * Licensed under the Apache License, Version 2.0. | ||
| * See the LICENSE file in the project root for more information. | ||
| */ | ||
|
Comment on lines
+2
to
+7
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect header style |
||
|
|
||
| #nullable enable | ||
| #if UNITY_EDITOR && UNITY_6000_5_OR_NEWER | ||
| using System.Linq; | ||
| using com.IvanMurzak.McpPlugin.Common; | ||
| using UnityEngine; | ||
| using UnityEngine.SceneManagement; | ||
|
|
||
| namespace com.IvanMurzak.Unity.MCP.Runtime.Utils | ||
| { | ||
| public static partial class GameObjectUtils | ||
| { | ||
| /// <summary> | ||
| /// Find Root GameObject in opened Prefab. Of array of GameObjects in a scene. | ||
| /// Find Root GameObject in opened Prefab, active scene, and DontDestroyOnLoad. | ||
| /// </summary> | ||
| /// <param name="scene">Scene for the search, if null the current active scene would be used</param> | ||
| /// <returns>Array of root GameObjects</returns> | ||
| public static UnityEngine.GameObject[] FindRootGameObjects(Scene? scene = null) | ||
| public static GameObject[] FindRootGameObjects(Scene? scene = null) | ||
| { | ||
| var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage(); | ||
| if (prefabStage != null) | ||
| return prefabStage.prefabContentsRoot.MakeArray(); | ||
|
|
||
| GameObject[] rootGos; | ||
|
|
||
| if (scene == null) | ||
| { | ||
| var rootGos = UnityEditor.SceneManagement.EditorSceneManager | ||
| rootGos = UnityEditor.SceneManagement.EditorSceneManager | ||
| .GetActiveScene() | ||
| .GetRootGameObjects(); | ||
|
|
||
| return rootGos; | ||
| } | ||
| else | ||
| { | ||
| return scene.Value.GetRootGameObjects(); | ||
| rootGos = scene.Value.GetRootGameObjects(); | ||
| } | ||
|
|
||
| // Include DontDestroyOnLoad root objects (only available at runtime) | ||
| if (UnityEditor.EditorApplication.isPlaying) | ||
| { | ||
| var ddolRoots = Resources.FindObjectsOfTypeAll<GameObject>() | ||
| .Where(go => go.scene.name == "DontDestroyOnLoad" | ||
| && go.transform.parent == null | ||
| && (go.hideFlags == HideFlags.None || go.hideFlags == HideFlags.HideInHierarchy)) | ||
| .ToArray(); | ||
|
|
||
| if (ddolRoots.Length > 0) | ||
| { | ||
| var combined = new GameObject[rootGos.Length + ddolRoots.Length]; | ||
| rootGos.CopyTo(combined, 0); | ||
| ddolRoots.CopyTo(combined, rootGos.Length); | ||
| return combined; | ||
| } | ||
| } | ||
|
|
||
| return rootGos; | ||
| } | ||
| public static UnityEngine.GameObject? FindByInstanceID(UnityEngine.EntityId instanceID) | ||
|
|
||
| public static GameObject? FindByInstanceID(EntityId instanceID) | ||
| { | ||
| if (instanceID == UnityEngine.EntityId.None) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (instanceID == EntityId.None) | ||
| return null; | ||
|
|
||
| var obj = UnityEditor.EditorUtility.EntityIdToObject(instanceID); | ||
| if (obj is not UnityEngine.GameObject go) | ||
| if (obj is not GameObject go) | ||
| return null; | ||
|
|
||
| return go; | ||
|
Comment on lines
+11
to
74
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GameObject logic should not be included in the PR about logs stacktrace |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,14 @@ | ||
| /* | ||
| ┌──────────────────────────────────────────────────────────────────┐ | ||
| │ Author: Ivan Murzak (https://github.com/IvanMurzak) │ | ||
| │ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │ | ||
| │ Copyright (c) 2025 Ivan Murzak │ | ||
| │ Licensed under the Apache License, Version 2.0. │ | ||
| │ See the LICENSE file in the project root for more information. │ | ||
| └──────────────────────────────────────────────────────────────────┘ | ||
| */ | ||
| * Author: Ivan Murzak (https://github.com/IvanMurzak) | ||
| * Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) | ||
| * Copyright (c) 2025 Ivan Murzak | ||
| * Licensed under the Apache License, Version 2.0. | ||
| * See the LICENSE file in the project root for more information. | ||
| */ | ||
|
Comment on lines
+2
to
+7
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect header style |
||
|
|
||
| #nullable enable | ||
| #if UNITY_EDITOR && !UNITY_6000_5_OR_NEWER | ||
| using System.Linq; | ||
| using com.IvanMurzak.McpPlugin.Common; | ||
| using UnityEngine; | ||
| using UnityEngine.SceneManagement; | ||
|
|
@@ -19,7 +18,7 @@ namespace com.IvanMurzak.Unity.MCP.Runtime.Utils | |
| public static partial class GameObjectUtils | ||
| { | ||
| /// <summary> | ||
| /// Find Root GameObject in opened Prefab. Of array of GameObjects in a scene. | ||
| /// Find Root GameObject in opened Prefab, active scene, and DontDestroyOnLoad. | ||
| /// </summary> | ||
| /// <param name="scene">Scene for the search, if null the current active scene would be used</param> | ||
| /// <returns>Array of root GameObjects</returns> | ||
|
|
@@ -29,26 +28,47 @@ public static GameObject[] FindRootGameObjects(Scene? scene = null) | |
| if (prefabStage != null) | ||
| return prefabStage.prefabContentsRoot.MakeArray(); | ||
|
|
||
| GameObject[] rootGos; | ||
|
|
||
| if (scene == null) | ||
| { | ||
| var rootGos = UnityEditor.SceneManagement.EditorSceneManager | ||
| rootGos = UnityEditor.SceneManagement.EditorSceneManager | ||
| .GetActiveScene() | ||
| .GetRootGameObjects(); | ||
|
|
||
| return rootGos; | ||
| } | ||
| else | ||
| { | ||
| return scene.Value.GetRootGameObjects(); | ||
| rootGos = scene.Value.GetRootGameObjects(); | ||
| } | ||
|
|
||
| // Include DontDestroyOnLoad root objects (only available at runtime) | ||
| if (UnityEditor.EditorApplication.isPlaying) | ||
| { | ||
| var ddolRoots = Resources.FindObjectsOfTypeAll<GameObject>() | ||
| .Where(go => go.scene.name == "DontDestroyOnLoad" | ||
| && go.transform.parent == null | ||
| && (go.hideFlags == HideFlags.None || go.hideFlags == HideFlags.HideInHierarchy)) | ||
| .ToArray(); | ||
|
|
||
| if (ddolRoots.Length > 0) | ||
| { | ||
| var combined = new GameObject[rootGos.Length + ddolRoots.Length]; | ||
| rootGos.CopyTo(combined, 0); | ||
| ddolRoots.CopyTo(combined, rootGos.Length); | ||
| return combined; | ||
| } | ||
| } | ||
|
|
||
| return rootGos; | ||
| } | ||
|
|
||
| public static GameObject? FindByInstanceID(int instanceID) | ||
| { | ||
| if (instanceID == 0) | ||
| return null; | ||
|
|
||
| #if UNITY_6000_3_OR_NEWER | ||
| var obj = UnityEditor.EditorUtility.EntityIdToObject((UnityEngine.EntityId)instanceID); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| var obj = UnityEditor.EditorUtility.EntityIdToObject((EntityId)instanceID); | ||
| #else | ||
| var obj = UnityEditor.EditorUtility.InstanceIDToObject(instanceID); | ||
| #endif | ||
|
Comment on lines
8
to
74
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GameObject logic should not be included in the PR about logs stacktrace |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation has a weak quality, because it creates an array and then tries to modify after creation. It would be more efficient to use LINQ to generate the proper data using a proper query.
And ideally need to minimize the amount of places where this should be applied to 1, if that is possible.