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
0
Question by Khena_B · Feb 06, 2018 at 04:42 PM · loadpathassetdatabasefolderrelative

AssetDatabase current folder?

Hey,

I want to load an asset from script, but is there a way to look for the asset in the same folder of the script or a sub folder, so that the asset can be moved without relying on a path to look for? Right now i'm using AssetDatabase.LoadAssetAtPath, i don't want to use the resource folder. Thanks

Comment
Add comment · Show 4
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 MacDx · Feb 06, 2018 at 05:11 PM 0
Share

I want to load an asset from script, but is there a way to look for the asset in the same folder of the script or a sub folder, so that the asset can be moved without relying on a path to look for?

Everything needs a path. It is a must. Paths are how you locate files, you can't load a file without knowing exactly where it is. You'll need a path one way or another. You can certainly make a system that abstracts paths, make them hidden, like it wasn't using them. Something like having an ID for every file and have your system track where each file is, even when it is moved, so the ID always points to the correct path. Sort of what Unity does with their assets, since you can move them and objects that reference them like a $$anonymous$$aterial uses a Texture won't simply lose the reference. I've never done anything like that but it certainly won't be easy to achieve.

So my question is, what exactly do you need this for? What's the end goal? Why is it so important that your scripts can find files even when these are moved? Why would they be moved?

Note: You should also remember that when you build your game, scripts won't exist the same way they are in the editor. They will get compiled into an assembly file (.dll) and put in a folder somewhere else than your project path.

avatar image Khena_B MacDx · Feb 06, 2018 at 05:18 PM 0
Share

I'm making a custom editor script which uses assetdatabase.loadassetatpath to get a texture file, but i don't want the script to rely on a specific path to work, i want it to look in it's own folder or sub folder for the texture file.

avatar image MacDx Khena_B · Feb 06, 2018 at 06:09 PM 0
Share

Ahhhh an editor script. Ok so you want this script to look for a file and if it doesn't find it then look for it in every subfolder until it finds it.

Show more comments

2 Replies

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

Answer by Khena_B · Feb 06, 2018 at 08:04 PM

I went with Bunny83's suggestion and used a Resources folder inside my Editor folder and used this line of code to get the texture:

 texture = Resources.Load("Testicon") as Texture2D;
Comment
Add comment · 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
2

Answer by Bunny83 · Feb 06, 2018 at 05:47 PM

There are several ways how this can be done.


Editor Resources Folder

If you create a resources folder inside an editor folder those resources will only be available to editor scripts. You would use EditorGUIUtility.Load to load resources from such a folder. Just like the runtime resources folder the required path is relative to the resources folder. So it doesn't matter where the folder is located as long as the path within the resources folder stays the same. See Special Folders for some more information.


Relative path

Another way could be to figure out the location of the actual editor script. Most editor classes are actually ScriptableObjects (Editor, EditorWindow, ...). The usual event order would be something like:


  • MonoScript.FromScriptableObject to figure out the MonoScript asset of the current instance of your editor script.

  • Use AssetDatabase.GetAssetPath to retrieve the assetpath of your script.

  • Use some string manipulation and / or the System.IO.Path class to construct a new relative path based on the path of your script.

Comment
Add comment · Show 6 · 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 Khena_B · Feb 06, 2018 at 06:23 PM 0
Share

I don't understand how to use $$anonymous$$onoScript.FromScriptableObject without an instance, here is an example script where i need to get the texture file:

  using UnityEditor;
  using UnityEngine;
  using System.Collections.Generic;
  
  [InitializeOnLoad]
  class CustomHierarchy
  {
      static Texture2D texture;
      
      static CustomHierarchy()
      {
          texture = AssetDatabase.LoadAssetAtPath ("Assets/Images/Testicon.png", typeof(Texture2D)) as Texture2D;
      }
 }
avatar image Bunny83 Khena_B · Feb 06, 2018 at 06:30 PM 0
Share

Well, this script is not a recognisable or trackable editorscript. It doesn't represent any sort of asset and therefore doesn't have an assetpath at all. $$anonymous$$eep in $$anonymous$$d that all code is compiled into assemblies (DLL files). So when the code runs it doesn't know where the code for a particular class comes from. Only assets ($$anonymous$$onoBehaviour or ScriptableObject derived classes) actually have a link between the script file and the contained class. That's why for those classes the file name has to match the classname. Your script example is pretty pointless. Where and how do you use that texture?


In your case i would recommend using a resources folder inside an editor folder.

avatar image Khena_B Bunny83 · Feb 06, 2018 at 06:36 PM 0
Share

I use EditorApplication.hierarchyWindowItemOnGUI and GUI.Label(rect, texture) to add the texture to my hierarchy, the script above is incomplete.

$$anonymous$$y script is similar to this: https://answers.unity.com/questions/431952/how-to-show-an-icon-in-hierarchy-view.html

Show more comments
avatar image MacDx Khena_B · Feb 06, 2018 at 06:51 PM 0
Share

Your class and your static constructor have different names. Does that even compile?

avatar image Khena_B MacDx · Feb 06, 2018 at 06:54 PM 0
Share

That was just a typo

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

75 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 avatar image avatar image avatar image

Related Questions

Load the sprite property of a png file from Application.persistentDataPath 2 Answers

Please Help With Folder Creation Script 0 Answers

Why dont FBX texture import and apply on the moment the fbx is imported? 0 Answers

How are unity references serialized? (instanceID related) 1 Answer

how to convert absolute path to relative Application.DataPath 1 Answer


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