Files
Marcus Sanatan 22e52664cd Asset store helper script + updated README (#521)
* Remove stray .meta file

* Add a new project that will do asset uploads

* Add asset store uploader

* refactor: Replace Debug.Log calls with McpLog helper across codebase

Standardize logging by replacing direct Debug.Log/LogWarning/LogError calls
with McpLog.Info/Warn/Error throughout helper classes and client registry.
Affected files:
- McpClientRegistry.cs
- GameObjectLookup.cs
- GameObjectSerializer.cs
- MaterialOps.cs
- McpConfigurationHelper.cs
- ObjectResolver.cs
- PropertyConversion.cs
- UnityJsonSerializer.cs
- UnityTypeResolver.cs

* feat: Add Asset Store release preparation script

Add prepare_unity_asset_store_release.py tool to automate Asset Store packaging:
- Stages temporary copy of MCPForUnity with Asset Store-specific edits
- Removes auto-popup setup window ([InitializeOnLoad] attribute)
- Renames menu entry to "Local Setup Window" for clarity
- Sets default HTTP base URL to hosted endpoint
- Defaults transport to HTTPRemote instead of HTTPLocal
- Supports dry-run mode and optional backup of existing Assets/MCPForUnity

* Show gif of MCP for Unity in Action

* Add shield with asset store link

* Update README to have asset store page unders installation section
2026-01-07 01:33:20 -04:00

94 lines
3.3 KiB
C#

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using MCPForUnity.Editor.Helpers;
using UnityEditor;
using UnityEngine;
namespace MCPForUnity.Editor.Helpers
{
/// <summary>
/// Unified property conversion from JSON to Unity types.
/// Uses UnityJsonSerializer for consistent type handling.
/// </summary>
public static class PropertyConversion
{
/// <summary>
/// Converts a JToken to the specified target type using Unity type converters.
/// </summary>
/// <param name="token">The JSON token to convert</param>
/// <param name="targetType">The target type to convert to</param>
/// <returns>The converted object, or null if conversion fails</returns>
public static object ConvertToType(JToken token, Type targetType)
{
if (token == null || token.Type == JTokenType.Null)
{
if (targetType.IsValueType && Nullable.GetUnderlyingType(targetType) == null)
{
McpLog.Warn($"[PropertyConversion] Cannot assign null to non-nullable value type {targetType.Name}. Returning default value.");
return Activator.CreateInstance(targetType);
}
return null;
}
try
{
// Use the shared Unity serializer with custom converters
return token.ToObject(targetType, UnityJsonSerializer.Instance);
}
catch (Exception ex)
{
McpLog.Error($"Error converting token to {targetType.FullName}: {ex.Message}\nToken: {token.ToString(Formatting.None)}");
throw;
}
}
/// <summary>
/// Tries to convert a JToken to the specified target type.
/// Returns null and logs warning on failure (does not throw).
/// </summary>
public static object TryConvertToType(JToken token, Type targetType)
{
try
{
return ConvertToType(token, targetType);
}
catch
{
return null;
}
}
/// <summary>
/// Generic version of ConvertToType.
/// </summary>
public static T ConvertTo<T>(JToken token)
{
return (T)ConvertToType(token, typeof(T));
}
/// <summary>
/// Converts a JToken to a Unity asset by loading from path.
/// </summary>
/// <param name="token">JToken containing asset path</param>
/// <param name="targetType">Expected asset type</param>
/// <returns>The loaded asset, or null if not found</returns>
public static UnityEngine.Object LoadAssetFromToken(JToken token, Type targetType)
{
if (token == null || token.Type != JTokenType.String)
return null;
string assetPath = AssetPathUtility.SanitizeAssetPath(token.ToString());
UnityEngine.Object loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, targetType);
if (loadedAsset == null)
{
McpLog.Warn($"[PropertyConversion] Could not load asset of type {targetType.Name} from path: {assetPath}");
}
return loadedAsset;
}
}
}