using UnityEngine;
using System.Collections;
using System;
public class ImageSaver : MonoBehaviour {
public Texture2D test;
WWW www;
void Start () {
StartCoroutine( SaveTexture("bar","bar"));
}
public IEnumerator SaveTexture(string imageName,string saveAs)
{
Texture2D tex;
byte[] byteArray;
WWW www= new WWW("file://"+Application.dataPath+"//Resources//"+imageName+".png");
yield return www;
if(www.texture!=null)
{
print("success");
}
tex=www.texture;
byteArray=tex.EncodeToPNG();
string temp=Convert.ToBase64String(byteArray);
PlayerPrefs.SetString(saveAs,temp); /// save it to file if u want.
PlayerPrefs.SetInt(saveAs+"_w",tex.width);
PlayerPrefs.SetInt(saveAs+"_h",tex.height);
}
public Texture2D RetriveTexture(string savedImageName)
{
string temp=PlayerPrefs.GetString(savedImageName);
int width=PlayerPrefs.GetInt(savedImageName+"_w");
int height=PlayerPrefs.GetInt(savedImageName+"_h");
byte[] byteArray= Convert.FromBase64String(temp);
Texture2D tex = new Texture2D(width,height);
tex.LoadImage(byteArray);
return tex;
}
void Update () {
test=RetriveTexture("bar");
}
}
hey this is the tested code that works at my end. I am using www to load the texture inside the game because you cannot encode the texture imported inside unity, unless it is imported as ARGB32 or RGB24. so im loading it like an external image file and processing it. other than that it is pretty much explainable i guess.