- Home /
How would I cast "someString" in the following code in order to get it to work?
I have some textures in the Resources folder and imported it with the Texture Importer.
In the C# script, I manipulate a type String that is catenated with the texture's names.
How would I cast "someString" in the following code in order to get it to work?
GUILayout.Button(Resources.Load(someString), GUILayout.Width(150))
Thanks CSDG
Answer by Bunny83 · Jun 10, 2011 at 02:43 AM
I don't get your problem... You said "someString" is a string there is no need to cast the string. I guess you want to cast the object that is returned by Resources.Load into a Texture2D.
Another thing:
You would call Resources.Load several times each frame. You only should load the texture once, at start:
private Texture2D myGUITexture = null;
void Start()
{
[...] // build the string
myGUITexture = (Texture2D)Resources.Load(someString,typeof(Texture2D));
if (myGUITexture == null)
{
Debug.LogWarning("Texture '"+someString+" no found");
}
}
void OnGUI()
{
[...]
GUILayout.Button(myGUITexture, GUILayout.Width(150));
[...]
}
Thanks Bunny83:
I will try this. When studying the documentation, it is mainly in javascript so a little frustration tends to build.
I noticed there are the Texture and the Texture2D. I assume they are different. Your advice is noted. When I declare a string, I simply use the type "String". Is there another or preferred string type I should use?
Thanks again CSDG
There are C# examples for most of the documentation. If you look where it says "JavaScript" that is actually a drop down which also contains C# examples.