Search Unity

Start the edition of a script programatically

Discussion in 'Immediate Mode GUI (IMGUI)' started by luciano182, Nov 20, 2016.

  1. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
    Hi! I'm developing an extension of Unity, but I'm having a problem.

    I need to open any script (inside the editor) programatically, and that script should be openned in the mono/msvs project, exactly like when you double click a script.

    I've tried using "EditorUtility.OpenWithDefaultApp(fileName);" but it opens the individual file, not inside the project.

    Anyone could solve this?

    Regards!
     
  2. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    AssetDatabase.OpenAsset() works for me, though I have to use MonoScript.FromMonoBehavior() or MonoScript.FromScriptableObject() to get an appropriate object to pass to OpenAsset(). Which means that I have to actually have an instance of the script available. I've tried to figure out how to go from script filename to a MonoScript instance, but I don't recall having succeeded.

    Hmm, a little bit of research suggests that AssetDatabase.LoadMainAssetFromPath() would also return a valid object to pass to OpenAsset(). So maybe try the following:

    Code (CSharp):
    1. AssetDatabase.OpenAsset(AssetDatabase.LoadMainAssetFromPath(fileName));
     
    luciano182 likes this.
  3. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
    Thank youuuu! I didn't know how to look for it! Thanksss :D