How do you load in a image to a Texture2D object programmatically? I am trying to create a GUISkin in code and I want to set the background of the style to an image without having to set it in the unity editor. I see that there is a LoadImage method that has one overload that accepts a byte array, but I haven’t found sufficient documentation on it to understand it.
thats no problem,
its just skin.GetStyle(“Your Style Name”).normal.background = blabla
for example … same for the other states etc naturally
Sorry, but I don’t quite understand. I did a typo where I said GUISkin when I meant GUIStyle, but I assume both are very similar.
Lets assume I have no other GUI elements defined in the project and we are dealing with a fresh slate. I would first create a GUIStyle and then set the .normal.background property to a Texture2D right? My question is how do I get a texture from my assets loaded into that Texture2D object?
Bump… Does no one know how to do this? There has to be a simple way to do it.
To get a texture, you can add a variable of type Texture2D to your script and then link the desired texture asset to it in the inspector. Alternatively, put the texture into the Resources folder and use Resources.Load to access it.
Sweet! That looks like exactly what I was searching for. Thanks Andeeeeeeeee
I know this is a very old post, but for anyone who hasa the same problem now, you can do this:
Texture2D myTexture;
myTexture = (Texture2D) Resources.load(“myTexture”);
Then in your assets folder, create a new folder called “Resources” and drag the image you want to load into it and name it the same name that you put in the string above, eg: “myTexture.jpg”