x


How to get a Texture asset that is in the project and pass it to a function?

For example, I have a function which takes in a Texture

function ClearTexture(tex : Texture2D)  
{
   // blah blah blah
}

And I have a texture named "Canvas" in my project. Is there any code I can use to get that texture and pass it to my function, hypothetically something like this?

ClearTexture(FindTextureByName("Canvas"));

Edit: The texture is not attached to any game object or part of any models. It just exists in the project's Assets folder.

more ▼

asked Mar 25 '10 at 09:24 AM

Extrakun gravatar image

Extrakun
1.3k 44 56 70

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I believe assets has to be attached to a game component somehow, otherwise it will not be included in the built game. On the component you are creating, you could create an array of textures, and assign the possible textures to the elements in the array.

In C#:

Texture2D Textures[];

Texture2D FindTexture(string name)
{
    foreach (Texture2D tex in Textures)
    {
        if (tex.name == name)
        {
            return tex;
        }
    } 
    return null;
}

If you don't want to attach the textures to the component, an alternative is to add the textures to a resource folder, and Resource.Load to load the texture

more ▼

answered Mar 25 '10 at 09:28 AM

KvanteTore gravatar image

KvanteTore
1.5k 8 15 33

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5056
x2188
x353
x216

asked: Mar 25 '10 at 09:24 AM

Seen: 5023 times

Last Updated: Mar 25 '10 at 09:44 AM