File.WriteAllBytes not working?

Hi,
I’ve been scripting some weird code lately that lets me project a mesh’s vertices onto a texture, adds blur to them and colors the whole thing in a way to show the distance between the mesh’s surface features and a flat surface.
After all of that stuff (which is working fine, surprisingly), I want to convert the texture into PNG using texture.EncodeToPNG() and then save this texture to disk. To do do, I tried out File.WriteAllBytes(), but Unity (v4.1.2f) doesn’t seem to know this expression, although many people on the forums seem to be using it.

I get this error message:

So basically, I did some research in the scripting reference and couldn’t find any class called “File” or any function “WriteAllBytes”. Yet at the same time, an example of precisely this block of code can be found here:
http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html

I pretty much copied-pasted the corresponding part into my code and get the above error message. What am I doing wrong? And what might cause this? Why doesn’t unity know the expression? Is File.WriteAllBytes obsolete or outdated?

This is my script:

function SaveTextureToFile () {
	var map = targetMap;
	var bytes = map.EncodeToPNG();
	File.WriteAllBytes(Application.dataPath + "/../" + targetMap.name + ".png", bytes);
}

Have you imported System.IO ? Because File class is inside that namespace.

2 Likes

You won’t find .NET functions in the Unity docs (mostly) since they are covered thoroughly by the MSDN docs.

–Eric

Thanks guys :smile:

Using import System.IO; did the trick. Everything working fine now :slight_smile: