- Home /
Access assets from project window through code
Hello,
This might seem a bit of a noob question, but, you know when you use 'var variable : GameObject' for example, and then in the inspector you drag and drop your game object from the project pane.
Is there a way to define your variable in your script via using the project file.
For example:
var Cube : GameObject;
function Start(){
Cube = Assets/Prefabs/cube.prefab;
}
Just to save me time, so I don't have to keep clicking and dragging. I do not know what this methord is called, or if its even possible. Any advice would be appreciated.
I renamed the question to be more broad, I hope you don't $$anonymous$$d.
Answer by Joshua · Jul 08, 2011 at 12:46 PM
Not a nooby question at all! It's one of those things I also couldn't find any proper documentation on until some people pointed me in the right direction, whilst it is on of the most important things there is if you want to speed things up on a large scale project.
Do you want this to happen at run time or in the editor?
For instance, if you have a folder with fifty images and you want to display a random one, then you want to do this at run time (you don't want to load the other 49 into memory if you're not going to use them).
On the other hand, if you have a deck of cards and and to be able to use all 52 textures without manually drag and dropping them in, you want to establish this connection during edit time.
To do this at run time use the Resources class. All the assets you want to access should be places in a folder, in the root of your project, named "Resources". You can then access them using for instance Resources.LoadAssetAtPath( path : String ), or even Resources.LoadAll( path : String, type : Type). The downside is that everything in the Resources folder is automatically included in your build (normally unused assets are not), unless you explicitly declare it not to be using Resources.UnloadUnusedAttets(). An other downside is that it'll take 'some' time to establish this connection.
If you decide to do this during edit time you should look into the AssetDatabase class, which is only available in edit mode and on scripts places in a folder named "Editor" (need not be in the root of you project, you can have as many as you want whereever you want). With this awesome class you can access and control your entire project folder. You can move assets, rename assets, duplicate assets, edit assets and load assets. This is what you're probably looking for here. Use AssetDatabase.LoadAssetAtPath( path : String ) to establish a connection.
An example would be, using me deck of cards example from above, having one script named DeckOfCards and one script named DeckOfCards_Editor. The first script would read:
var cardTextures : Texture[];
function Start()
{
Shuffle();
}
function Shuffle()
{
//mix the textures up
}
The editor script, places in the befor ementioned Editor folder, would read:
import UnityEditor.EditorGUILayout;
@CustomInspector( DeckOfCards ) class DeckOfCards_Editor extends Editor
{
var folder : Object;
var endString : String = "(DefaultAsset)"//DragAndDropTitle will put this behind the path, so remove it. May be different, untested, but I assume it's a defaultAsset
var path : String;
override function OnInspectorGUI()
{
if( path == null )
path = "no folder selected";
if( Button( "find path" )
{
BeginHorizontal()
PrefixLabel( "folder: " );
folder = ObjectField( folder, Object );
path = ObjectNames.GetDragAndDropTitle( folder );
path = path.TrimEnd( endString.toCharArray() );
var textures : Texture[] = AssetDatabase.LoadAllAssetsAtPath( path );
target.textures = textures;
}
}
}
Note that this is untested and probably contains a lot of errors and mistakes. It should give you a good idea of how to use this - and using this properly can make life for your artists a LOT easier. Note that you can ignore the entire editor script if you want to manually hard-code the path, but this is will break if you move the asset, is annoying because you have to look up the path, and will have to be done each time you want to do something like this.
Your answer
Follow this Question
Related Questions
Relative asset paths 1 Answer
Scriptable object keeps referencing the objects? 1 Answer
How to play music from computer ?? 1 Answer
Asset Server Team and project management Unity Pro 2 Answers
Complete Lerpz Tutorial 1 Answer