12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System.Linq;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace LLM.Editor.Analysis
- {
- /// <summary>
- /// Provides a list of GameObjects found in the current scene based on a query.
- /// Supports searching by name, component type, or tag.
- /// </summary>
- public class FindInSceneProvider : IContextProvider
- {
- private class SceneQueryResult
- {
- public string name;
- public string id;
- }
- public object GetContext(Object target, string qualifier)
- {
- if (string.IsNullOrEmpty(qualifier))
- {
- return "Error: A query string (qualifier) is required for FindInScene (e.g., 'name: Player', 'component: Rigidbody', 'tag: Enemy').";
- }
- var gameObjects = Object.FindObjectsOfType<GameObject>();
- var queryParts = qualifier.Split(new[] { ':' }, 2);
- var queryType = queryParts[0].Trim().ToLower();
- var queryValue = queryParts.Length > 1 ? queryParts[1].Trim() : "";
- var results = gameObjects.AsQueryable();
- switch (queryType)
- {
- case "name":
- results = results.Where(go => go.name.ToLower().Contains(queryValue.ToLower()));
- break;
- case "component":
- results = results.Where(go => go.GetComponent(queryValue));
- break;
- case "tag":
- results = results.Where(go => go.CompareTag(queryValue));
- break;
- case "layer":
- results = results.Where(go => go.layer == LayerMask.NameToLayer(queryValue));
- break;
- default:
- return $"Error: Invalid query type '{queryType}'. Use 'name', 'component', or 'tag'.";
- }
- // Return a structured result with name and instance ID for each found object.
- return results.Select(go => new SceneQueryResult { name = go.name, id = go.GetInstanceID().ToString() }).ToList();
- }
- }
- }
|