Accessing Assets from code

Hi evryone!

I would like to know if there is any way to access an asset from within a script.

As an example, I would like to access Assets/Images/myImage.psd from within

private Texture2D myTexture; Start(){

myTexture = // access to Assets/Images/myImage.psd

}

OnGUI(){

GUI.DrawTexture(new Rect(0, 0, 50, 50), myTexture);

}

Thanks in advance!

Simon

1 Like

You have to put the asset in a folder called "Resources". Then it's a matter of using Resources.Load.

Texture2D myTexture; 

void Start()
{
    // Given you have placed Images/myImage.psd in a folder
    // called Resources:
    myTexture = Resources.Load("Images/myImage") as Texture2D;
}

void OnGUI()
{
    GUI.DrawTexture(new Rect(0, 0, 50, 50), myTexture);
}

http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html

but I’m sure you don’t have to rely on the resource folder, why cant I access my images folder?

Might as well just use the resource folder. Unity often requires you to name things in a certain way for things to work.

The posts above have improper syntax.