Search Unity

Immediate Window - Execute C# snippets from the editor

Discussion in 'Assets and Asset Store' started by Deleted User, Mar 5, 2012.

  1. Deleted User

    Deleted User

    Guest

    UPDATE 7/30/2012: The Immediate Window has been slightly improved and uploaded to the Asset Store. It's still completely free, but now you can quickly and easily get at it without the Unify Wiki).
    http://u3d.as/content/nick-gravelyn/immediate-window



    Ever want to execute a little bit of code in the editor? Maybe shift a few objects around? Scale objects randomly? It's a pain to make a new script file, wire up a menu item, and then get to write the five lines of code you only want to execute once or twice. Why can't you just type in the useful bits and run your code?

    I hit this tonight so I wrote up a small Immediate Window asset, which is completely free and open source on the Unify Wiki. Here's what it looks like:


    (image missing due to Unify wiki being down)

    As you can see it's about as simple as it gets. Just a text area and a button to execute the code. It does try to be smart so if you don't change the code and keep clicking Execute it reuses the previously compiled code. So there's a little hitch on the first Execute while it compiles, but then you can click away and it runs butter smooth.

    It only works with C# scripts since I know how to compile those at runtime; if folks know how to add other languages be my guest.

    Get it here: http://unifycommunity.com/wiki/index.php?title=ImmediateWindow.

    Hope you all find it useful like I did. Please reply if you expand upon it or use it for anything cool; not required but always fun to see what people do with your code. :)
     
    Last edited by a moderator: Jul 30, 2012
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I'd find your code more interesting if you would use named parameters; I don't know what arguments are expected in all of your methods.
     
  3. Deleted User

    Deleted User

    Guest

    I'm sorry, but I'm not sure I understand you. This doesn't have any methods you need to call. Just put the script into a file, open up the Immediate Window, type some C# code, and hit Execute. This isn't a library you should be calling from other scripts.
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Example of what you have:
    Code (csharp):
    1. EditorWindow.GetWindow<ImmediateWindow>("Immediate", false)
    And clearer alternatives (the first parameter may be obvious, but what are you making false?):
    Code (csharp):
    1. EditorWindow.GetWindow<ImmediateWindow>("Immediate", focus:false)
    2. EditorWindow.GetWindow<ImmediateWindow>(title:"Immediate", focus:false)
    "Your" was perhaps a poor choice of words. I was going with the understanding that by using them, you make them yours.
     
    Last edited: Mar 5, 2012
  5. Deleted User

    Deleted User

    Guest

    Oh I see. I'm not a fan of named parameters being used just for that clarity. They're great if you mix them with optional parameters, but I think it's too much code pollution to use them just for identifying the parameter name. If you're curious, though, all the methods I call are either Unity APIs or in standard .NET libraries, so the documentation is pretty good for all of them.

    And also the false parameter in your example is for whether it's a utility window (not focus) which, while double checking the docs to make sure I knew what I was doing, I now see defaults to false so my second parameter is actually unnecessary. :p
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It's good that you work where you work, then. ;) I recommend that you don't use any more than a single letter for your own members; it will make your code cleaner. :p
     
  7. Deleted User

    Deleted User

    Guest

    Just a quick update for anyone who finds this sort of thing useful, I've slightly improved the Immediate Window and uploaded it to the Asset Store. It's still completely free, but now you can quickly and easily get at it without the Unify Wiki).
    http://u3d.as/content/nick-gravelyn/immediate-window
     
  8. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    I for one find it very useful! I have been looking for a little tool like this as I hate not being able to test some code without having to create a new script everytime...

    Quick question if you don't mind, how are you compiling/executing the code from the window? (i haven't had the chance to look at your code yet)
     
  9. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    I would be quite glad to pay for this (even in the 50-100$ range) if it was a full featured immediate window "VS 2010 immediate window style", features i would like to see are:
    - Syntax Coloring / full autocompletion
    - Defaults to monoline + enter to execute and output result while being able to toggle back to multiline
    - Works both the at design and runtime
    - Embedable in the game for script execution "in game" (super debuging tool on stand alone exe)
     
  10. Deleted User

    Deleted User

    Guest

    There are the System.CodeDom.Compiler namespace and Microsoft.CSharp namespace which have classes for compiling code dynamically. It compiles into an in-memory DLL and I then use the System.Reflection namespace to retrieve the compiled method and execute it. The code is pretty well commented so once you dig in it should be fairly straightforward to follow.

    Thanks, but I probably won't make this a paid asset. Once an asset is paid, support must be provided. I don't have lots of time to provide support so my plan is to do a couple of updates and tweaks over time, but likely nothing gigantic.
    I definitely wanted syntax coloring but couldn't figure out how to hack that into the editor UI controls. Same with autocomplete.
    I can probably add this fairly easily. I was already considering something like this for an update.
    I won't be doing runtime execution, sorry. I don't know what assemblies get bundled into games (or how to change it to include the required ones) and I personally don't find this sort of runtime execution as useful as design time. There are other assets that have in-game consoles that perform similar actions so I would recommend checking those out instead.
     
  11. insominx

    insominx

    Joined:
    Jan 23, 2012
    Posts:
    32
    This doesn't compile for me. I get " error CS0234: The type or namespace name `CSharp' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?".

    So I checked for that assembly to try and add a reference but it doesn't exist on my machine. Am I missing some part of the installation?

    Edit: It seems that it works fine outside of the Plugins folder. Question is, why can't it be relocated there?
     
    Last edited: Jan 22, 2013
  12. Deleted User

    Deleted User

    Guest

    I believe Plugins is for run time code whereas this tool requires build time assemblies, but that's just a guess. I've always kept it in a top level Editor directory.
     
  13. cesarpo

    cesarpo

    Joined:
    Jun 8, 2013
    Posts:
    97
  14. Bonfire-Boy

    Bonfire-Boy

    Joined:
    Nov 19, 2014
    Posts:
    5