using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Reporting;
using Newtonsoft.Json.Linq;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq; // Add LINQ namespace for Select extension method
using System.Globalization;
///
/// Handles editor control commands like undo, redo, play, pause, stop, and build operations.
///
public static class EditorControlHandler
{
///
/// Handles editor control commands
///
public static object HandleEditorControl(JObject @params)
{
string command = (string)@params["command"];
JObject commandParams = (JObject)@params["params"];
switch (command.ToUpper())
{
case "UNDO":
return HandleUndo();
case "REDO":
return HandleRedo();
case "PLAY":
return HandlePlay();
case "PAUSE":
return HandlePause();
case "STOP":
return HandleStop();
case "BUILD":
return HandleBuild(commandParams);
case "EXECUTE_COMMAND":
return HandleExecuteCommand(commandParams);
case "READ_CONSOLE":
return ReadConsole(commandParams);
default:
return new { error = $"Unknown editor control command: {command}" };
}
}
private static object HandleUndo()
{
Undo.PerformUndo();
return new { message = "Undo performed successfully" };
}
private static object HandleRedo()
{
Undo.PerformRedo();
return new { message = "Redo performed successfully" };
}
private static object HandlePlay()
{
if (!EditorApplication.isPlaying)
{
EditorApplication.isPlaying = true;
return new { message = "Entered play mode" };
}
return new { message = "Already in play mode" };
}
private static object HandlePause()
{
if (EditorApplication.isPlaying)
{
EditorApplication.isPaused = !EditorApplication.isPaused;
return new { message = EditorApplication.isPaused ? "Game paused" : "Game resumed" };
}
return new { message = "Not in play mode" };
}
private static object HandleStop()
{
if (EditorApplication.isPlaying)
{
EditorApplication.isPlaying = false;
return new { message = "Exited play mode" };
}
return new { message = "Not in play mode" };
}
private static object HandleBuild(JObject @params)
{
string platform = (string)@params["platform"];
string buildPath = (string)@params["buildPath"];
try
{
BuildTarget target = GetBuildTarget(platform);
if ((int)target == -1)
{
return new { error = $"Unsupported platform: {platform}" };
}
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = GetEnabledScenes();
buildPlayerOptions.target = target;
buildPlayerOptions.locationPathName = buildPath;
BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
return new
{
message = "Build completed successfully",
summary = report.summary
};
}
catch (System.Exception e)
{
return new { error = $"Build failed: {e.Message}" };
}
}
private static object HandleExecuteCommand(JObject @params)
{
string commandName = (string)@params["commandName"];
try
{
EditorApplication.ExecuteMenuItem(commandName);
return new { message = $"Executed command: {commandName}" };
}
catch (System.Exception e)
{
return new { error = $"Failed to execute command: {e.Message}" };
}
}
///
/// Reads log messages from the Unity Console
///
/// Parameters containing filtering options
/// Object containing console messages filtered by type
public static object ReadConsole(JObject @params)
{
// Default values for show flags
bool showLogs = true;
bool showWarnings = true;
bool showErrors = true;
string searchTerm = string.Empty;
// Get filter parameters if provided
if (@params != null)
{
if (@params["show_logs"] != null) showLogs = (bool)@params["show_logs"];
if (@params["show_warnings"] != null) showWarnings = (bool)@params["show_warnings"];
if (@params["show_errors"] != null) showErrors = (bool)@params["show_errors"];
if (@params["search_term"] != null) searchTerm = (string)@params["search_term"];
}
try
{
// Get required types and methods via reflection
Type logEntriesType = Type.GetType("UnityEditor.LogEntries,UnityEditor");
Type logEntryType = Type.GetType("UnityEditor.LogEntry,UnityEditor");
if (logEntriesType == null || logEntryType == null)
return new { error = "Could not find required Unity logging types", entries = new List