- Home /
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
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.
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.
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.
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;
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.
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;
}
}
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.
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
Your class and your static constructor have different names. Does that even compile?
Your answer

Follow this Question
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