- Home /
Assigning script-generated textures
Hello everyone, new guy here. I've come across a particularly strange problem (I think).
So I'm trying to recreate an old game and one of the requirements I've set myself is that my program has to be compatible with the old game's data files (which are in an old format that's not supported by Unity). One of these data files contains textures, and as far as I can tell the code I've written is reading those out properly. The problems start when I try to assign a texture to a Unity 3D object: the texture simply does not show up, and the object remains its default white-greyish color. Here's the test code I'm working with at the moment (simplified):
//this script is attached to an empty GameObject
public class Initialize : MonoBehaviour {
Style theStyle;
List<GameObject> gameObjects;
void Awake () {
readStyle ();
gameObjects=new List<GameObject>();
}
private void readStyle()
{
//stuff happens here that reads out a file, creates a Style object and assigns it to theStyle.
//It contains public Texture2D[] tileTextures, which contains all textures.
}
void Start () {
GameObject objectToAdd = GameObject.CreatePrimitive (PrimitiveType.Cube);
objectToAdd.renderer.material.mainTexture=theStyle.tileTextures[10]; //just assigning one of the textures to the cube
gameObjects.Add (objectToAdd);
}
}
The above code didn't work the way I expected it to: the cube that is created is untextured. I tried lots of things to figure out what the problem was. One thing I did was the following test: to check whether tileTextures actually contains textures and a texture gets properly assigned to the cube, I modified Start() as follows:
void Start () {
GameObject objectToAdd = GameObject.CreatePrimitive (PrimitiveType.Cube);
objectToAdd.renderer.material.mainTexture=theStyle.tileTextures[10];
File.WriteAllBytes(Application.dataPath + "/Resources/file12412.png", ((Texture2D)(objectToAdd.renderer.material.mainTexture)).EncodeToPNG ());
Texture2D tex = Resources.Load("file12412") as Texture2D;
objectToAdd.renderer.material.mainTexture = tex;
gameObjects.Add (objectToAdd);
}
So, basically, after assigning the texture to the cube, it then takes the cube's texture and exports it as a file. I inspected the resulting file (file12412.png) and it looks fine. After exporting the file, it then imports it again and assigns the imported texture to the cube. The reason I did that is because I wanted to see if a texture that is created using one of Unity's standard functions would actually work properly. It didn't produce any change at all though, but strangely enough, when I then ran the program again a little while later, with the file12412.png file still in the Resources folder, the cube showed up textured correctly.
I then cleared the resources folder to verify this behavior, and this happened:
1st time running without anything in the resources folder, the cube remained untextured.
2nd time running (immediately after the 1st time, but with the file that was generated during the 1st time running still in the resources folder), the cube remained untextured
3rd time running directly after the 2nd time, the cube shows up textured.
It seems to me Unity has to get used to the idea of there being a file in its resources folder or something.
But the most important question is: Why isn't the cube showing up properly textured without all this file exporting/importing? What am I doing wrong?
Feel free to ask for clarifications of course, and thanks in advance for any help!