Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ string[] destinationPaths
stringBuilder.AppendLine($"[Success] Copied asset from {sourcePath} to {destinationPath}.");
}
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
return stringBuilder.ToString();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ string[] paths
stringBuilder.AppendLine(Error.SourcePathIsEmpty());
continue;
}


#nullable enable
}

AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
return stringBuilder.ToString();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ string[] paths
}

AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return "[Success] Deleted assets at paths:\n" + string.Join("\n", paths);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ string shaderName
name: material.name,
logger: McpPlugin.McpPlugin.Instance.Logger
);
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
return $"[Success] Material instanceID '{material.GetInstanceID()}' created at '{assetPath}'.\n{result}";
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ SerializedMember content
// AssetDatabase.CreateAsset(asset, assetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return result.ToString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ string[] destinationPaths
}
}
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
return stringBuilder.ToString();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public string Close

StageUtility.GoBackToPreviousStage();

UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return @$"[Success] Prefab at asset path '{assetPath}' closed. " +
$"Prefab with GameObject.name '{goName}' saved: {save}.";
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public string Create
return Error.NotFoundPrefabAtPath(prefabAssetPath);

EditorUtility.SetDirty(go);
EditorApplication.RepaintHierarchyWindow();

UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

var result = McpPlugin.McpPlugin.Instance!.McpManager.Reflector.Serialize(
prefabGo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public string Instantiate
var bounds = go.CalculateBounds();

EditorUtility.SetDirty(go);
EditorApplication.RepaintHierarchyWindow();
UnityEditor.EditorApplication.RepaintProjectWindow();
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return $"[Success] Prefab successfully instantiated.\n{go.Print()}";
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ GameObjectRef gameObjectRef
.GetCustomAttribute<McpPluginToolAttribute>()
.Name;

UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return @$"[Success] Prefab '{prefabStage.assetPath}' opened. Use '{name}' to close it.
# Prefab information:
{prefabStage.prefabContentsRoot.ToMetadata()?.Print() ?? "null"}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public string Save() => MainThread.Instance.Run(() =>

PrefabUtility.SaveAsPrefabAsset(prefabGo, assetPath);

UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return @$"[Success] Prefab at asset path '{assetPath}' saved. " +
$"Prefab with GameObject.name '{goName}'.";
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace com.IvanMurzak.Unity.MCP.Editor.API
[McpPluginToolType]
public partial class Tool_Component
{
static IEnumerable<Type> AllComponentTypes => AppDomain.CurrentDomain.GetAssemblies()
public static IEnumerable<Type> AllComponentTypes => AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof(UnityEngine.Component).IsAssignableFrom(type) && !type.IsAbstract);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public string Set
Selection.instanceIDs = instanceIDs ?? new int[0];
Selection.activeInstanceID = activeInstanceID;

UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return "[Success] " + SelectionPrint;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
*/

#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Linq;
using com.IvanMurzak.McpPlugin;
using com.IvanMurzak.ReflectorNet.Utils;
using com.IvanMurzak.Unity.MCP.Runtime.Data;
using com.IvanMurzak.Unity.MCP.Runtime.Extensions;
using com.IvanMurzak.Unity.MCP.Runtime.Utils;

namespace com.IvanMurzak.Unity.MCP.Editor.API
{
Expand All @@ -27,58 +28,80 @@ public partial class Tool_GameObject
Title = "Add Component to a GameObject in opened Prefab or in a Scene"
)]
[Description("Add a component to a GameObject.")]
public string AddComponent
public AddComponentResponse AddComponent
(
[Description("Full name of the Component. It should include full namespace path and the class name.")]
string[] componentNames,
GameObjectRef gameObjectRef
)
=> MainThread.Instance.Run(() =>
{
var go = gameObjectRef.FindGameObject(out var error);
if (error != null)
return $"[Error] {error}";
return MainThread.Instance.Run(() =>
{
var go = gameObjectRef.FindGameObject(out var error);
if (error != null)
throw new Exception(error);

if (go == null)
return "[Error] GameObject not found.";
if (go == null)
throw new Exception("GameObject not found.");

if (componentNames == null)
return $"[Error] No component names provided.";
if (componentNames == null)
throw new Exception("No component names provided.");

if (componentNames.Length == 0)
return $"[Error] No component names provided.";
if (componentNames.Length == 0)
throw new Exception("No component names provided.");

var stringBuilder = new StringBuilder();
var response = new AddComponentResponse();

foreach (var componentName in componentNames)
{
var type = TypeUtils.GetType(componentName);
if (type == null)
foreach (var componentName in componentNames)
{
stringBuilder.AppendLine(Tool_Component.Error.NotFoundComponentType(componentName));
continue;
}
var type = TypeUtils.GetType(componentName);
if (type == null)
{
// try to find component with exact class name without namespace
type = Tool_Component.AllComponentTypes.FirstOrDefault(t => t.Name == componentName);
if (type == null)
{
response.Errors ??= new List<string>();
response.Errors.Add(Tool_Component.Error.NotFoundComponentType(componentName));
continue;
}
}

// Check if type is a subclass of UnityEngine.Component
if (!typeof(UnityEngine.Component).IsAssignableFrom(type))
{
stringBuilder.AppendLine(Tool_Component.Error.TypeMustBeComponent(componentName));
continue;
}
// Check if type is a subclass of UnityEngine.Component
if (!typeof(UnityEngine.Component).IsAssignableFrom(type))
{
response.Errors ??= new List<string>();
response.Errors.Add(Tool_Component.Error.TypeMustBeComponent(componentName));
continue;
}

var newComponent = go.AddComponent(type);
var newComponent = go.AddComponent(type);

if (newComponent == null)
{
stringBuilder.AppendLine($"[Warning] Component '{componentName}' already exists on GameObject or cannot be added.");
continue;
if (newComponent == null)
{
response.Errors ??= new List<string>();
response.Errors.Add($"[Warning] Component '{componentName}' already exists on GameObject or cannot be added.");
continue;
}

response.Messages ??= new List<string>();
response.Messages.Add($"[Success] Added component '{componentName}'.");

response.AddedComponents.Add(new ComponentDataShallow(newComponent));
}

stringBuilder.AppendLine($"[Success] Added component '{componentName}'. Component instanceID='{newComponent.GetInstanceID()}'.");
}
UnityEditor.EditorUtility.SetDirty(go);
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

stringBuilder.AppendLine(go.Print());
return stringBuilder.ToString();
});
return response;
});
}

