- Home /
[solved] How to save instance of the texture?
With the following code I make sure that the revenues image from URL: [Javascript]
var modys : boolean = false;
var modus : boolean = false;
private var windowRect : Rect =new Rect (Screen.width/2, 300, 220, 200);
var Text : String = "";
var TextName : String = "";
var t : TextMesh;
var nameTexture : TextMesh;
function OnMouseDown() {
modys = !modys;
}
function OnGUI () {
if(modys){
// Register the window. Notice the 3rd parameter
windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
}
}
// Make the contents of the window
private function DoMyWindow (windowID : int) {
GUILayout.Label("URL");
Text = GUILayout.TextField (Text, 10000);
GUILayout.Label("NameImage");
TextName = GUILayout.TextField (TextName, 10000);
if(GUILayout.Button ("Save")) {
t.text = Text;
nameTexture.text = TextName;
modys = false;
Attive();
Debug.Log(t.text);
}
if(GUILayout.Button ("Close")) {
modys = false;
}
}
function Attive() {
var url = t.text;
// Create a texture in DXT1 format
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
// Start a download of the given URL
www = new WWW(url);
yield www;
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture(renderer.material.mainTexture);
Debug.Log(url);
}
I would like to save the instance of the image texture, how can I do? The system is activated by clicking on the plan that has the texture you will be asked to put a link and the name of the future texture. Then when you insert the texture is displayed in the plan, but has not yet created a new texture, so I need to create it, so I have to make sure that you save the texture taken from the URL, and that will give him the name chosen by the user , and subsequently recall to save to that particular GameObject (the plane) that determined texture.
Help me. Thank you.
Answer by whydoidoit · Apr 17, 2013 at 06:00 PM
You can save the texture by doing:
import System.IO;
...
File.WriteAllBytes(Application.persistentDataPath + "/" + someFilename, someTexture.EncodePNG());
Thank you for taking my call. The error which gives me is the following:
BCE0017: The best overload for the method 'System.IO.File.WriteAllBytes (String, byte [])' is not compatible with the argument list '(System.Object, String)'.
Thanks for your help.
The arguments are actually reversed - the path part comes first. Just swap the arguments and you should be good...
File.WriteAllBytes(Application.persistentDataPath + "/" + someFilename, (byte[]) someTexture.EncodePNG());
The new error is :
BCE0043: Unexpected token: ).
but i don't see the error in the script that you have posted
You have the wrong number of ) compared to (. Double check your line.
This is a very basic compiler error, btw.
As Dr. Cox would say, You should try to figure some of this stuff out on your own, newbie, or you're never going to make it as a coder.
Answer by netkingZ · Apr 17, 2013 at 10:18 PM
Ok I found a way to save the texture, but the saves in this way:
*http://i46.tinypic.com/2u5vsrs.png*
instead of the original image is thus:
*http://www.gamerscritic.it/wp-content/uploads/2013/01/remember-me-shot.jpg*
why?
First try with an ARGB32 image - as that's the standard for PNG format. Does that work?
I created one for you so you can be sure you have the right format:
This is the solution I found:
// Opens a file selection dialog for a PNG file and saves a selected texture to the file.
import System.IO;
function Attive() {
var texture = new Texture2D (128, 128, TextureFormat.ARGB32, false);
var textures = new Texture2D (128, 128, TextureFormat.ARGB32, false);
if (textures.Length == 0) {
EditorUtility.DisplayDialog("Select Textures",
"The selection must contain at least one texture!", "Ok");
return;
}
var path = EditorUtility.SaveFolderPanel("Save textures to directory", "", "");
if (path.Length != 0) {
// for( var texture : Texture2D in textures) {
// Convert the texture to a format compatible with EncodeToPNG
if (texture.format != TextureFormat.DXT1 && texture.format != TextureFormat.RGB24) {
var newTexture = Texture2D(texture.width, texture.height);
newTexture.SetPixels(texture.GetPixels(0),0);
texture = newTexture;
}
var pngData = texture.EncodeToPNG();
if (pngData != null )
File.WriteAllBytes(path + "/" + nameTexture.text + ".png", pngData);
else
Debug.Log("Could not convert " + texture.name + " to png. Skipping saving texture");
}
AssetDatabase.Refresh();
}
Here is cross platform solution to save screenshot to gallery. http://unitydevelopers.blogspot.in/2014/06/capture-and-save-to-camera-roll-and.html
There are 2 versions so you have freedom to select as per your need.