Skip to main content
Glama
PropertyCommand.cs3.94 kB
using System; using System.Reflection; using UnityEngine; using UnityMCP.Client.Editor.Logging; namespace UnityMCP.Client.Editor.Commands { /// <summary> /// Command for getting a property of an object /// </summary> public class PropertyCommand : CommandBase { private readonly string _objectVariableName; private readonly string _propertyName; /// <summary> /// Create a new PropertyCommand /// </summary> /// <param name="objectVariableName">The variable name of the object to get the property from</param> /// <param name="propertyName">The name of the property to get</param> /// <param name="resultVariableName">The variable name to store the result in, if any</param> public PropertyCommand(string objectVariableName, string propertyName, string resultVariableName = null) : base(resultVariableName) { _objectVariableName = objectVariableName; _propertyName = propertyName; } /// <summary> /// Execute the command and return the result /// </summary> /// <param name="context">The command context</param> /// <returns>The result of the command execution</returns> public override object Execute(CommandContext context) { // Get the object from the context object obj = context.GetVariable(_objectVariableName); if (obj == null) { Debug.LogError($"[Unity MCP] Object '{_objectVariableName}' not found in context"); return null; } // Get the property value using reflection try { // Get the property info PropertyInfo propertyInfo = obj.GetType().GetProperty(_propertyName); if (propertyInfo == null) { // Try to get it as a field FieldInfo fieldInfo = obj.GetType().GetField(_propertyName); if (fieldInfo == null) { Debug.LogError($"[Unity MCP] Property or field '{_propertyName}' not found on object '{_objectVariableName}'"); return null; } // Get the field value object result = fieldInfo.GetValue(obj); // Log the result Debug.Log($"[Unity MCP] Got field '{_propertyName}' from object '{_objectVariableName}': {result}"); // Store the result in the context if a variable name was provided if (!string.IsNullOrEmpty(ResultVariableName)) { context.SetVariable(ResultVariableName, result); } // Return the result return result; } // Get the property value object propertyValue = propertyInfo.GetValue(obj); // Log the result Debug.Log($"[Unity MCP] Got property '{_propertyName}' from object '{_objectVariableName}': {propertyValue}"); Logging.AILoggerStatic.Log("unity-property-result", new { property = _propertyName, value = propertyValue?.ToString() }); // Store the result in the context if a variable name was provided if (!string.IsNullOrEmpty(ResultVariableName)) { context.SetVariable(ResultVariableName, propertyValue); } // Return the property value return propertyValue; } catch (Exception ex) { Debug.LogError($"[Unity MCP] Error getting property '{_propertyName}' from object '{_objectVariableName}': {ex.Message}"); return null; } } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TSavo/Unity-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server