Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Open a script in your game? (Advanced Topic)

Discussion in 'Scripting' started by Rick-Gamez, Apr 19, 2015.

  1. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    My game is actually a script writing program that is a little different than most. So far I have it compiling the first few lines of a C# script complete with a Start event (if you add one). This program is still in the primitive stages of dev but anyway It then saves a C# code file that can import into unity. and be attached to your game objects. This all works fine, I can even open my program with unity as its code editor...

    But how would this have to work in reverse to open a file?

    For Example when unity opens monodevelop what info is being sent to monodevelop to tell it what script to open? And how would Monodevelop receive this data in order to know what to do?...

    Thanks in advance.
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Same way open with works for most things, its calling mono than providing the path to the file to open as a argument.
     
  3. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Thanks, that certainly helps some but, what I need to know is how would I be able to access this data path so I could say add it to a dictionary or list? My program opens multiple tabs (or will be able to) so I will need to keep some sort of association for this info. I can probably figure out the stream reader part on my own.

    It is also worth noting that I have never worked with anything that opens a file when told to by another program so I'm pretty much a noob on this topic.

    And sorry for my being unclear (speaking is not my strong suit)

    thanks again
     
  4. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    >what I need to know is how would I be able to access this data path
    If you know class name but not sure where it is placed, you can scan Assets hierarchy and find file with Class_name + .cs. Usually files have the same name as types in it.
     
  5. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    >If you know class name but not sure where it is placed, you can scan Assets hierarchy and find file with Class_name + .cs. Usually files have the same name as types in it.<

    I will try to make some sense out of this but if I'm understanding correctly, then when the path is sent its actually creating an asset in my program?(if so this will make it easier) Or am I mixed up?

    I can only open a built version of my editor with unity so I can't see the hierarchy.
     
    Last edited: Apr 19, 2015
  6. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    In addition to exporting a script I have added a function that will export a .txt file complete with code tags (this will make it easier for the user to post their script on these forums) I still need to get the above reply answered but on this reply I am just testing that function out and I'm sorry for taking up space (but this needs to be tested :) )

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6.  
    7. public class thisIsATest : MonoBehaviour
    8. {
    9.     void start()
    10.     {
    11.  
    12.     }
    13.  
    14. }
    15.  
    And it Works!!!

    I also fixed it so it capitalizes Start (oops typo I'm glad I tested...)
     
  7. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    >when the path is sent its actually creating an asset in my program?

    You are absolutely right! I thought you speak about Editor extension. You can not know where class file was after compilation. After compilation all C# types will lay to
    Assembly-CSharp.dll and Assembly-CSharp-firstpass.dll files according to they folder in Project tab. If you wish to search the assembly where the type was added try this snippet:

    Code (csharp):
    1.  
    2. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
    3. {
    4.    foreach (System.Type type in assembly.GetTypes())
    5.    {
    6.      Debug.Log(type + " -- " + assembly.FullName); // print names of all types in all project assemblies
    7.    }
    8. }
    9.  
     
    Last edited: Apr 20, 2015
  8. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Thanks for your reply I will test this as soon as I have had enough coffee :) . One other question say I wanted to add different language settings(code languages I mean) This way it can compile JS. But would .js files be found the same way? An uh what if I placed a HaXe file in unity would unity know how to run it if I ever wanted to include haxe settings with my program? (Sorry this part of coding is like the dark side of the moon to me lol I am self taught which is probably why)Not to mention I have only been coding with CS for like 2 months >.>

    thanks again!!!
     
    Last edited: Apr 20, 2015
  9. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
  10. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    This compiler uses snap together blocks to write the code and will be a stand alone app that spits out a CS file or whatever that can be imported into other programs such as unity. So its not a text based compiler at all though I may add a text editor at some point (I already have the text values anyway right?)

    All I am trying to do here is have the program find the path that it was told to open when you double click a script so I can then tell it to read the file at that location. I'm just not sure how to bridge that one gap.
     
  11. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Basically when you double click a script info is sent to the compiler telling it what path to find the file at I just need to know how to tell my program to recieve the data then put that data into a variable so I know how to tell the program where to find it.

    after that I will use a stream reader to get the data from the file at the said location.
     
  12. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    I am sorry, I still have not understood clearly what are you trying to do :)

    >I just need to know how to tell my program to receive the data then put that data into a variable so I know how to tell the program where to find it.

    Where you click to script?
    a) If in file browser you can add file association with your own program. After windows (or another OS) starts yourprogram.exe \\path\yuor_script.cs you can get path to script from ARGV array, take a look to this doc http://www.dotnetperls.com/main

    b) if you are clicking to file in your own control which shows a list of files in some directory. So you have to add some piece of code to this control to achieve from him some extra info you need after they catch OnClick event
     
  13. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    I will Check out that link. But let me try to simplify the idea here:

    1) you are in unity and looking at a script that you want to open.

    2) you double click the script...

    3)unity opens mono develop

    4) mono develop opens the script you clicked on

    ???/ How does mono develop know what script was clicked? so it can open it???

    ***My program has to pretty much do the same thing***(or at least something close)***
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The code in a Unity projects gets put into three different assemblies, one for CS, one for JS, and one for BOO. Each of those has a firstpass dll, which refers to the code you put in the plugins folder, a editor dll, which refers to the code you put in an editor folder, and a main dll which is everything else. I've seen others too, but that's the gist of it.

    When you select a external program to open your scripts, it probably gets sent the .snl file in your project root, which is the file that contains all of the metadata about your .Net project. In addition, it would get some command line arguments describing which file and line number that should be selected.

    If you really want to know, create an .exe that prints whatever command line arguments it gets to a log, and point Unity to that as it's script editing program. That'll probably give you some interesting output.
     
  15. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    >How does mono develop know what script was clicked? so it can open it???

    1) Any program may be run with arguments, and MonoDevelop too.

    For example if you run this command
    "c:\Program Files\Unity5\MonoDevelop\MonoDevelop.lnk" "d:\project\script.cs"

    MonoDevelop will start and open script.cs

    2) Please take a look at Unity menu Edit / Preference / External Tools / External Script Editor

    You can put in that field any program you like, for example I use Notepad++

    3) If you double click on a script in the Project tab, Unity will:

    a) get the string from Edit / Preference / External Tools / External Script Editor
    b) add to this string the full system path to the script which was clicked
    c) use the resulting string as the command for the operational system, so the program was used in this command will start as I mentioned in the gap 1) with path to the script as an argument

    So, you can add your proram to Edit / Preference / External Tools / External Script Editor or somewhat else start it with the path to the desired script as an argument. After start program have to parse input arguments and open the desired script.
     
    Last edited: Apr 25, 2015
  16. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Exactly what @KEMBL said and what I stated in the first reply. No black magic here in fact it is the same way operating systems are able to launch a program that automatically opens a file when you double click a file in explorer or finder.

    All executables got a main function that accepts a array of argumeants, the standard for most applications that act on a file, is to take the first argumeant passed in and treat it like a path to a file to open. This is what unity and mono are doing, except that mono and visual studio accept more argumesnts to better describe the file, such as the proper .SLN file and information about what line number to open the file at.
     
    KEMBL likes this.