Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aosome23 · Dec 17, 2016 at 03:36 PM · editordirectory

Finding path(Directory) of Script from class

I know you can find the path of the script file if you have an instance: AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(instance));

However it seems odd to me that you need to have an instance of the object to get the path of the script. I need this in a static method so I don't have any reference to an object and it seems odd to just create a new instance for just finding the path.

Is there a way to do it with something like typeof(ClassName)?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaCookie · May 08, 2020 at 12:10 AM

Hi! This is an old thread, but for anyone who gets on this page, here is a solution.

Since you need an instance of a ScriptableObject, you can create an empty one through code without saving any asset, gets its source script path, and then destroy it.

To test that, create a script TestScriptableObject.cs anywhere in your project, and copy the following code:

 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 [CreateAssetMenu]
 public class TestScriptableObject : ScriptableObject
 {
     #if UNITY_EDITOR
     [MenuItem("Tests/Log TestScriptableObject script path")]
     public static void LogTestScriptableObjectPath()
     {
         // Create the asset instance
         TestScriptableObject tmpInstance = ScriptableObject.CreateInstance<TestScriptableObject>();
         // Get the script asset used by the object
         MonoScript sourceScriptAsset = MonoScript.FromScriptableObject(tmpInstance);
         // Log the script path, using AssetDatabase.GetAssetPath() as you said
         Debug.Log("Path to TestScriptableObject script: " + AssetDatabase.GetAssetPath(sourceScriptAsset));
         // Use DestroyImmediate() to destroy the created asset in edit mode
         DestroyImmediate(tmpInstance);
     }
     #endif
 }

If you click on the new menu Tests > Log TestScriptableObject script path, you will see the path to the source script path in the console window.

I hope I helped :)

Comment
Add comment · Show 1 · 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 JasonC_ · Jun 05, 2021 at 07:56 AM 0
Share

This doesn't work if TestScriptableObject is a static class, though.

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

72 People are following this question.

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

Related Questions

DirectoryNotFoundException with full existing path... ? How can this be? I need an answer ASAP 1 Answer

Possible to move directory without reimporting assets? 3 Answers

Multiple Editor and Plugins Directory 3 Answers

Unity double save/back-up 1 Answer

Debug.Log(files in a directory) problem 0 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