Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by ronronmx · May 19, 2012 at 09:55 PM · editor-scriptingassetdatabase

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by Bunny83 · May 20, 2012 at 12:39 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · May 20, 2012 at 05:05 AM 0
Share

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

avatar image dkozar · Nov 03, 2012 at 11:17 AM 1
Share

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

var script = $$anonymous$$onoScript.From$$anonymous$$onoBehaviour(comp); // gets script as an asset

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

// Danko ;)

avatar image
0

Answer by immunitystudios · Oct 28, 2012 at 10:36 AM

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

Thanks!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Oct 29, 2012 at 05:53 PM 0
Share

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 = $$anonymous$$onoScript.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.

avatar image immunitystudios · Oct 30, 2012 at 12:02 AM 0
Share

Awesome! Worked great! =)

avatar image ronronmx · Nov 21, 2012 at 10:30 PM 0
Share

Cool! Glad it worked

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting oracle with unity tutorial, Steps or a way? 1 Answer

How to assign variables like Texture2D, TextAsset, Transform, etc. using code? 1 Answer

Resources.FindObjectsOfTypeAll() doesn't return all Textures 2 Answers

Programmatically Exporting an Asset Package 4 Answers

can i load and use in runtime unity game ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges