• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by su_4thevision · Sep 20, 2018 at 01:02 AM · objectscript.format

Do not convert to the same format (UnityEngine.Object-> UnityEngine.Object)

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(""====================""); }} }}"; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

4.png (97.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

See camera point of view ingame 1 Answer

how to capture object? 0 Answers

How to manage an object using joints? 0 Answers

attaching the hingeJoint to the desired point 0 Answers

how to scale the transport? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges