x


Get path of editor script?

I'm writing an editor script which needs to save values to a text file, and I need the path of that script so that I can save the text file in the same folder.

I have tried the following with no luck:

private Object thisScript;
private string thePath;

void OnEnable ()
{
    thisScript = FindObjectOfType(typeof(myScript)) as myScript;

    thePath = AssetDatabase.GetAssetPath( thisScript );    // Returns null
    thePath = AssetDatabase.GetAssetPath( this );    // Returns null as well.

    Debug.Log("thePath = " + thePath);

}

Now Unity already shows me the path of this script in the Debug window, when the Debug.Log() appears, telling me where the Log came from, so it knows where it is, but I can't access it...I feel like a complete idiot right now!

Thanks for your time guys, any help will be very appreciated as always! Stephane

more ▼

asked May 19 '12 at 09:55 PM

ronronmx gravatar image

ronronmx
806 84 101 122

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You tried to get an asset path from an instance of a normal .NET / Mono class which of course doesn't work ;) The class / type that is represented by a MonoScript is not an asset, it's just a mono class. You need to use MonoScript.FromScriptableObject to get the MonoScript asset that contains your desired type. Note that the types Editor (for custom inspectors) and EditorWindow are derived from ScriptableObject.

To get the MonoScript of a "normal" MonoBehaviour class, you would use MonoScript.FromMonoBehaviour

As final hint: The type MonoScript is derived from TextAsset, so your script is just a text file from Unity's point of view. However when the MonoScript asset is compiled, you can get the System.Type object for the class that is represented by this script with MonoScript.GetClass.

more ▼

answered May 20 '12 at 12:39 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

I couldn't have asked for a better explanation, thanks a lot for your help and time :)

May 20 '12 at 05:05 AM ronronmx

var comp = gameObject.GetComponent<YourComponent>(); // gets your component (attached to gameObject)

var script = MonoScript.FromMonoBehaviour(comp); // gets script as an asset

AssetDatabase.OpenAsset(script); // opens script in your predefined script editor

// Danko ;)

Nov 03 '12 at 11:17 AM dkozar
(comments are locked)
10|3000 characters needed characters left

Hi I'm trying to do the same thing .. Did you get this to work? What was the code you ended up using?

Thanks!

more ▼

answered Oct 28 '12 at 10:36 AM

immunitystudios gravatar image

immunitystudios
1

Actually, I ended up not needing to do this after all, so I don't really have any example to show you, sorry.

But from the answer above that Bunny83 gave me, it looks like you would do something like this:

var script = MonoScript.FromScriptableObject( this );
var path = AssetDatabase.GetAssetPath( script );

Anyone, please correct me if I'm wrong! I can't test the code above atm, so I can't tell if it's gonna work or not.

Oct 29 '12 at 05:53 PM ronronmx

Awesome! Worked great! =)

Oct 30 '12 at 12:02 AM immunitystudios

Cool! Glad it worked

Nov 21 '12 at 10:30 PM ronronmx
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5098
x420
x349
x232

asked: May 19 '12 at 09:55 PM

Seen: 1057 times

Last Updated: Nov 21 '12 at 10:30 PM