public class AddComponentResponse
{
public List<ComponentDataShallow> AddedComponents { get; set; } = new List<ComponentDataShallow>();
public List<string>? Messages { get; set; }
public List<string>? Errors { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#nullable enable
using System;
using System.ComponentModel;
using com.IvanMurzak.McpPlugin;
using com.IvanMurzak.ReflectorNet.Utils;
Expand All @@ -29,7 +30,7 @@ public partial class Tool_GameObject
)]
[Description(@"Create a new GameObject at specific path.
if needed - provide proper 'position', 'rotation' and 'scale' to reduce amount of operations.")]
public string Create
public GameObjectRef Create
(
[Description("Name of the new GameObject.")]
string name,
Expand All @@ -43,53 +44,49 @@ public string Create
Vector3? scale = null,
[Description("World or Local space of transform.")]
bool isLocalSpace = false,
[Description("-1 - No primitive type; 0 - Cube; 1 - Sphere; 2 - Capsule; 3 - Cylinder; 4 - Plane; 5 - Quad.")]
int primitiveType = -1
PrimitiveType? primitiveType = null
)
=> MainThread.Instance.Run(() =>
{
if (string.IsNullOrEmpty(name))
return Error.GameObjectNameIsEmpty();

var parentGo = default(GameObject);
if (parentGameObjectRef?.IsValid ?? false)
return MainThread.Instance.Run(() =>
{
parentGo = parentGameObjectRef.FindGameObject(out var error);
if (error != null)
return $"[Error] {error}";
}
if (string.IsNullOrEmpty(name))
throw new ArgumentException(Error.GameObjectNameIsEmpty());

position ??= Vector3.zero;
rotation ??= Vector3.zero;
scale ??= Vector3.one;
var parentGo = default(GameObject);
if (parentGameObjectRef?.IsValid ?? false)
{
parentGo = parentGameObjectRef.FindGameObject(out var error);
if (error != null)
throw new Exception(error);
}

var go = primitiveType switch
{
0 => GameObject.CreatePrimitive(PrimitiveType.Cube),
1 => GameObject.CreatePrimitive(PrimitiveType.Sphere),
2 => GameObject.CreatePrimitive(PrimitiveType.Capsule),
3 => GameObject.CreatePrimitive(PrimitiveType.Cylinder),
4 => GameObject.CreatePrimitive(PrimitiveType.Plane),
5 => GameObject.CreatePrimitive(PrimitiveType.Quad),
_ => new GameObject(name)
};
go.name = name;
position ??= Vector3.zero;
rotation ??= Vector3.zero;
scale ??= Vector3.one;

var go = primitiveType != null
? GameObject.CreatePrimitive(primitiveType.Value)
: new GameObject(name);

go.name = name;

// Set parent if provided
if (parentGo != null)
go.transform.SetParent(parentGo.transform, false);
// Set parent if provided
if (parentGo != null)
go.transform.SetParent(parentGo.transform, false);

// Set the transform properties
go.SetTransform(
position: position,
rotation: rotation,
scale: scale,
isLocalSpace: isLocalSpace);
// Set the transform properties
go.SetTransform(
position: position,
rotation: rotation,
scale: scale,
isLocalSpace: isLocalSpace);

EditorUtility.SetDirty(go);
EditorApplication.RepaintHierarchyWindow();
EditorUtility.SetDirty(go);
UnityEditor.EditorApplication.RepaintHierarchyWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

return $"[Success] Created GameObject.\n{go.Print()}";
});
return new GameObjectRef(go);
});
}
}
}
Loading