Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 · script.objectformat

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.

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

problem with wheelcollider 0 Answers

Script on Prefab is not changeable without changing all other instaniated objects 1 Answer

The position of the object and its changes! Help me can not understand! 0 Answers

How do I show a custom dynamic string instead of the script name of a MonoBehaviour component in the Inspector? 1 Answer

Rotate with script and point 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges