- Home /
Do not convert to the same format (UnityEngine.Object-> UnityEngine.Object)
In Unity Editor, I popped up a pop-up window and wrote an example to make the function execute in the instance. Use CSharpCodeProvider. You want to put a UnityEngine.Object into a UnityEngine.Object. An error occurs at this time. After checking the debug, UnityEngine.Object is followed by \ r.
Source code and error pictures will be uploaded. Do you have any problems with this?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using UnityEditor;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System;
public class ImmediateWindow : EditorWindow
{
private string scriptText = string.Empty;
// cache of last method we compiled so repeat executions only incur a single compilation
private MethodInfo lastScriptMethod;
// position of scroll view
private Vector2 scrollPos;
void OnGUI()
{
// start the scroll view
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
// show the script field
string newScriptText = EditorGUILayout.TextArea(scriptText);
if (newScriptText != scriptText)
{
// if the script changed, update our cached version and null out our cached method
scriptText = newScriptText;
lastScriptMethod = null;
}
// store if the GUI is enabled so we can restore it later
bool guiEnabled = GUI.enabled;
// disable the GUI if the script text is empty
GUI.enabled = guiEnabled && !string.IsNullOrEmpty(scriptText);
// show the execute button
if (GUILayout.Button("Execute"))
{
var clearType = typeof(Editor).Assembly.GetType("UnityEditor.LogEntries");
MethodInfo method = clearType.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public);
method.Invoke(null, null);
// if our script method needs compiling
if (lastScriptMethod == null)
{
// create and configure the code provider
var codeProvider = new CSharpCodeProvider();
var options = new CompilerParameters();
options.GenerateInMemory = true;
options.GenerateExecutable = false;
// bring in system libraries
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("System.Core.dll");
options.ReferencedAssemblies.Add("CSharp.dll");
foreach (var item in Selection.transforms)
{
Vector3 v =item.position;
}
// bring in Unity assemblies
options.ReferencedAssemblies.Add(typeof(EditorWindow).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(Transform).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(UnityEngine.Vector3).Assembly.Location);
// compile an assembly from our source code
var result = codeProvider.CompileAssemblyFromSource(options, string.Format(scriptFormat, scriptText));
// log any errors we got
if (result.Errors.Count > 0)
{
foreach (CompilerError error in result.Errors)
{
// the magic -11 on the line is to compensate for usings and class wrapper around the user script code.
// subtracting 11 from it will give the user the line numbers in their code.
Debug.LogError(string.Format("Immediate Compiler Error ({0}): {1}", error.Line - 11, error.ErrorText));
}
}
// otherwise use reflection to pull out our action method so we can invoke it
else
{
var type = result.CompiledAssembly.GetType("ImmediateWindowCodeWrapper");
lastScriptMethod = type.GetMethod("PerformAction", BindingFlags.Public | BindingFlags.Static);
}
}
// if we have a compiled method, invoke it
if (lastScriptMethod != null)
lastScriptMethod.Invoke(null, null);
}
// restore the GUI
GUI.enabled = guiEnabled;
// close the scroll view
EditorGUILayout.EndScrollView();
}
[MenuItem("Window/Immediate")]
static void Init()
{
// get the window, show it, and hand it focus
var window = EditorWindow.GetWindow<ImmediateWindow>("Immediate", false);
window.Show();
window.Focus();
}
// script we wrap around user entered code
private readonly string scriptFormat = @"
using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; using System.Text; using System; public static class ImmediateWindowCodeWrapper {{ public static void PerformAction() {{ Debug.Log(""====================""); // user code goes here {0}; Debug.Log(""====================""); }} }}"; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////