Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Inner code editor

Discussion in 'Scripting' started by Burton-kun, Jan 13, 2016.

  1. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    Hello!
    I'm trying to insert a sort of "inner" code editor in a Editor Window of Unity.
    I know I may not be very clear but try to image Visual Studio 2015 or MonoDevelop or Sublime Text but inside a Unity Window.
    I'm working on a event system with an editor. After some ideas and different versions of this event system I decided to try this approach.
    I use this Script.cs attached to some objects, in this script there's a string variable called "instructions".
    In this variable there'll be some C# code so every object will have it's own actions to do.
    The editor makes me able to edit these actions. The script will parse the string and compile the C# code inside. The problem is: Is there a way to write those codes in the string as a real script editor does? Or maybe is there a way to open the default Unity script editor with a template.cs.txt and write there the code?
    To be clear, when I say "As a real script editor does" I mean: write some chars and the tool recognise by himself the method or the variable you're calling, auto bracket etc.
    I know this can be a little strange as question, but I never tried something like that and I'm just asking for some tips or references.
    In this moment I'm writing those codes in a simple EditorGUI textArea:
    code editor.PNG
    As you can image is very uncomfortable to write C# in there.
    How can I make this work? Thanks :)
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    can't do it that way...

    That's just creating a string in the serialized object for Script.cs. You can't just compile that... what would it compile to? Anyways, even if you did get it to compile to some dll which maybe got stored somewhere in the asset folder, the string is still getting serialized up. So you'd be bloating up your scene with string copies of the source code.

    You also can't runtime compile it, otherwise you'd lack any AOT support, and you'd have to structure some way to instantiate the code and hook into it to allow it access to the unity api... such as Update.


    IF you were to create an editor like this... it would be done a very different way. And it'd be pretty complicated. You'd most likely have it generate source code files, which really would just make the editor a proxy for existing scripts. So you'd just end up with the various unique scripts files anyways (or even if you compiled them to individual dll's... ugh... you'd end up with multiple dll files which could ONLY be edited via this editor).


    Why would you even want this, especially since Visual Studio, MonoDevelop, Sublime, and any other advanced text editor has tons of features that make coding easier than a basic text editor in the inspector.
     
  3. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    I don't actually need a script editor in Unity to replace the official script editors, I just need a way to write personal C# code in the script component of those Game Objects.
    Every object with EventScript.cs attached must have some kind of personal C# code in order to show different behaviours in game.
    The only way I can think of doing this without an Editor is to create a different script for every npc and in the long run it would be very heavy.
    I found a way to execute C# code written in string, so the only thing left is to write this C# in the string.
    That's why i thought to an editor.
    Since this moment I used to write it in a EditorGUILayout.TextArea but it's very uncomfortable and I was looking for a way to simplify this step.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    What is this way of executing C# code written in a string?

    Because I'm 99.9999% sure it's going to not be supported on AOT devices (like webgl and iOS), and will also execute slowly (since it'll have to be compiled at runtime) which will be more "heavy" than having a different script for each. Because technically you DO have a different script for each one, but you also have the added necessity of interpreting it at runtime rather than at compile time.

    Also, if EditorGUILayout.TextArea is "uncomfortable" for you... writing your own isn't going to be any more comfortable. EditorGUILayout is integrated into the unity editor API and is pretty tame in usage. I mean honestly, what's uncomfortable about it? You pass in a string, and it spits it back out with the changes. How much simpler could you get than 1 line of code?
     
  5. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    You have been very helpful, I'll try something else. Thank you.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Cheat. Build an editor extension that creates regular .cs files. Let unity compile them as per normal.